2011-04-29 75 views
3

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

回答

3

我做了類似的事情,這是我的副本:

我打開一個txt文件,|作爲分隔符。然後將工作表的內容複製到我的目標工作簿(全局變量)。然後關閉包含原始txt文件的第一個工作簿而不保存。

Workbooks.OpenText的代碼基本上是記錄一個宏並將其調整爲我的需要。

Sub ImportTextFile(path As String) 
    Dim SheetName As String 
    Dim TMPWorkBook As Workbook 
    Dim FilePath As String 
    Dim TxtFilePath As String 
    Dim TxtFileName As String 

    Set WB = ThisWorkbook 

    SheetName = "Test_Result" 
    TxtFileName = path & "file.txt" 

    Workbooks.OpenText FileName:= _ 
     TxtFileName _ 
     , Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _ 
     xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, _ 
     Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 1), _ 
     Array(2, 1)), DecimalSeparator:=".", ThousandsSeparator:=",", _ 
     TrailingMinusNumbers:=True 

    Set TMPWorkBook = ActiveWorkbook 
    TMPWorkBook.Sheets("file").Select 
    Cells.Select 
    Selection.Copy 

    ResultWB.Activate 

    ResultWB.Sheets(SheetName).Select 
    Range("A1").Select 
    ActiveSheet.Paste 
    Application.CutCopyMode = False 
    Cells.Select 
    Cells.EntireColumn.AutoFit 
    ActiveSheet.Range("A1").Select 
    TMPWorkBook.Close savechanges:=False 

End Sub 
+0

完全沒問題,我看到我做的副本和粘貼,而且你的代碼可能更好,因爲其他人使用這些代碼可以稍微更改代碼以更好地滿足他們的需求,而不是使用我的代碼。感謝您的答覆 – 2011-04-29 22:01:19

1

我發現一些代碼,做拷貝,它似乎是正確的(只測試一次儘管如此,對不起)

對不起,發佈這個問題,它只是我找到的代碼不能正常工作,但我能弄明白。

'' The sheet is added here 
Sheets.Add.Name = "Temp_Text_File" 
'' Going through the code I think when you add a sheet it's automatically targetted, but this is a precaution -- makes "Temp_Text_File" the active sheet 
ActiveWorkbook.Sheets("Temp_Text_File").Activate 
'' Next three lines open and copy text file (not physically opened.... just opened to read) 
Set fso = CreateObject("scripting.FileSystemObject") 
Set f = fso.GetFile(filePath) 
f.Copy (filePath) 
'' This paste method will start at range "A1" 
ActiveSheet.Paste 

謝謝

傑西Smothermon

+0

如果你的txt文件的內容是由一些字符分隔(逗號,分號,...),那麼你應該使用'Workbooks.OpenText'方法測得的值進入正確的列。 (請參閱我的解決方案) – stema 2011-04-29 22:02:50

+0

是的,這就是爲什麼我選擇了我的答案,最終答案更靈活 – 2011-04-29 22:11:06

相關問題