VBA for each

acharsanthosh

New Member
Joined
Dec 20, 2017
Messages
9
Hello Folks,

I do have a report with around 45 sheets and A table data in the last sheet Naming "Other Data"

Other data table has all the sheet names and am running for each based on this sheet names.

Now I want to run the 'for each' to update C4 value in each sheet based on the table in "Other Data" in the column C has data for all 45 sheets

For example sheet1 C4 should be C2 of "Other data", sheet2 C4 should be C3 of "Other data",sheet3 C4 should be C4 of "Other data"........like this for all the sheets.

Below is the Code which I prepared and it is updating the C2 value in all the table

Sub For each
Dim Rng As Range, Dn As Range, R As Range, ShtRng As Range
With Sheets("Other Data")
Set Rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))

End With
For Each Dn In Rng
With Sheets(Dn.Value)
Set ShtRng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
For Each R In ShtRng

R.Range("C5") = Sheets("Other Data").Range("C4")


Next R

End With
Next Dn
End Sub


Kindly Help !!!
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
Please explain the layout of your table more.
So column C obviously has the value that you want to update.
But does column A or column B have the sheet name that should be updated?
If not, how do you know which sheet gets updated with which value?
 

acharsanthosh

New Member
Joined
Dec 20, 2017
Messages
9
Sorry for confusion.

Under "Other Data" I do have the list of Sheet names in Column A and am running for each based on this sheet names
 

acharsanthosh

New Member
Joined
Dec 20, 2017
Messages
9
Basically I do have sheet names like Sheet1, Sheet2, Sheet3 so on till 45...And all the sheet names listed in Other Data Tab ....Other data tab table has "Principal Value" in column C for all sheet names...now I want to update "Principal Value" in each respective sheets based on the Table in "other Data " Tab
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
Try this:
Code:
Sub PopulateMacro()

    Dim lastRow As Long
    Dim myRow As Long
    Dim dstSheet As String
    
    Application.ScreenUpdating = False
    
'   Find last row in column A on "Other Data" sheet
    lastRow = Sheets("Other Data").Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows, starting in row 2
    For myRow = 2 To lastRow
        dstSheet = Sheets("Other Data").Cells(myRow, "A")
        Sheets(dstSheet).Range("C4") = Sheets("Other Data").Cells(myRow, "C")
    Next myRow
        
    Application.ScreenUpdating = True
    
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,556
Messages
5,596,820
Members
414,104
Latest member
imamalidadashzada

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
Top