我試圖創建一些東西從.txt文件讀取數據,然後將數據填充到.xls中,但打開.txt文件後,如何獲取數據?基本上我試圖獲得日期爲'04/06/2010'的第三列。打開.txt文件後,當我使用時,ActiveSheet
未指向.txt文件。如何將.txt文件中的數據填充到VBA中的Excel中?
我的.txt文件是這樣的(空格分隔):
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
下面是我的代碼有:
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
誰能指出哪裏出了問題嗎?
改變格式我要補充一點,我在你的代碼中沒有遇到任何錯誤,它只是沒有做任何事情。 – 2010-04-05 21:09:15