我有一个过长的代码块,我想知道如何清理的建议,因为我清楚地以最有效的方式编写它。
理想情况下,我正在寻找有关如何定义clearcontents将作为列表运行的工作表/范围的建议,然后循环遍历每个并运行脚本。
如何最好地完成这项工作?
感谢您提供的任何帮助。
Sub ClearContents()
Sheets("Control1").Select
Cells.Select
Selection.ClearContents
Sheets("Control2").Select
Cells.Select
Selection.ClearContents
Sheets("Data").Select
Range("A8").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P1").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P2").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P3").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P4").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P5").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P7").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S2P1").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S2P4").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S2P8").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S3P1").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S4P11").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S5P2").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P8").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S1P8").Select
Range("G2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S5P10").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Sheets("S5P10").Select
Range("L2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
End Sub
答案 0 :(得分:4)
我有一些改进代码的建议:
避免使用.Select
。这会减慢你的宏。
您可以使用以下方式清除内容:
Worksheets("S5P10").Range("L2").Clear
使用完全限定名称。防爆。 Worksheets("Sheet1")
您可以编写可重复使用的程序:
Sub ClearContents(oSheet as String, oRange as String)
Worksheets(oSheet).Range(oRange).Clear
End Sub
然后,您可以使用带有数组的loop
或只提供工作表和范围来调用您的程序
Call ClearContents("S5P10", "L2")
答案 1 :(得分:0)
Sub ClearSomeSheets()
Dim ws as worksheet
For each ws in thisworkbook.worksheets
select case ws.name
case "Control1","Control2",
ws.usedrange.clearcontents
case "Data"
ws.Range(ws.Range("A8"), ws.Range("A8").End(xlToRight).End(xlDown)).ClearContents
case "S1P1","S1P8" 'etc....
ws.Range(ws.Range("A2"), ws.Range("A2").End(xlToRight).End(xlDown)).ClearContents
end select
next ws
end sub