我想要统计文本文件中的特定列,我认为最好的方法是将所有内容从文本文件复制到Excel工作表中,然后从那里计算它(否则我需要尝试直接从Excel文件中读取那一行)。下面是我到目前为止的代码:VBA Excel复制文本文件到工作表
Dim filePath As String
Dim currentValue As String
Dim iRow As Long
Dim iCol As Long
Dim badAddress As Long
Dim coverageNoListing As Long
Dim activeListing As Long
Dim noCoverageNoListing As Long
Dim inactiveListing As Long
Dim fso As Object
Dim f As Object
'' filePath would include entire file name (picked from a browser button)
filePath = ActiveSheet.Range("B2").Text
'' Makes sure there isn't a sheet named "Temp_Text_File"
For Each testSheet In ActiveWorkbook.Worksheets
If testSheet.Name Like "Temp_Text_File" Then flag = True: Exit For
Next
'' If there is a sheet named "Temp_Text_File" it will be deleted
If flag = True Then
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("Temp_Text_File").Delete
Application.DisplayAlerts = True
End If
'' Recreate sheet
Sheets.Add.Name = "Temp_Text_File"
'' Here I would want to copy everything (similar to manually doing "Ctrl+A" then "Ctrl+C") from the text file
'' Then paste into worksheet (similar to manually doing "Ctrl+V") within this created worksheet range("A1")
'' Delete at the end (user has no need for it)
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("Temp_Text_File").Delete
Application.DisplayAlerts = True
谢谢
杰西Smothermon
完全没问题,我看到我做的副本和粘贴,而且你的代码可能更好,因为其他人使用这些代码可以稍微更改代码以更好地满足他们的需求,而不是使用我的代码。感谢您的答复 – 2011-04-29 22:01:19