Simple loop through column

SerenityNetworks

Board Regular
Joined
Aug 13, 2009
Messages
131
Office Version
  1. 365
Platform
  1. Windows
Sorry, it's been too long since I've fooled with VBA. I've forgotten how to loop.

I can the simple code below to display a picture in Cell B1 from the URL in C1. But the URLs in Column-C actually start at C9 and go to an indefinite number of rows. In one worksheet there may be 10 rows with URLs in Column-C. In another worksheet there may be 100 rows with URLs in Column-C. There will be no blank cells between C9 and the end of Column-C. How do I modify the macro below to loop through so each row B9 and down is populated with a picture from the URL on the same row in Column-C?

Thanks in advance,
Andrew

Code:
Sub Test()
    Dim Pic As Picture
    Application.ScreenUpdating = False
    With ActiveSheet.Range("C1")
        Set Pic = .Parent.Pictures.Insert(.Value)
        With .Offset(, -1)
            Pic.Top = .Top
            Pic.Left = .Left
            Pic.Height = .Height
            Pic.Width = .Width
        End With
    End With
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Assuming your code is good, try this:
Code:
Sub Test()

    Dim Pic As Picture
    Dim lastRow As Long
    Dim myRow As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column C
    lastRow = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Loop through column C
    For myRow = 1 To lastRow
        With Range("C" & lastRow)
            Set Pic = .Parent.Pictures.Insert(.Value)
            With .Offset(, -1)
                Pic.Top = .Top
                Pic.Left = .Left
                Pic.Height = .Height
                Pic.Width = .Width
            End With
        End With
    Next myRow
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Perfect. (Well, almost. I had to change
With Range("C" & lastRow) to With Range("C" & myRow), but otherwise it was perfect.

Thank you!
Andrew
 
Upvote 0
Perfect. (Well, almost. I had to change
With Range("C" & lastRow) to With Range("C" & myRow), but otherwise it was perfect.
Ah, good catch!
Just seeing if you were paying attention!;)
 
Upvote 0
Code:
[COLOR=#333333]For myRow = 1 To lastRow

[/COLOR]
what is myrow over here ? I am a beginner in VBA
 
Upvote 0
You are setting myRow to by the current iteration of the loop.

So, if lastRow was 4, you have 4 loops.
The first time through, myRow is 1, the second time it is 2, the third time it is 3, and the last time it is 4.
 
Upvote 0
Thanks Joe. Appreciate your support in helping me learn
 
Upvote 0

Forum statistics

Threads
1,216,562
Messages
6,131,422
Members
449,651
Latest member
Jacobs22

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