宏可以单步执行但不使用热键

时间:2019-07-10 14:52:03

标签: excel vba

我有一个相对简单的宏。它接收用户选择的文本文件,并使用固定宽度的定界符将其切成始终相同的列。我的问题是,当我运行宏时,所选文件将打开,但是它不执行文本到列。

只要您单步执行宏,它将执行此操作。当您连续两次在同一文件上使用宏时,它也可以工作。

Sub Historical()

With Application.FileDialog(msoFileDialogFilePicker)
        'Only one file
        .AllowMultiSelect = False
        'Add filters
        .Filters.Add "All", "*.*"
        'Show the dialog box
        .Show

        'Store in fullpath variable
        fullpath = .SelectedItems.Item(1)
End With

Dim WrkBk As Workbook
Dim WrkSht As Worksheet
Dim sheetname As String

Set WrkBk = Workbooks.Open(fullpath)

'Code stops here. The file will open but nothing below happens. I tried adding a wait.
Application.Wait (Now + TimeValue("0:00:02"))

Columns("A:A").Select

Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 9), Array(14, 1), Array(17, 9), Array(18, 1), Array(23, 9), _
        Array(24, 1), Array(30, 1), Array(62, 1), Array(72, 1), Array(84, 1), Array(94, 1), Array( _
        118, 1)), TrailingMinusNumbers:=True

End Sub

1 个答案:

答案 0 :(得分:0)

弄清楚了。这是因为我在宏的热键中使用了Shift键,这导致了Workbooks.Open命令的问题。