2011-04-29 49 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

相关问题