How to use multiple "With"

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello all,
I need help with how to use multiple with in a procedure. I want to use them to reference Worksheets. So say I have four sheets .
Code:
For i = 1 To 20
    With Sheet1
      .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "B") & ".jpg")
      .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "F") & ".jpg")
    End With
Next i

So from the above code, I want them run on sheets 2 3 and 4. But I don't want separate procedures for them.
Regards
Kelly
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Depending on how your sheets are named, maybe:

Code:
For s = 2 to 4
    For i = 1 To 20
        With Sheets(s)
          .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "B") & ".jpg")
          .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "F") & ".jpg")
        End With
    Next i
Next s

or

Code:
For Each s in Array("Sheet2", "Sheet3", "Sheet4")
    For i = 1 To 20
        With Sheets(s)
          .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "B") & ".jpg")
          .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "F") & ".jpg")
        End With
    Next i
Next s
 
Last edited:
Upvote 0
Assuming that there are only four sheets
Code:
[FONT=Verdana]Dim ws as worksheet[/FONT]
[FONT=Verdana]For each ws in worksheets[/FONT]
[FONT=Verdana]With ws[/FONT]
For i = 1 To 20
      .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "" & .Cells(10 * i - 8, "B") & ".jpg")
      .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "" & .Cells(10 * i - 8, "F") & ".jpg")
Next i
End With
Next ws
 
Last edited:
Upvote 0
Depending on how your sheets are named, maybe:

Code:
For s = 2 to 4
    For i = 1 To 20
        With Sheets(s)
          .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "B") & ".jpg")
          .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "F") & ".jpg")
        End With
    Next i
Next s

or

Code:
For Each s in Array("Sheet2", "Sheet3", "Sheet4")
    For i = 1 To 20
        With Sheets(s)
          .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "B") & ".jpg")
          .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "\" & .Cells(10 * i - 8, "F") & ".jpg")
        End With
    Next i
Next s

The for s = 2 To 4 , how do I Dim the s ?
Will the code use the sheet name or the code name?

I want it to use the codename . I ran it and nothing happened. I know I am doing something wrongly. But can't figure it yet.
 
Upvote 0
Assuming that there are only four sheets
Code:
[FONT=Verdana]Dim ws as worksheet[/FONT]
[FONT=Verdana]For each ws in worksheets[/FONT]
[FONT=Verdana]With ws[/FONT]
For i = 1 To 20
      .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "" & .Cells(10 * i - 8, "B") & ".jpg")
      .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "" & .Cells(10 * i - 8, "F") & ".jpg")
Next i
End With
Next ws

There are more than 4 sheets but I want to run it on only 4. They follow in order say 10, 11, 12, 13 .
 
Upvote 0
The for s = 2 To 4 , how do I Dim the s ?
Will the code use the sheet name or the code name?

I want it to use the codename . I ran it and nothing happened. I know I am doing something wrongly. But can't figure it yet.

Okay I turned off my error handler and I had the error: run-time error "9"
Subscript out of range
 
Upvote 0
For the first example:

Code:
Dim s as Long

For the second example:

Code:
Dim s as Variant

The first example uses the number of the sheet. The number is usually not a good way to do things, since as you rename or move sheets, the number doesn't really relate to the actual sheet name, and can get confusing. I'd recommend the second version, just put the sheet names you want in the Array.


But even that can cause problems, if you rename your sheets. Then you'd get a run-time error "9", when VBA can't find the sheet name. To get around that, you can mark the sheets you want to process by putting (for example) an "X" in cell Z1 on the sheets you want it to run on. Then the code would look something like:

Code:
Sub test1()
Dim ws As Worksheet, i As Long
    
    For Each ws In Worksheets
        If ws.Range("Z1") = "X" Then
            With ws
                For i = 1 To 20
                    .OLEObjects("Image" & i).Object.Picture = LoadPicture(fPath & "" & .Cells(10 * i - 8, "B") & ".jpg")
                    .OLEObjects("Image" & i + 10).Object.Picture = LoadPicture(fPath & "" & .Cells(10 * i - 8, "F") & ".jpg")
                Next i
            End With
        End If
    Next ws
    
End Sub
(with thanks to Alan for using some of his code)

I'm not aware of a way to use codenames in a loop, and even if you could, you'd probably run into some of the same issues I mentioned. Hope this helps.
 
Upvote 0
Oh okay. This last code is very cool. Thanks for it. Now I am cool To move on.
Kelly
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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