简体   繁体   中英

Excel VBA ClearContents error

Im trying to run macro which should clear contents from sheets AA, BB and CC and then move into sheet MENU but its highliting me an error on line 4. Please see the code below.

Sub clear_sheets()

Snames = Split(AA, BB, CC)

For Count = 0 To UBound(Snames)

    Sheets(Snames(Count)).Range("A3:C3").End(xlDown).ClearContents

Next
    Sheets("MENU").Select
Optimise (False)

End Sub

I got a little bit different approach posted on a different forum. Which would run more efficiently, I mean which one will will be putting less stress on a processing?

    Sub clear_sheets()

Optimise (True)

Snames = Split("AA, BB, CC", ", ")

For count = 0 To UBound(Snames)
MyRange = Range("A3:C3", Range("A3:C3").End(xlDown)).Address

    ThisWorkbook.Sheets(Snames(count)).Range(MyRange).ClearContents

Next
    Sheets("MENU").Select
Optimise (False)

End Sub

Update I understand this is not a code writing website but I would like to ask you if you could have a look at my code below.

Sub distribute_dsp_data_9()

Sheets("raw_data_1_9").Visible = True

Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
    :="DTTD"
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets("DTTD").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
    :="FDTL"
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets("FDTL").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
    :="FULL"
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Sheets("FUL ON").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Sheets("raw_data_1_9").Select
ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2

Sheets("raw_data_1_9").Visible = False

End Sub

Try this. I've declared your variables properly and also assigned values to your array using Array() rather than Split() both are fine, but Array() is more flexible

Sub clear_sheets()
    Dim sheetNames As Variant
    Dim count As Integer
    sheetNames = Array("AA", "BB", "CC")

    For Count = 0 To UBound(Snames)
        With Sheets(sheetNames(Count))
            Range(.Range("A3"), .Range("C3").End(xlDown)).ClearContents
        End With
    Next

    Sheets("MENU").Select
    Optimise (False)
End Sub

Also, it's better to use ThisWorkbook.Sheets() rather than just Sheets() if your code refers to the same workbook you are writing your VBA in. If you don't do this then VBA will assume you are referring to the sheets in whichever workbook is active when you run the code - that's not usually a good thing.

Update I've changed the code to delete what I think you might want deleted?

采用:

Snames = Split("AA", "BB", "CC")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM