我想要統計文本文件中的特定列,我認爲最好的方法是將所有內容從文本文件複製到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