为什么我的 Excel 宏从热键运行时会失败,而从宏功能区运行时却可以工作?

为什么我的 Excel 宏从热键运行时会失败,而从宏功能区运行时却可以工作?

我真的很困惑我创建的用于解析空格分隔文件的宏到底是怎么回事。

只要我从对话框中选择宏功能区和宏,宏就会成功运行。

但是,如果我尝试使用热键运行它,则发生的一切是 A 列突出显示,然后 B 列突出显示,然后它跳回到突出显示 A 列。

我尝试了几种不同的热键组合,当前是Ctrl+ Shift+ Y

以下是宏代码,如能得到任何帮助我将不胜感激。

Sub DLEligibleParse()
    '
    ' DLeligibleparse Macro
    ' this utilizes the eligible list end positionseff 4-22-12
    '
    '
    Columns("B:B").Select
    Selection.NumberFormat = "0000000000000"
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(13, 1), Array(43, 1), Array(45, 1), Array(80, 1), _
        Array(115, 1), Array(150, 1), Array(185, 1), Array(220, 1), Array(222, 1), Array(231, 1), _
        Array(266, 1), Array(301, 1), Array(336, 1), Array(371, 1), Array(373, 1), Array(382, 1), _
        Array(385, 1), Array(402, 1), Array(455, 1), Array(490, 1), Array(525, 1), Array(527, 1), _
        Array(536, 1), Array(539, 1), Array(549, 1), Array(559, 1), Array(564, 1), Array(579, 1), _
        Array(581, 1), Array(596, 1), Array(598, 1), Array(610, 1), Array(622, 1), Array(640, 1), _
        Array(655, 1), Array(670, 1), Array(687, 1), Array(702, 1), Array(717, 1), Array(734, 1), _
        Array(749, 1), Array(764, 1), Array(781, 1), Array(796, 1), Array(811, 1), Array(828, 1), _
        Array(843, 1), Array(858, 1), Array(875, 1), Array(890, 1), Array(905, 1), Array(922, 1), _
        Array(937, 1), Array(952, 1), Array(969, 1), Array(984, 1), Array(999, 1), Array(1016, 1), _
        Array(1031, 1), Array(1046, 1), Array(1063, 1), Array(1078, 1), Array(1093, 1), Array( _
        1110, 1), Array(1125, 1), Array(1140, 1), Array(1157, 1), Array(1172, 1), Array(1187, 1), _
        Array(1204, 1), Array(1212, 1), Array(1224, 1), Array(1225, 1), Array(1226, 1), Array( _
        1227, 1), Array(1228, 1)), TrailingMinusNumbers:=True
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("A1").Select
End Sub

答案1

我建议删除“select”语句。它们会以不可预测的方式扰乱代码执行。试试这个。

Sub DLEligibleParse()
    '
    ' DLeligibleparse Macro
    ' this utilizes the eligible list end positionseff 4-22-12
    '
    '
    Columns("B:B").NumberFormat = "0000000000000"
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(13, 1), Array(43, 1), Array(45, 1), Array(80, 1), _
        Array(115, 1), Array(150, 1), Array(185, 1), Array(220, 1), Array(222, 1), Array(231, 1), _
        Array(266, 1), Array(301, 1), Array(336, 1), Array(371, 1), Array(373, 1), Array(382, 1), _
        Array(385, 1), Array(402, 1), Array(455, 1), Array(490, 1), Array(525, 1), Array(527, 1), _
        Array(536, 1), Array(539, 1), Array(549, 1), Array(559, 1), Array(564, 1), Array(579, 1), _
        Array(581, 1), Array(596, 1), Array(598, 1), Array(610, 1), Array(622, 1), Array(640, 1), _
        Array(655, 1), Array(670, 1), Array(687, 1), Array(702, 1), Array(717, 1), Array(734, 1), _
        Array(749, 1), Array(764, 1), Array(781, 1), Array(796, 1), Array(811, 1), Array(828, 1), _
        Array(843, 1), Array(858, 1), Array(875, 1), Array(890, 1), Array(905, 1), Array(922, 1), _
        Array(937, 1), Array(952, 1), Array(969, 1), Array(984, 1), Array(999, 1), Array(1016, 1), _
        Array(1031, 1), Array(1046, 1), Array(1063, 1), Array(1078, 1), Array(1093, 1), Array( _
        1110, 1), Array(1125, 1), Array(1140, 1), Array(1157, 1), Array(1172, 1), Array(1187, 1), _
        Array(1204, 1), Array(1212, 1), Array(1224, 1), Array(1225, 1), Array(1226, 1), Array( _
        1227, 1), Array(1228, 1)), TrailingMinusNumbers:=True
    Cells.EntireColumn.AutoFit
End Sub

相关内容