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
 
I see!
now code starts working but stops at the empty cells with this message.
says the definition is not correct
 

Attachments

  • コメント 2023-03-28 174730.jpg
    コメント 2023-03-28 174730.jpg
    57.8 KB · Views: 6
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I see!
now code starts working but stops at the empty cells with this message.
says the definition is not correct

I believe the second button is "Debug" in Chinese? Click on that and tell me which line is it highlighting? And what is the exact error message
 
Upvote 0
Ah I understood. The error is because there is no image path in col B. You can handle it by adding an extra check If Len(Trim(.Cells(x, 2).Value2)) <> 0 Then as shown below.

VBA 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 = 2 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\baba\" & _
                                              .Cells(x, 2).Value2 & _
                                              ".jpg", False, True, 10, 10, 10, 10)
                                                         
                '~~> Customize the shape values
                With cPic
                    .LockAspectRatio = msoFalse
                   
                    .Height = 80
                    .Width = 80
                   
                    .Left = ws.Cells(x, 1).Left + ws.Cells(x, 1).Width / 2 - .Width / 2
                    .Top = ws.Cells(x, 1).Top + ws.Cells(x, 1).Height / 2 - .Height / 2
                End With
            End If
        Next x
    End With
End Sub
 
Upvote 0
Glad it's resolved. BTW, I would also recommend using proper error handling so that it can catch other errors. For example, what if the image path has a typo 😉
 
Upvote 0
Dear Siddhart,

Sorry to bother you. The code works fine but the cell were the pic is pasted is now merged (the one were the pic name is not merged)
I would like the pic to be centered in the merged cell.

Thank you
 

Attachments

  • コメント 2023-04-07 143102.jpg
    コメント 2023-04-07 143102.jpg
    48.2 KB · Views: 6
Upvote 0
Try this (UNTESTED - Doing this from memory).

VBA Code:
.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
 
Upvote 0
perfect!
just another question..is there a way to paste the pictures so that when some cell are hidden the pics become also hidden?

thank you
 
Upvote 0
Check if the row is hidden and then hide the picture.

VBA Code:
If ws.Cells(x, 1).EntireRow.Hidden Then
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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