簡體   English   中英

VBA,打開時用“ |”分隔excel文件

[英]VBA, Delimit excel files by “|” upon opening

我有一個宏可以在excel中打開.txt文件,是否可以在打開時對它們進行定界? 注意:已打開多個文件,因此諸如活動工作簿之類的內容由“ |”分割,但不確定如何分割。 UserInput在我的詞典中,並且是文件選擇器。

這是我目前擁有的:

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

任何幫助,將不勝感激。

拆分並循環遍歷

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  

假設您使用的是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如果無法打開文件將顯示錯誤消息,您可能完全擺脫了錯誤處理程序(我在上面的編輯版本中已經這樣做),或者可以通過設置來抑制OpenText的自動錯誤消息Application.DisplayAlerts設置為False,然后繼續擁有自己的錯誤處理程序。 (這取決於文件不存在時要執行的操作。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM