如何将.text文件中的数据填充到VBA中的Excel中?

时间:2010-04-05 19:31:59

标签: excel vba text file

我正在尝试创建一些内容来从.txt文件中读取数据,然后将数据填充到.xls中,但在打开.txt文件后,如何获取数据?基本上我正试图获得'04 / 06/2010'的第三栏。打开.txt文件后,当我使用ActiveSheet.Cells(row, col)时,ActiveSheet未指向.txt文件。

我的.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

有人能指出这里出错的地方吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

下面的代码将读取文本文件并将值粘贴到Sheet2的单元格中。但是,如果您在Date列中放置一个格式

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;@")