VBA to insert values into multiple sheets, with range variable

glynn1969

Board Regular
Joined
Nov 24, 2018
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hello

I have a workbook with 8 sheets (sheet1 to sheet8)

in cell A1 on sheet I have a variable number eg 3

i want this number to be used as a column identifier eg 3 = column C, 4 = column D etc

So i then want select sheets 2 to 5 and select range C8 to M8 and enter the value "0" in that range and also for range C12 to M12 and enter the Value "0" also (where C in the above range is determined by cell A1 value above).

Any Help much appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This assumes your 8 sheets are arranged in ascending order (Sheet1,Sheet2,...,Sheet8).
Code:
Sub glynn()
Dim i As Long, Col As String
For i = 2 To 5
    With Sheets(i)
        Col = Chr(64 + .Range("A1").Value)
        Intersect(.Range(Col & ":M"), .Rows(8)).Value = 0
        Intersect(.Range(Col & ":M"), .Rows(12)).Value = 0
    End With
Next i
End Sub
 
Upvote 0
if you want only sheets 2 to 5.
And the value of the column is on sheet1, cell A1


Code:
Sub Macro11()
    col = Sheets("Sheet1").Range("A1").Value
    shs = Array("Sheet2", "Sheet3", "Sheet4", "Sheet5")
    For Each sh In Array("Sheet2", "Sheet3", "Sheet4", "Sheet5")
        Sheets(sh).Range(Sheets(sh).Cells(8, col), Sheets(sh).Cells(8, "M")).Value = 0
        Sheets(sh).Range(Sheets(sh).Cells(12, col), Sheets(sh).Cells(12, "M")).Value = 0
    Next
End Sub
 
Upvote 0
Sorry - getting an error on Col and SHS - do i need to set these as DIM, if so do you know what?

Thank you - very early on my macro learning
 
Upvote 0
Hi Joe - thats great, just one thing - this requires me to inpu a value into A1 on each sheet as opposed to it just being on A1 on sheet 1 and applied that across all sheets.

Any help on that .
Thank you
 
Upvote 0
Thanks for setting me in right direction - sorted now, cheers:)


Sub glynn()
Dim i As Long
Dim Col As Long

Sheets("Sheet1").Select
Col = Range("A1").Value
For i = 2 To 5
With Sheets(i).Select
Range(Cells(8, Col), Cells(8, "M")).Value = 0
Range(Cells(12, Col), Cells(12, "M")).Value = 0

End With
Next i
End Sub
 
Upvote 0
Well, it's a pleasure that already works for you, that's the idea that you also know the code to make changes.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

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