VBA,Delimit excel文件" |"开场时

时间:2016-07-12 19:54:24

标签: excel vba excel-vba

我有一个在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

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分: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  

答案 1 :(得分:1)

如果您使用的是Excel 2010或更高版本,则以下内容应该有效(主要更改是您的WorkBooks.Open语句):

Java

如果Workbooks.OpenText将显示错误消息,如果它无法打开文件,你可以完全摆脱你的错误处理程序(我在上面编辑的版本中已经这样做了),或者你可以抑制OpenText的自动错误将Application.DisplayAlerts设置为False,然后继续拥有自己的错误处理程序。 (这取决于如果文件不存在,您想要做什么。)