在VBA中循环浏览工作表时select一个范围

我已经缩短了我的代码为了这个问题的目的,但我得到的错误是相同的。

当试图select每个工作表上列A中的数据并使用它的东西的单元格时,在第一个工作表之后出现错误:

Sub quickSub() Dim sh As Worksheet For Each sh In Worksheets sh.Range("A6", Range("A6").End(xlDown)).Select ''Random bits of code here where I manipulate selection on each worksheet Next End Sub 

我得到的错误是:

 "Run-time error '1004': Method 'Range' of object'_Worksheet' failed. 

尝试这个:

 sh.Activate sh.Range("A6", "A" & sh.Range("A6").End(xlDown).row).Select 

我确定范围参考是在右边的表单上结束了。我已经结束了,返回最后一个行号,并与可能不需要的列字母连接起来,但可能会使您更容易进行debugging。

更新:

增加了激活线。 select可能需要工作表激活。

UPDATE2:

这里是“正确”的方式来做到这一点,而不使用select使用此方法直接引用工作表数据INSTEAD需要工作表移动工作表。 这个最佳实践会增加您的代码性能

 Sub quickSub() Dim sh As Worksheet For Each sh In Worksheets With sh.Range("A6", "A" & sh.Range("A6").End(xlDown).row) '- lines that manipulate the 'selection' in the above with .Value = "NewValue" .font.bold = true End With ''Random bits of code here where I manipulate selection on each worksheet Next End Sub