个人工作簿宏未运行

时间:2018-10-31 05:09:45

标签: excel vba excel-vba

我记录了一个宏,并按照Microsoft's instructions将其保存到我的个人工作簿中。然后,我将此宏添加到我的快速访问工具栏中。但是,当我单击它时,什么也没有发生。奇怪的是,如果我将完全相同的代码分配给Button Click,则一切运行良好。

代码现在可能已经不相关了,但是我只是将其包括进来,以防它可能变得相关。

以下是代码:

Option Explicit

Public Sub test(metric, greenNum, orangeNum, ws)
Dim metricName As Range                                                                             
Dim rowsCounter As Long                                                                             ' Rows loop counter
Dim numOfRows As Long                                                                               ' Number of rows
Dim numOfColumns As Long                                                                            ' Number of columns
Dim baseline As Double                                                                              ' Baseline to compute against
Dim newCellValue As Double                                                                          ' Each cell after baseline to make computations on
Dim columnsCounter As Long                                                                          ' Columns loop counter

Dim greenCell As Double
Dim orangeCell As Double

Dim regEx As New RegExp
Dim strPattern As String: strPattern = "[^0-9.]"                                                    
Dim strReplace As String: strReplace = ""
With regEx
        .Global = True
        .Pattern = strPattern
End With

numOfRows = Range("A" & Rows.Count).End(xlUp).Row                                                   
numOfColumns = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column                                  

For rowsCounter = 1 To numOfRows
    Set metricName = Range("A" & rowsCounter)                                                       

    If metricName.Value = metric Then

        For columnsCounter = 3 To numOfColumns

             If IsNumeric(ws.Cells(rowsCounter, 2)) Then                                            
                baseline = Math.Abs(ws.Cells(rowsCounter, 2))                                       
             Else
                If ws.Cells(rowsCounter, 2) <> "NA" And Not IsEmpty(ws.Cells(rowsCounter, 2)) Then
                    baseline = Math.Abs(regEx.Replace(ws.Cells(rowsCounter, 2), strReplace))
                End If
             End If

             If IsNumeric(ws.Cells(rowsCounter, columnsCounter)) Then                                ' Check if cell value is number
                 newCellValue = Math.Abs(ws.Cells(rowsCounter, columnsCounter))                     
             Else
                 If ws.Cells(rowsCounter, columnsCounter) <> "NA" And Not IsEmpty(ws.Cells(rowsCounter, columnsCounter)) Then
                     newCellValue = Math.Abs(regEx.Replace(ws.Cells(rowsCounter, columnsCounter), strReplace))
                 End If
             End If

             greenCell = baseline * greenNum
             orangeCell = baseline * orangeNum

             If newCellValue >= 0 And newCellValue < greenCell And Not IsEmpty(ws.Cells(rowsCounter, columnsCounter)) Then
                 ws.Cells(rowsCounter, columnsCounter).Interior.Color = RGB(0, 255, 0)
             ElseIf newCellValue >= greenCell And newCellValue <= orangeCell Then
                 ws.Cells(rowsCounter, columnsCounter).Interior.Color = RGB(255, 165, 0)
             ElseIf IsEmpty(ws.Cells(rowsCounter, columnsCounter)) Then
                 ws.Cells(rowsCounter, columnsCounter).Interior.Color = RGB(255, 255, 255)
             Else
                 ws.Cells(rowsCounter, columnsCounter).Interior.Color = RGB(255, 0, 0)
             End If
        Next columnsCounter
    End If
Next rowsCounter
End Sub

然后,我在另一个子代码中调用此代码:

Public Sub runAll()
Application.EnableEvents = False
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    Const a = "test"
    Const b = 1.05
    Const c = 1.2
    Call test(a, b, c, ws)
Next ws
Application.EnableEvents = True
End Sub

当我尝试运行此宏时,什么都没有发生-没有错误,没有输出。

但是,当我将此代码复制到给定Workbook的模块并在按钮上单击时调用test()时-一切正常。

如何在不使用按钮的情况下运行保存在“个人工作簿”中的宏?

0 个答案:

没有答案