我试图在工作簿的工作表子集中插入两行,方法是在工作表中定义一个范围并循环遍历该范围内的工作表,但代码只能在活动工作表上运行。
Sub StatePIPData()
Dim sheet_name As Range
For Each sheet_name In Sheets("WS").Range("A:A")
If sheet_name.Value = "" Then
Exit For
Else
'Insert 2 rows for 2011 and 2012's data
Range("A14").EntireRow.Insert
Cells(14, 1) = Cells(15, 1) + 1
Range("A14").EntireRow.Insert
Cells(14, 1) = Cells(15, 1) + 1
End If
Next sheet_name
End Sub
关于如何让这个循环工作的任何想法?
答案 0 :(得分:0)
这是你需要的吗?
Sub StatePIPData()
Dim sheet_name As Range
Dim sh As Worksheet
For Each sheet_name In ThisWorkbook.Worksheets("WS").Range("A:A")
If sheet_name.Value = "" Then
Exit For
Else
On Error Resume Next
Set sh = ThisWorkbook.Worksheets(sheet_name)
On Error GoTo 0
If Not sh Is Nothing Then
With sh
'Insert 2 rows for 2011 and 2012's data
.Range("A14").EntireRow.Insert
.Cells(14, 1) = .Cells(15, 1) + 1
.Range("A14").EntireRow.Insert
.Cells(14, 1) = .Cells(15, 1) + 1
End With
End If
End If
Next sheet_name
End Sub