Creating a For Each Cell loop inside another For Each Cell Loop to repeat code on different files which are listed in sheet

Yorke

New Member
Joined
Nov 29, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a For Each Cell loop, inside another For Each Cell Loop. I have not been able to find answers for this anywhere, is this even possible? The outside loop is supposed to Pull data from different files which are listed in Data sheet, cells Z2 to Z4, I then want the inside for loops to work on the data (I have already written loads of other code inside which I need to apply to loads of excel files). I am not sure this outside loop even works as I haven't been able to run due to the error 'For Control Variable already in use'.

For Each Cell In ThisWorkbook.Worksheets("Data").Range("Z2:Z4")
Set OpenBook = "C:\Users\Yorke\VBA\" & Cell.Value & ".xls"

.............. 'Other code

For Each Cell In ThisWorkbook.Worksheets("Data").Range("J2:J") 'This is just an example, I have lots more other for loops working too
If Cell.Value = 0 Then Cell.Value = Cell.Offset(0, 1)
Next Cell

.............. 'Other code

Next Cell
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
VBA Code:
Dim Cell As Range, Cell2 As Range

For Each Cell In ThisWorkbook.Worksheets("Data").Range("Z2:Z4")
    Set OpenBook = "C:\Users\Yorke\VBA\" & Cell.Value & ".xls"

    .............. 'Other code

    For Each Cell2 In ThisWorkbook.Worksheets("Data").Range("J2:J") 'This is just an example, I have lots more other for loops working too
        If Cell2.Value = 0 Then Cell2.Value = Cell2.Offset(0, 1)
    Next

.............. 'Other code

Next
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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