Results 1 to 1 of 1

VBA code to import text file to the fixed excel file

Threaded View

  1. #1
    Registered User
    Join Date
    08-30-2010
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    1

    VBA code to import text file to the fixed excel file

    Hi

    Currently I need a macro to import text file into a excel file and this is the code below. However every time when the text file is import, a new excel file will be created, can anyone help me to change the code, so that the text file will always import into the same excel file name " monthly standard working file".
          Dim ResultStr As String
          Dim FileName As String
          Dim FileNum As Integer
          Dim Counter As Double
          FileName = InputBox("Enter the Text File's name, e.g. test.txt")
          If FileName = "" Then End
          FileNum = FreeFile()
          Open FileName For Input As #FileNum
          Application.ScreenUpdating = False
          Workbooks.Add template:=xlWorksheet
          Counter = 1
          Do While Seek(FileNum) <= LOF(FileNum)
              Application.StatusBar = "Importing Row " & _
                 Counter & " of text file " & FileName
              Line Input #FileNum, ResultStr
              If Left(ResultStr, 1) = "=" Then
                 ActiveCell.Value = "'" & ResultStr
              Else
                 ActiveCell.Value = ResultStr
              End If
              If ActiveCell.Row = 65536 Then
                 ActiveWorkbook.Sheets.Add
              Else
                 ActiveCell.Offset(1, 0).Select
              End If
              Counter = Counter + 1
          Loop
          Close
          Application.StatusBar = False
         
          End Sub
    Last edited by Leith Ross; 08-30-2010 at 10:06 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1