打开工作簿时,在一个工作表中自动运行宏

时间:2013-08-23 09:45:59

标签: vba

我正在尝试在打开工作簿时自动运行宏。我在ThisWorkbook选项卡中使用privatesub命令。

然而,当我关闭Excel文件并再次打开它时,宏似乎也在另一张纸上运行,导致循环引用错误。我如何解决这个问题,因此它只在一张纸上运行(“封面纸”)。是否将宏放在实际的图纸模块中?

Private Sub Workbook_Open()    
With Sheets("Cover Sheet")
With Range("B21")
        .Formula = "=COUNTIFS('Design Risk Scoring Sheet'!$AN$12:$AN$" & Sheets("Design Risk Scoring Sheet").Cells(Rows.count, "AN").End(xlUp).Row & ",""<""&B20, 'Design Risk Scoring Sheet'!$B$12:$B$" & Sheets("Design Risk Scoring Sheet").Cells(Rows.count, "AN").End(xlUp).Row & ", """" )"
        .AutoFill Destination:=Range("B21:AF21"), Type:=xlFillDefault
End With

With Range("B22")
        .Formula = "=COUNTIFS('Design Risk Scoring Sheet'!$BF$12:$BF$" & Sheets("Design Risk Scoring Sheet").Cells(Rows.count, "AN").End(xlUp).Row & ",""<""&B20, 'Design Risk Scoring Sheet'!$B$12:$B$" & Sheets("Design Risk Scoring Sheet").Cells(Rows.count, "AN").End(xlUp).Row & ", """" )"
        .AutoFill Destination:=Range("B22:AF22"), Type:=xlFillDefault
End With
End With

1 个答案:

答案 0 :(得分:3)

您只错过dots两次但重要的点数。您的第二个和第三个With...End With对象引用应以dots开头:

With .Range("B21")
    '...your code here
End With

With .Range("B22")
    '...your code here
End with