2016-07-12 80 views
0

我有一個宏,可以在excel中打開.txt文件,有沒有辦法在打開時對它們進行分隔?注:多個文件處於打開狀態,所以類似於「|」分隔的活動工作簿,不知道如何分割。 UserInput位於我的字典中,是文件選取器。VBA,用「|」分隔excel文件打開

這是我目前有:

Sub Rec() 


    Dim wb As Workbook, fileNames As Object, errCheck As Boolean 
    Dim ws As Worksheet, wks As Worksheet, wksSummary As Worksheet 
    Dim y As Range, intRow As Long, i As Integer 



    ' Turn off screen updating and automatic calculation 
    With Application 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 
    End With 



'get user input for files to search 
Set fileNames = CreateObject("Scripting.Dictionary") 
errCheck = UserInput.FileDialogDictionary(fileNames) 
If errCheck Then 
    Exit Sub 
End If 


For Each Key In fileNames 'loop through the dictionary 



On Error Resume Next 
Set wb = Workbooks.Open(fileNames(Key)) 
If Err.Number <> 0 Then 
    Set wb = Nothing ' or set a boolean error flag 
End If 
On Error GoTo 0 ' or your custom error handler 


Next 'End of the fileNames loop 
Set fileNames = Nothing 



' Reset system settings 
With Application 
    .Calculation = xlCalculationManual 
    .ScreenUpdating = True 
    .Visible = True 
End With 

End Sub 

任何幫助,將不勝感激。

+0

在使用「數據>>文本」來打開一個管道分隔文件時記錄一個宏。這會給你一些如何處理這個問題的想法。 –

+0

它能夠在第一個文件上工作,關於如何循環它們的任何想法? – Jonnyboi

回答

1

您使用Excel 2010或更高版本以下應工作(主要變化是你的WorkBooks.Open語句)提供了循環:

Sub Rec() 
    Dim fileNames As Object, errCheck As Boolean 
    Dim ws As Worksheet, wks As Worksheet, wksSummary As Worksheet 
    Dim y As Range, intRow As Long, i As Integer 

    ' Turn off screen updating and automatic calculation 
    With Application 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 
    End With 

    'get user input for files to search 
    Set fileNames = CreateObject("Scripting.Dictionary") 
    errCheck = UserInput.FileDialogDictionary(fileNames) 
    If errCheck Then 
     Exit Sub 
    End If 

    For Each Key In fileNames 'loop through the dictionary 

     On Error Resume Next 
     Workbooks.OpenText Filename:=filenames(Key), _ 
          DataType:=xlDelimited, _ 
          Other:=True, _ 
          OtherChar:="|" 
     On Error GoTo 0 ' or your custom error handler 

    Next 'End of the fileNames loop 
    Set fileNames = Nothing 

    ' Reset system settings 
    With Application 
     .Calculation = xlCalculationManual 
     .ScreenUpdating = True 
     .Visible = True 
    End With 
End Sub 

由於如果無法打開文件,Workbooks.OpenText將顯示一條錯誤消息,您可能完全擺脫了您的錯誤處理程序(我在上述編輯版本中已完成此操作),或者可以通過設置Application來禁止OpenText的自動錯誤消息.DisplayAlerts爲False,然後繼續有你自己的錯誤處理程序。 (這取決於如果文件不存在時您想要執行的操作。)

+0

不幸的是2010年,但無論如何感謝! – Jonnyboi

+0

哎呀 - 我誤讀[微軟的文檔](https://msdn.microsoft.com/en-us/library/office/ff837097.aspx) - 它也在2010年的作品 – YowE3K

+0

我在vba編輯器中用紅色文本得到它,什麼我做錯了嗎? – Jonnyboi

1

拆分,並通過它

Sub Break_String() 
Dim WrdArray() As String 
Dim text_string As String 
text_string = "A|B|C|D" 
WrdArray() = Split(text_string, "|") 
For i = LBound(WrdArray) To UBound(WrdArray) 
    strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i) 
Next i 
MsgBox strg 
End Sub 
+0

我在哪裏改變這個按字符分割?感謝您的回覆。 – Jonnyboi

+0

補充說....... – MatthewD

+0

感謝您的更新,我必須輸入「|」 ?或者它只是分隔 – Jonnyboi