用多个工作表声明?
我正在为当前工作簿中的某些工作表重复一组操作
For Each ws In Worksheets Select Case ws.Name Case "2015", "2016", "Rate", "Month" 'code here End Select Next ws
这似乎非常低效XD是否有可能做类似的事情
With ws1, ws2, ws3 'code here end with
我没有那么多的表,所以循环只需要2秒,但必须有一个更快捷/优雅的方式?
工作表是包含当前工作簿中所有工作表的集合。
为了简化你的代码,你可以创build你自己的集合,只需要处理表单,然后用它来代替。 那么你将不需要selectcase语句来限制循环处理哪些表单。
但是,collectionscollections可能比以前做的更多。 尽pipe这是OO的方式。
所以在一个模块中,你可能会这样做:
Sub aa() Dim colSheets As Collection Set colSheets = New Collection colSheets.Add Sheet1 ' here I use the object name from the VBA "(Name)" property colSheets.Add Worksheets("MyWorksheetName") colSheets.Add Worksheets("SummarySheet") Dim sht As Worksheet For Each sht In colSheets Debug.Print sht.Name Next sht Set colSheets = Nothing End Sub