How to loop

bouncey

New Member
Joined
Jan 13, 2010
Messages
35
Hi there,

I have the following code but I need for it to keep going until the last cell that has data. I have no idea how to do this could anybody point me in the right direction?
(code)Private Sub Worksheet_Change(ByVal Target As Range)

Dim myPict As Picture
Dim PictureLoc As String

If Target.Address = Range("A2").Address Then

ActiveSheet.Pictures.Delete

PictureLoc = "C:\\Users\Admin\Desktop\images" & Range("A2").Value & ".png"

With Range("B2")
Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
.RowHeight = myPict.Height
myPict.Top = .Top
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
End With

End If

End Sub (Code)
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
This is just a wild guess at what you want ????

Code:
Sub MM1()
Dim myPict As Picture, PictureLoc As String
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Pictures.Delete
For r = 2 To lr
    PictureLoc = "C:\\Users\Admin\Desktop\images" & Range("A" & r).Value & ".png"
    With Range("B" & r)
        Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
        .RowHeight = myPict.Height
        myPict.Top = .Top
        myPict.Left = .Left
        myPict.Placement = xlMoveAndSize
    End With
Next r
End Sub
 
Last edited:
Upvote 0
Thanks Michael this worked a treat. I don't suppose you know how I can control the height and width of the jpeg's?
 
Upvote 0
Maybe like this

Code:
Sub MM1()
Dim myPict As Picture, PictureLoc As String
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Pictures.Delete
For r = 2 To lr
    PictureLoc = "C:\\Users\Admin\Desktop\images" & Range("A" & r).Value & ".png"
    With Range("B" & r)
        Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
        .RowHeight = myPict.Height
        myPict.Top = .Top
        myPict.Left = .Left
        myPict.Width = 150
        myPict.Height = 150
        'myPict.Placement = xlMoveAndSize
    End With
Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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