VBA,cmdBarButton不执行整个Sub

由于某种原因,当我的菜单button“P Wave”被点击时, Sub Main()并没有完全执行。 但是,如果我用F5编辑器手动执行Sub Main() ,它应该像它应该那样工作。 就像当我点击我的“P Wave”button时,只会Call updateCell执行。 有人可以帮忙解释一下吗? 这很混乱。

下面是我在模块1 Private Sub Workbook_Open()中的代码

 Private Sub Main() 'Run all neccessary Subs to process spreadsheet Call parseCSV Call resizeColumns Call updateCell Call hideColumns End Sub Private Sub hideColumns() 'hide uneccessary columns ActiveSheet.Range("E:F,L:V,X:AW,AY:BD,BI:BK,BM:CB,CF1").EntireColumn.Hidden = True End Sub Private Sub resizeColumns() 'resize cells to fit data ActiveSheet.Columns.AutoFit ActiveSheet.Range("A1").EntireColumn.ColumnWidth = 10 ActiveSheet.Range("B1").EntireColumn.ColumnWidth = 5 ActiveSheet.Range("C1").EntireColumn.ColumnWidth = 5 ActiveSheet.Range("D1").EntireColumn.ColumnWidth = 5 ActiveSheet.Range("J1").EntireColumn.ColumnWidth = 5 ActiveSheet.Range("K1").EntireColumn.ColumnWidth = 5 ActiveSheet.Range("W1").EntireColumn.ColumnWidth = 10 ActiveSheet.Range("BE:BH").EntireColumn.ColumnWidth = 5 ActiveSheet.Range("BL1").EntireColumn.ColumnWidth = 45 ActiveSheet.Range("CG:CN").EntireColumn.ColumnWidth = 5 End Sub Private Sub updateCell() 'Cause cell updates in "Notes" colum to make line breaks appear Dim currentValue As String Dim lRow As Long With ActiveSheet() lRow = .Range("BL" & .Rows.Count).End(xlUp).Row With .Range("BL1:BL" & lRow) .WrapText = False .Value = .Value End With End With End Sub Private Sub parseCSV() 'Parse "Notes" column and return Moods/Keywords to their apropriate cells Dim CSV As String Dim fullArray() As String Dim lRow As Long Dim Keywords As String Dim Moods As String Dim i As Long lRow = ActiveSheet().Range("BL" & ActiveSheet().Rows.Count).End(xlUp).Row 'Find last row For i = 3 To lRow 'Create loop that extends through data CSV = ActiveSheet.Range("BL" & i) fullArray() = Split(CSV, vbNewLine) If UBound(fullArray()) >= 2 Then 'If then to ignore errors ActiveSheet.Range("CE" & i).Value = fullArray(2) 'enter third line ActiveSheet.Range("CD" & i).Value = fullArray(1) 'enter second line Else End If Next i End Sub Private Sub loopTest() 'Test loop for parse funcion Dim i As Long Dim lRow As Long lRow = ActiveSheet().Range("BL" & ActiveSheet().Rows.Count).End(xlUp).Row For i = 3 To lRow ActiveSheet.Range("CD" & i).Value = "testing" Next i End Sub Private Sub parseCSVTest() 'test Parse "Notes" column and return Moods/Keywords to their apropriate cells Dim CSV As String CSV = ActiveSheet.Range("BL4") Dim fullArray As Variant fullArray = Split(CSV, vbNewLine) Dim Moods As Variant Moods = Application.Index(fullArray, 1, 3) Dim Keywords As Variant Keywords = Application.Index(fullArray, 1, 2) ActiveSheet.Range("CD4").Value = Moods ActiveSheet.Range("CE4").Value = Keywords End Sub 

这是我在WorkBook中的代码

 Private Sub Workbook_Open() MsgBox "Open is working" Dim cmbBar As CommandBar Dim cmbControl As CommandBarControl Set cmbBar = Application.CommandBars("Worksheet Menu Bar") Set cmbControl = cmbBar.Controls.Add(Type:=msoControlPopup, temporary:=True) 'adds a menu item to the Menu Bar With cmbControl .Caption = "P Wave" 'names the menu item With .Controls.Add(Type:=msoControlButton) 'adds a dropdown button to the menu item .Caption = "Process Sheet" 'adds a description to the menu item .OnAction = "Main()" 'runs the specified macro .FaceId = 1098 'assigns an icon to the dropdown End With End With End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) On Error Resume Next 'in case the menu item has already been deleted Application.CommandBars("Worksheet Menu Bar").Controls("My Macros").Delete 'delete the menu item MsgBox "Close is working" End Sub 

尝试这个:

  .OnAction = "ThisWorkbook.Main" 'runs the specified macro 

要么

  .OnAction = "Main" 'runs the specified macro