VBA新手:代码在第一个工作表上运行良好,但在其余工作表上抛出错误
Dim st As Worksheet
Set st = ActiveSheet
For Each ws In ThisWorkbook.Worksheets
ws.Activate
''--------------------------------''
'Print lables on worksheet'
''------------------------------''
ws.Cells(2, 15).value = "Greatest_increase"
ws.Cells(3, 15).value = "Greatest_decrease"
ws.Cells(4, 15).value = "Greatest total"
ws.Cells(1, 16).value = "name"
ws.Cells(1, 17).value = "Value"
'Print values on worksheet'
''------------------------------------------------------''
ws.Range("P2").value = name1
ws.Range("P3").value = name2
ws.Range("P4").value = name3
ws.Range("Q2").value = GreatIncrease
ws.Range("Q3").value = GreatDecrease
ws.Range("Q4").value = GreatTotal
`'Create a table "Growth_Table" for range("O1:Q4")'
'-----------------------------------------------------------------------''
Dim tablename As String
Dim TableExists As Boolean
'tablename = "Growth_Table"
TableExists = False
On Error GoTo Skip
If ActiveSheet.ListObjects("Growth_Table").Name = "Growth_Table" Then
TableExists = True
End If
Skip:
On Error GoTo 0
If Not TableExists And (ws.Range("O2").value = "Greatest_increase") Then
ActiveSheet.ListObjects.Add(xlSrcRange, ws.Range("O1:Q4"), , xlYes).Name = "Growth_Table"
ActiveSheet.ListObjects("Growth_Table").TableStyle = "TableStyleLight9"
Else
Exit Sub
End If
Next
st.Activate
工作表被命名为A、B、C、D。我想通过循环来运行所有工作表的代码。但是代码在sheetA上运行良好,但在sheetB上抛出'下标超出范围。是因为,“Growth Table”已经存在于工作表A上吗?有任何修复方法吗?
请帮帮忙!
1条答案
按热度按时间mzsu5hc01#
为每个工作表添加表
密码