LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Get External Data Macro

I have put together a skeleton from code I have used and I tested it.
Make sure you add "Microsoft Scripting Runtime" to References in Visual
Basic Editor.

It pops up the open file dialog, creates a new worksheet "NewSheet",
reads a file and populates "NewSheet"


Sub Test()
ReadFile
End Sub

Public Function ReadFile() As Boolean

' Add Tools-Reference-Microsoft Scripting Runtime
Dim fsoFileSystemObject As FileSystemObject
Dim strFileName As String
Dim fFile As File
Dim tsTextStream As TextStream
Dim strLine As String
Dim wsNewSheet As Worksheet
Dim i As Integer

Set fsoFileSystemObject = CreateObject("Scripting.FileSystemObject")

strFileName = Application.GetOpenFilename()

If strFileName = "False" Then
MsgBox "Cancelled"
Else
Set wsNewSheet =
Worksheets.Add(After:=Worksheets(Worksheets.Count) )
wsNewSheet.Name = "NewSheet"
Set fFile = fsoFileSystemObject.GetFile(strFileName)
Set tsTextStream = fFile.OpenAsTextStream(ForReading)
i = 0
Do While tsTextStream.AtEndOfStream < True
strLine = tsTextStream.ReadLine
wsNewSheet.Range("A1").Offset(i, 0) = strLine
i = i + 1
Loop
tsTextStream.Close
End If

Set fsoFileSystemObject = Nothing

Application.StatusBar = "Finished reading file."

ReadFile = True

End Function






*** Sent via Developersdex http://www.developersdex.com ***
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get External Data Macro mkerstei Excel Discussion (Misc queries) 0 August 5th 05 06:46 PM
import external data via macro/vba question Drabbacs Excel Programming 1 May 24th 04 11:36 PM
Run Macro on External Data Refresh John Witte Excel Programming 3 December 3rd 03 01:42 PM
Run Macro on External Data Refresh John Witte Excel Programming 0 December 3rd 03 12:49 AM
vba macro for getting external data pmz Excel Programming 1 October 31st 03 07:42 AM


All times are GMT +1. The time now is 03:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"