我尝试使用循环清除某些工作表的内容,但无法清除单元格中的数据。
这是我开始使用的(工作)静态代码:
Set oWS2 = Sheets("WEBSHOP-NL")
Set oWS3 = Sheets("WEBSHOP-FR")
Set oWS4 = Sheets("WEBSHOP-EN")
~~>
oWS2.UsedRange.Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
oWS3.UsedRange.Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
oWS4.UsedRange.Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
这就是我已经得到的:消息框输出oWS2~> oWS3~> oWS4 但是......它没有清除内容。
' LoopThroughSheets
Dim Shts As Variant
Dim Sht As Variant
Shts = Array("oWS2", "oWS3", "oWS4")
For Each Sht In Shts 'Loop Sheets Code goes here:
Sht.UsedRange.Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
MsgBox "Sht = " & Sht, vbInformation
Next Sht
我在这里俯瞰什么?谢谢你回复。
答案 0 :(得分:1)
您的数组是字符串数组,而不是工作表对象。因此,您必须根据其名称指定您感兴趣的工作表。就像您使用Sheets对象在原始代码Set oWS2 = Sheets("WEBSHOP-NL")
中所做的那样。
Dim Shts As Variant
Dim Sht As Variant
Shts = Array("WEBSHOP-NL", "WEBSHOP-FR", "WEBSHOP-EN")
For Each Sht In Shts 'Loop Sheets Code goes here:
Sheets(Sht).UsedRange.Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
MsgBox "Sht = " & Sht, vbInformation
Next Sht
或者你可以坚持使用你现有的代码,但是删除数组中变量的引号,但这似乎很麻烦,设置所有这些变量,将它们粘贴到数组中,然后迭代数组:
Set oWS2 = Sheets("WEBSHOP-NL")
Set oWS3 = Sheets("WEBSHOP-FR")
Set oWS4 = Sheets("WEBSHOP-EN")
'LoopThroughSheets
Dim Shts As Variant
Dim Sht As Variant
Shts = Array(oWS2, oWS3, oWS4)
For Each Sht In Shts 'Loop Sheets Code goes here:
Sht.UsedRange.Offset(1).SpecialCells(xlCellTypeConstants).ClearContents
MsgBox "Sht = " & Sht, vbInformation
Next Sht