2010-04-05 37 views
0

我試圖創建一些東西從.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 

誰能指出哪裏出了問題嗎?

回答

0

在下面的示例中,我將變量ws設置爲等於我想要的工作表,並且可以稍後使用該變量來引用工作表。關鍵字ActiveWorkbook應該指向新打開的文本文件。我可以告訴你想要怎麼處理這些信息,比如我做了一些東西。

Sub GetBidData() 

    Dim dtBid As Date 
    Dim ws As Worksheet 
    Dim rFound As Range 
    Dim sFile As String 

    dtBid = #4/6/2010# 
    sFile = Environ("USERPROFILE") & "\My Documents\ForecastFile.txt" 

    Workbooks.OpenText Filename:=sFile, _ 
     StartRow:=1, _ 
     DataType:=xlDelimited, _ 
     Space:=True 
    Set ws = ActiveWorkbook.Sheets(1) 

    Set rFound = ws.Columns(1).Find(_ 
     Format(dtBid, ws.Range("A1").NumberFormat), , xlValues, xlWhole) 

    If Not rFound Is Nothing Then 
     MsgBox rFound.Value & vbCrLf & _ 
      rFound.Offset(0, 1).Value & vbCrLf & _ 
      rFound.Offset(0, 2).Value 
    End If 

End Sub 
+0

改變格式我要補充一點,我在你的代碼中沒有遇到任何錯誤,它只是沒有做任何事情。 – 2010-04-05 21:09:15

0

通常應該避免使用ActiveWorkbook對象,除非你是積極的,要引用時,運行您的代碼將永遠積極的工作簿。相反,您應該將您正在使用的工作簿設置爲一個變量。理論上,您應該可以使用OpenText方法來執行此操作,但VBA不會那樣。 (我敢肯定這是一個錯誤),所以,正確的,你打開你的文本文件後,我會做到這一點:

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

Dim ForecastWorkbook As Workbook, book As Workbook 
Dim ForecastFileName As String 

ForecastFileName = "YourFileNameHere.txt" 

For Each book In Application.Workbooks 

    If book.Name = ForecastFileName Then 

     Set ForecastWorkbook = book 
     Exit For 

    End If 

Next book 

然後,不是這個......

cell_value = activeSheet.Cells(row, col) 

...這樣做...

cell_value = ForecastWorkbook.Sheets(1).Cells(row, col).Value 
0

下面的代碼將讀取文本文件並將值粘貼到Sheet2的單元格中。但是如果你把日期列的格式,將這樣的伎倆

Public Sub Read_text() 
Sheet2.Activate 
Set fso = New FileSystemObject 
Fname = Application.GetOpenFilename 
x = 1 
y = 1 
Set Stream = fso.OpenTextFile(Fname, ForReading, True) 
Do While Not Stream.AtEndOfStream 
      Str_text = Stream.ReadLine 'Perform your actions 
      rdtext = Split(Str_text, " ") 
      Sheet2.Cells(x, y) = rdtext(0) 
      Sheet2.Cells(x, y + 1) = rdtext(1) 
      Sheet2.Cells(x, y + 2) = rdtext(2) 
      x = x + 1 
      y = 1 
Loop 
Stream.Close 
End Sub 

例如:下面的代碼將在'05/04/2010'

Sheet2.Cells(x, y) = Format(rdtext(0), "mm/dd/yyyy;@") 
+0

它總是也解釋你的答案,而不僅僅是轉儲代碼。 – 2017-04-11 11:45:49

相關問題