VBA clear contents of multiple ranges.

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
910
Office Version
  1. 365
Platform
  1. Windows
I was hoping someone could help me with the below code. I need to clear multiple ranges as shown below but get a runtime error on the large Range: 1004 Method 'Range' of object' Global failed.

Any help fixing this would be greatly appreciated.

Thank you

VBA Code:
Sub Clear1()
'
' Clear1 Macro
 
 Range("A2,A4, A6:B10, A14:B14, A16:B16, A20:B20, A22:B22, A24:B24, A27:B27, A29:B29, A31:B31, A34:B34, A36:B36, A38:B38, A41:B41, A43:B43, A45:B45, A48:B48, A50:B50, A52:B52, A55:B55, A57:B57, A59:B59, A62:B62, A64:B64, A66:B66, A69:B69, A71:B71, A73:B73, A76:B76, A78:B78, A80:B80, A83:B83, A85:B85, A87:B87, A90:B90, A92:B92, A94:B94, A97:B97, A99:B99, A101:B101").ClearContents

    Dim o As Object
    For Each o In ActiveSheet.OLEObjects
        If InStr(1, o.Name, "CheckBox") > 0 Then
            o.Object.Value = False
        End If
    Next

    Range("A1").Select
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Apparently it doesn't support that many ranges. I separated it into 2 lines:

VBA Code:
Sub Clear1()
'
' Clear1 Macro
 
  Range("A2, A4, A6:B10,  A14:B14, A16:B16, A20:B20," & _
        "A22:B22, A24:B24, A27:B27, A29:B29, A31:B31," & _
        "A34:B34, A36:B36, A38:B38, A41:B41, A43:B43," & _
        "A45:B45, A48:B48, A50:B50, A52:B52, A55:B55").ClearContents
  Range("A57:B57, A59:B59, A62:B62, A64:B64, A66:B66," & _
        "A69:B69, A71:B71, A73:B73, A76:B76, A78:B78," & _
        "A80:B80, A83:B83, A85:B85, A87:B87, A90:B90," & _
        "A92:B92, A94:B94, A97:B97, A99:B99, A101:B101").ClearContents
  
  Dim o As Object
  For Each o In ActiveSheet.OLEObjects
     If InStr(1, o.Name, "CheckBox") > 0 Then
         o.Object.Value = False
     End If
  Next
  
  Range("A1").Select
End Sub
 
Upvote 0
Solution
It looks like it is a limit on how many individual ranges you can put into RANGE(). This worked below:

VBA Code:
Range("A2,A4, A6:B10, A14:B14, A16:B16, A20:B20, A22:B22, A24:B24, A27:B27, A29:B29, A31:B31, A34:B34, A36:B36, A38:B38, A41:B41, A43:B43, A45:B45, A48:B48, A50:B50, A52:B52, A55:B55, A57:B57, A59:B59, A62:B62, A64:B64, A66:B66, A69:B69, A71:B71, A73:B73").ClearContents

Edit: What Dante said.
 
Upvote 0
To clarify, it's not the number of ranges, it's the number of characters in the text passed to Range, as Micron said.

You can also use Union to get round it:

VBA Code:
Union(Range("A2,A4,A6:B10,A14:B14,A16:B16,A20:B20,A22:B22,A24:B24,A27:B27,A29:B29,A31:B31,A34:B34,A36:B36,A38:B38,A41:B41,A43:B43,A45:B45,A48:B48,A50:B50,A52:B52,A55:B55"), _
Range("A57:B57,A59:B59,A62:B62,A64:B64,A66:B66,A69:B69,A71:B71,A73:B73,A76:B76,A78:B78,A80:B80,A83:B83,A85:B85,A87:B87,A90:B90,A92:B92,A94:B94,A97:B97,A99:B99,A101:B101")).ClearContents
 
Upvote 1
Apparently it doesn't support that many ranges. I separated it into 2 lines:

VBA Code:
Sub Clear1()
'
' Clear1 Macro
 
  Range("A2, A4, A6:B10,  A14:B14, A16:B16, A20:B20," & _
        "A22:B22, A24:B24, A27:B27, A29:B29, A31:B31," & _
        "A34:B34, A36:B36, A38:B38, A41:B41, A43:B43," & _
        "A45:B45, A48:B48, A50:B50, A52:B52, A55:B55").ClearContents
  Range("A57:B57, A59:B59, A62:B62, A64:B64, A66:B66," & _
        "A69:B69, A71:B71, A73:B73, A76:B76, A78:B78," & _
        "A80:B80, A83:B83, A85:B85, A87:B87, A90:B90," & _
        "A92:B92, A94:B94, A97:B97, A99:B99, A101:B101").ClearContents
 
  Dim o As Object
  For Each o In ActiveSheet.OLEObjects
     If InStr(1, o.Name, "CheckBox") > 0 Then
         o.Object.Value = False
     End If
  Next
 
  Range("A1").Select
End Sub

I did not know about the limit.
This worked perfect, Thank you!
 
Upvote 1

Forum statistics

Threads
1,221,531
Messages
6,160,357
Members
451,642
Latest member
mirofa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top