1

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

但是,当我关闭 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
4

1 回答 1

3

你只错过了dots两次但很重要的点。您的第二个和第三个With...End With对象引用应该以dots这种方式开始:

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

With .Range("B22")
    '...your code here
End with
于 2013-08-23T10:41:52.857 回答