2013-08-23 73 views
1

我試圖在打開工作簿時自動運行宏。我在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 

回答

3

您只錯過了dots兩次,但重要的點。你的第二個和第三With...End With對象引用應該以這種方式與dots開始:

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

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

謝謝我試一下,看看它是否工作。我已將它放在「封面」上,現在看起來可行。 – AAli

相關問題