我如何参考工作表来循环使用不同的工作表,并在每个工作表上执行相同的function?

我是VBA新手,正在努力学习它。 我已经写了这个函数来试图循环同一工作簿中的一些工作表,并添加一列来更新星期标记为'ww'的基于左边的单元格。

我在For循环中遇到问题,它应该通过定义的sh1循环到sh3。 如何使用sheet来声明表单循环(“sh”&i)对For循环不起作用?

Sheets("sh" & i).Activate部分是我收到debugging错误的地方。 “运行时错误:9下标超出范围”

我很大程度上不确定自己在做什么,并做了很多实验。 另一方面,有没有什么好的在线学习课程可以用来提高我对VBA的认识?

 Function updateWWColumn() Set sh1 = ActiveWorkbook.Sheets("Data_WE") Set sh2 = ActiveWorkbook.Sheets("Data_E") Set sh3 = ActiveWorkbook.Sheets("Data_PC") 'placeholder to contain the week number in integer form from the cell on the right. Dim placeholder As Integer Dim i As Integer For i = 1 To 3 Sheets("sh" & i).Activate ' Run time error: 9 subscript out of range ActiveSheet.Columns("U:U").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Worksheets("sh" & i).Columns("P:P").Delete Shift:=xlToLeft placeholder = Right(Worksheets("sh" & i).Range("S83"), 2) Worksheets("sh" & i).Range("T83").Value = "WW" & placeholder + 1 Next i End Function 

不要使用三个variables来存储您的图纸参考 – 使用三个存储您的特定图纸的项目的数组。

所以我在这里完成的是把sh1,sh2和sh3改成一个叫sh的数组。 那么无论你在哪里使用WorkSheets("sh"&i)你都可以使用sh(i)

 Function updateWWColumn() Dim sh(3) As Worksheet Set sh(1) = ActiveWorkbook.Sheets("Data_WE") Set sh(2) = ActiveWorkbook.Sheets("Data_E") Set sh(3) = ActiveWorkbook.Sheets("Data_PC") 'placeholder to contain the week number in integer form from the cell on the right. Dim placeholder As Interger Dim i As Integer For i = 1 To 3 sh(i).Activate ActiveSheet.Columns("U:U"). _ Insert Shift:=xlToRight, _ CopyOrigin:=xlFormatFromLeftOrAbove sh(i).Columns("P:P"). _ Delete Shift:=xlToLeft placeholder = Right(sh(i).Range("S83"), 2) sh(i).Range("T83").Value = "WW" & placeholder + 1 Next i End Function 

您可以使用一组工作表:

 Public sh(1 To 3) As Worksheet Sub updateWWColumn() ' Only use Function when you are returning a value Set sh(1) = ActiveWorkbook.Worksheets("Data_WE") Set sh(2) = ActiveWorkbook.Worksheets("Data_E") Set sh(3) = ActiveWorkbook.Worksheets("Data_PC") 'placeholder to contain the week number in integer form from the cell on the right. Dim placeholder As Integer ' Note: Integer, not Interger Dim i As Integer For i = 1 To 3 With sh(i) .Columns("U:U"). _ Insert Shift:=xlToRight, _ CopyOrigin:=xlFormatFromLeftOrAbove .Columns("P:P"). _ Delete Shift:=xlToLeft placeholder = Right(.Range("S83"), 2) .Range("T83").Value = "WW" & placeholder + 1 End With Next i End Sub 

或者,您可以使用其名称遍历每个工作表:

 Sub updateWWColumn() ' Only use Function when you are returning a value Dim ws As Worksheet 'placeholder to contain the week number in integer form from the cell on the right. Dim placeholder As Integer ' Note: Integer, not Interger Dim i As Integer For Each ws In ActiveWorkbook.Worksheets(Array("Data_WE", "Data_E", "Data_PC")) With ws .Columns("U:U"). _ Insert Shift:=xlToRight, _ CopyOrigin:=xlFormatFromLeftOrAbove .Columns("P:P"). _ Delete Shift:=xlToLeft placeholder = Right(.Range("S83"), 2) .Range("T83").Value = "WW" & placeholder + 1 End With Next End Sub 

其他张贴的答案是好的。 或者,您可以遍历工作簿中的每个工作表:

 Dim sht As Worksheet For Each sht In ActiveWorkbook.Worksheets sht.Activate ' etc... Next sht 

或者,如果您只想要影响以名称Data_开头的Data_ ,则可以embeddedif语句:

 Dim sht As Worksheet For Each sht In ActiveWorkbook.Worksheets If Left(sht.Name, 5) = "Data_" Then sht.Activate ' etc... End If Next sht