How to populate data from .txt file into Excel in VBA?

Posted by swei on Stack Overflow See other posts from Stack Overflow or by swei
Published on 2010-04-05T19:31:58Z Indexed on 2010/04/06 0:53 UTC
Read the original article Hit count: 321

Filed under:
|
|
|

I'm trying to create something to read data from a .txt file, then populate data into .xls, but after open the .txt file, how do I get the data out? Basically I'm trying to get the the third column of the lines dated '04/06/2010'. After I open the .txt file, when I use ActiveSheet.Cells(row, col), the ActiveSheet is not pointing to .txt file.

My .txt file is like this (space delimited):

04/05/10 23 29226
04/05/10 24 26942
04/06/10 1 23166
04/06/10 2 22072
04/06/10 3 21583
04/06/10 4 21390

Here is the code I have:

Dim BidDate As Date

BidDate = '4/6/2010'

Workbooks.OpenText Filename:=ForecastFile, StartRow:=1, DataType:=xlDelimited, Space:=True

If Err.Number = 1004 Then
    MsgBox ("The forecast file " & ForecastFile & " was not found.")
    Exit Sub
End If

On Error GoTo 0


Dim row As Integer, col As Integer


row = 1
col = 1

cell_value = activeSheet.Cells(row, col)
MsgBox ("the cell_value=" & cell_value)

Do While (cell_value  <> BidDate) And (cell_value <> "")
    row = row + 1
    cell_value = activeSheet.Cells(row, col)
   ' MsgBox ("the value is " & cell_value)
Loop

If cell_value = "" Then
    MsgBox ("A load forecast for " & BidDate & " was not found in your current load forecast file titled '" + ForecastFile + ". " + "Make sure you have a load forecast for the current bid date and then open this spreadsheet again.")
    ActiveWindow.Close
    Exit Sub
End If

Can anyone point out where it goes wrong here?

© Stack Overflow or respective owner

Related posts about vba

Related posts about excel