代码不会进入下一张表格,只更改活动表格

我目前正在写一个简单的macros(见下文)。 我打算让macros更改我的工作簿中的所有工作表。 然而,它只改变了我所在的活动工作表中的单元格,我不得不手动移动到下一个工作表并运行macros来改变当前的单元格,但是它非常繁琐,特别是当我有大约125个工作表要更新时。

Sub worksheetloop() Dim sh As Worksheet With ThisWorkbook For Each sh In ActiveWorkbook.Worksheets Range("$F$5") = "toothwidth" ' Key contact Range("$F$8") = "x,y,z" ' Core team Range("$J$5") = "bleh" 'date Range("$L$5") = "9/12/" 'Updated Range("$M$5") = "A" ' Revision Date Next sh End With End Sub 

您需要在您的For Each sh In ActiveWorkbook.Worksheets sh工作表对象中限定您使用的Range For Each sh In ActiveWorkbook.Worksheets循环中。

试试下面的代码:

 Dim sh As Worksheet For Each sh In ThisWorkbook.Worksheets With sh .Range("$F$5") = "toothwidth" ' Key contact .Range("$F$8") = "x,y,z" ' Core team .Range("$J$5") = "bleh" 'date .Range("$L$5") = "9/12/" 'Updated .Range("$M$5") = "A" ' Revision Date\ End With Next sh