我在“ ThisWorkbook”模块中设置了一个宏,以运行“ BeforeSave”。我还有两个其他的宏,我还需要运行“ BeforeSave”。我可以在此模块中添加其他宏吗?
我已经在“标准”模块部分中创建了宏,并且可以通过选择“运行”按钮来使用它们。我试图将“宏/模块名称”添加到“ BeforeSave”宏的底部,该宏什么也没有做,只是给了我错误。
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call HideRows
Call DivAdminApproval
Call ProjNumbrReq
End Sub
Public Sub HideRows()
'When a row begins with X in Travel Expense Codes worksheet, hide the row
Const beginRow As Long = 3
Const endRow As Long = 38
Const chkCol As Long = 14
Dim rowCnt As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Travel Expense Codes")
For rowCnt = endRow To beginRow Step -1
With ws.Cells(rowCnt, chkCol)
.EntireRow.Hidden = (.Value = "X")
End With
Next rowCnt
End Sub
Public Sub ProjNumbrReq()
'Call ProjNumbrReq
With Worksheets("Travel Expense Voucher")
For Each myCell In .Range("U15:U45")
If myCell.Value > 0 And .Cells(myCell.row, "N") = "" Then
MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbCritical, "Important:"
Cancel = True
Exit Sub
End If
Next myCell
End Sub
Public Sub DivAdminApproval()
'Call DivAdminApproval
With Worksheets("Travel Expense Voucher")
If Worksheets("Travel Expense Voucher").Cells("F5") = 2 Then
For Each myCell In .Range("O15:O45")
If myCell.Value = 0.58 Then
MsgBox "You have selected reimbursement at the 'HIGH' mileage rate ($.58/mile). To receive reimbursement at this rate, Division Administrator Approval is Required.", vbCritical, "Important:"
Exit Sub
End If
Next myCell
End Sub
ProjNumbrReq和DivAdminApproval宏已在End Sub之前列出,希望它们能起作用。但是,它们没有运行。
答案 0 :(得分:1)
遵循这些原则,如果您的宏不在“ ThisWorkbook”模块中,请确保将其定义为公共(而不是私有):
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call sMacro1
Call sMacro2
Call sMacro3
End Sub
Private Sub sMacro1()
'do something
End Sub
Private Sub sMacro2()
'do something
End Sub
Private Sub sMacro3()
'do something
End Sub