Macro to Clear Data in Cell D37 listed on all sheets Listed in Array

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,605
Office Version
  1. 2021
Platform
  1. Windows
I have written code to clear data in Cell D37 on all sheets Listed in the Array

However when running the macro, the data in D37 is not being cleared

It would be appreciated if someone would kindly amend my code


Code:
 Sub Clear_Ref_Advices()

Dim arr, Wks

arr = Array(" advice BR1 GRT", " advice BR1 GRT", " advice BR3 GRT", " advice BR4 GRT" _

, " advice BR5 GRT", " advice BR6 GRT")

For Each Wks In arr

Range("D37").ClearContents



Next Wks

 End Sub




 
I'm assuming the double-up on BR1 is a typo, and that there is supposed to be a space before each sheet name?

VBA Code:
Sub Clear_D37()
    Dim Wks As Worksheet
    
    For Each Wks In Worksheets(Array(" advice BR1 GRT", " advice BR2 GRT", " advice BR3 GRT", " advice BR4 GRT", _
    " advice BR5 GRT", " advice BR6 GRT"))
        Wks.Range("D37").ClearContents
    Next Wks
    
End Sub
 
Upvote 0
Solution
Your code would be clearing the same cell. You need to specify which sheet the D37 should be cleared on.
 
Upvote 0
many Thanks for the help Kevin. Code works perfectly
 
Upvote 0
Here is how I would do it:
Modify Sheet names
VBA Code:
Sub My_Array_Work()
'Modified 6/13/2022  1:56:12 AM  EDT
Application.ScreenUpdating = False
Dim Del As Variant
Dim i As Long
Dim ans As Long
Del = Array("Alpha", "Charlie", "Delta")
ans = UBound(Del)

For i = 0 To ans
    Sheets(Del(i)).Range("D37").ClearContents
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0

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