简体   繁体   中英

Excel VBA ClearContents in both ranges and single cells at click of button

I am trying to create a macro in a workbook that clears contents from both ranges and single cells at the click of a button; I have written the below code that works for all of the ranges, but errors out for the single cells. Do you know what word I should use instead of "Range" to clear contents from single cells?

Sub Clearcells()
'Updateby Extendoffice 20161008
Range("C1:C2").ClearContents
Range("I11:I24").ClearContents
Range("I26:I31").ClearContents
Range("I33:I38").ClearContents
Range("I40:I44").ClearContents
Range("I46:I49").ClearContents
Range("I51:I54").ClearContents
Range("156:I58").ClearContents
Range("I60:I62").ClearContents
Range("I64:I66").ClearContents
Range("I68:I69").ClearContents
Range("I71:I72").ClearContents
Range("I74:I75").ClearContents
Range("I77:I78").ClearContents
Range("I80:I81").ClearContents
Range("I83:I84").ClearContents
Range("I86:I87").ClearContents
Range("I89:I90").ClearContents
Range("I92:I93").ClearContents
Range("I95:I96").ClearContents
Range("I98:I99").ClearContents
Range("I101:I102").ClearContents
Range("I104:I105").ClearContents
Range("I107:I108").ClearContents
Range("I110").ClearContents
Range("I112").ClearContents
Range("I114").ClearContents
Range("I116").ClearContents
Range("I118").ClearContents
Range("I120").ClearContents
Range("I122").ClearContents
Range("I124").ClearContents
Range("I126").ClearContents
Range("I128").ClearContents
Range("I130").ClearContents
Range("I132").ClearContents
Range("I134").ClearContents
Range("I136").ClearContents
Range("I138").ClearContents
Range("I140").ClearContents
Range("I142").ClearContents
Range("I144").ClearContents
End Sub

There is no difference in the syntax by which to address a range of a single or multiple cells. I think BigBen pretty much nailed it. Nevertheless I suggest to use code as given below.

Sub ClearCells()

    Dim Def     As String                   ' range Definition
    Dim Sp()    As String
    Dim i       As Integer
    
    Def = "C1:C2,I11:I24,I26:I31,I33:I38,I40:I44,I46:I49,I51:I54,I56:I58,I60:I62," & _
          "I64:I66,I68:I69,I71:I72,I74:I75,I77:I78,I80:I81,I83:I84,I86:I87,I89:I90," & _
          "I92:I93,I95:I96,I98:I99,I101:I102,I104:I105,I107:I108,I110,I112,I114,I116," & _
          "I118,I120,I122,I124,I126,I128,I130,I132,I134,I136,I138,I140,I142,I144"
    Sp = Split(Def, ",")
    For i = 0 To UBound(Sp)
'        Debug.Print Range(Sp(i)).Address
        Range(Sp(i)).ClearContents
    Next i
End Sub

Debug.Print Range(Sp(i)).Address is intended to help you find typos like BigBen pointed out because the code will fail when it comes to an invalid address.

Even simpler code like Range("C1:C2,I11:I24,I26:I31,I33:I38,I40:I44,I46:I49").ClearContents also works but your list is too long. Excel rejects it and I presume that the same would be true if you tried to create a 'Union` range by joining them all. The above suggestion is reliable and steady even if expanded.

Talking of expansion, the code could be sped up by disabling screen updating while it runs. Precede the loop with Application.ScreenUpdating = False and repeat the same line with True following the loop.

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