VBA (help with picture pasting formula)

alekun86

New Member
Joined
Mar 28, 2023
Messages
19
Platform
  1. Windows
Hi Everybody,

Can I ask you help with the following VBA code?
It works fine. However when the formula find an empty cell it stops. I would like it to keep on going despite the empty cells here and there and stop when the are no more cells with text.

Thank you for you help



Sub Picture()
Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long, cPic

lastrow = Worksheets("sheet1").Range("B1").CurrentRegion.Rows.Count
x = 2
For x = 2 To lastrow
Set pastehere = Cells(x, 1)
pasterow = pastehere.Row
Cells(pasterow, 1).Select 'This is where picture will be inserted
pictname = Cells(x, 2) 'This is the picture name
Set cPic = ActiveSheet.Shapes.AddPicture("C:\Users\90009672\Desktop\baba\" & pictname & ".jpg", False, True, PosizLeft, PositTop, True, True)
' ActiveSheet.Pictures.Insert("C:\Users\90009672\Desktop\baba\" & pictname & ".jpg").Select 'Path to where pictures are stored
With cPic
.LockAspectRatio = msoFalse
.Height = 80#
.Width = 80#
.Left = Cells(pasterow, 1).Left + Cells(pasterow, 1).Width / 2 - .Width / 2
.Top = Cells(pasterow, 1).Top + Cells(pasterow, 1).Height / 2 - .Height / 2

End With
Next x
Set cPic = Nothing
End Sub
 
at which point of the code should I insert this command?
here is the code

Option Explicit

Sub InsertPicture()
Dim ws As Worksheet
Dim LastRow As Long
Dim x As Long
Dim cPic As Shape

'~~> Set this to the relevant worksheet
'~~> Use Code Name if possible
Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
'~~> Find the last row. Fully qualify the Range and the Rows Object
'~~> by adding a DOT before it
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row

'~~> Loop through the row. No need to select the cell where the paste
'~~> is going to happen. You are handling that later
For x = 3 To LastRow
'~~> Check if the cell in B is not empty
If Len(Trim(.Cells(x, 2).Value2)) <> 0 Then
'~~> Insert the shape
Set cPic = .Shapes.AddPicture("C:\Users\90009672\Desktop\FW23\" & _
.Cells(x, 2).Value2 & _
".jpg", False, True, 10, 10, 10, 10)

'~~> Customize the shape values
With cPic
.LockAspectRatio = msoFalse

.Height = 100
.Width = 100


.Left = ws.Cells(x, 1).Left + (ws.Cells(x, 1).MergeArea.Width - .Width) / 2
.Top = ws.Cells(x, 1).Top + ws.Cells(x, 1).MergeArea.Height / 2 - .Height / 2


End With
End If
Next x
End With
End Sub
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I can give you the answer but, how about you tell me where would you insert the code? :)

I would recommend trying it and come back if you are still stuck?
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,104
Members
453,021
Latest member
Justyna P

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