Image VBA

Deelof

New Member
Joined
Aug 30, 2023
Messages
46
Office Version
  1. 2019
Platform
  1. Windows
I'm using the following VBA to add images into rows but they are not aligning, any ideas how to improve so they do?
URL's are held in column Z.

VBA Code:
Sub URLPictureInsert()

    Dim Pshp As Shape
    Dim xRg As Range
    Dim xCol As Long
    On Error Resume Next
    Application.ScreenUpdating = False
    Set Rng = ActiveSheet.Range("Z4:Z219")
    For Each cell In Rng
        filenam = cell
        ActiveSheet.Pictures.Insert(filenam).Select
        Set Pshp = Selection.ShapeRange.Item(1)
        If Pshp Is Nothing Then GoTo lab
        xCol = cell.Column + 1
        Set xRg = Cells(cell.Row, xCol)
        With Pshp
            .LockAspectRatio = msoFalse
            .Width = 90
            .Height = 110
            .Top = xRg.Top + (xRg.Height - .Height) / 2
            .Left = xRg.Left + (xRg.Width - .Width) / 2
        End With
lab:
    Set Pshp = Nothing
    Range("A2").Select
    Next
    Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Check if the following works better for you.

VBA Code:
Sub URLPictureInsert()
  Dim Pshp As Shape
  Dim xRg As Range, rng As Range, cell As Range
  Dim xCol As Long
  Dim filenam As String
  
  On Error Resume Next
  Application.ScreenUpdating = False
  Set rng = ActiveSheet.Range("Z4:Z219")
  For Each cell In rng
    filenam = cell
    If filenam <> "" And Dir(filenam) <> "" Then
      ActiveSheet.Pictures.Insert(filenam).Select
      Set Pshp = Selection.ShapeRange.Item(1)
      If Pshp Is Nothing Then GoTo lab
      xCol = cell.Column + 1
      Set xRg = Cells(cell.Row, xCol)
      With Pshp
        .LockAspectRatio = msoFalse
        .Width = xRg.Width - 2      '90
        .Height = xRg.Height - 2    '110
        .Top = xRg.Top + 1          '+ (xRg.Height - .Height) / 2
        .Left = xRg.Left + 1        '+ (xRg.Width - .Width) / 2
      End With
    End If
lab:
    Set Pshp = Nothing
    Range("A2").Select
  Next
  Application.ScreenUpdating = True
End Sub

:)
 
Upvote 0
Check if the following works better for you.

VBA Code:
Sub URLPictureInsert()
  Dim Pshp As Shape
  Dim xRg As Range, rng As Range, cell As Range
  Dim xCol As Long
  Dim filenam As String
 
  On Error Resume Next
  Application.ScreenUpdating = False
  Set rng = ActiveSheet.Range("Z4:Z219")
  For Each cell In rng
    filenam = cell
    If filenam <> "" And Dir(filenam) <> "" Then
      ActiveSheet.Pictures.Insert(filenam).Select
      Set Pshp = Selection.ShapeRange.Item(1)
      If Pshp Is Nothing Then GoTo lab
      xCol = cell.Column + 1
      Set xRg = Cells(cell.Row, xCol)
      With Pshp
        .LockAspectRatio = msoFalse
        .Width = xRg.Width - 2      '90
        .Height = xRg.Height - 2    '110
        .Top = xRg.Top + 1          '+ (xRg.Height - .Height) / 2
        .Left = xRg.Left + 1        '+ (xRg.Width - .Width) / 2
      End With
    End If
lab:
    Set Pshp = Nothing
    Range("A2").Select
  Next
  Application.ScreenUpdating = True
End Sub

:)
Thanks but do I need to make any adjustments to the script as it's still not aligning the images?
 
Upvote 0
it's still not aligning the images

Align to what?

Could you post an image of how the images look after running the macro in post #2.

And another image of how you would like the result.


This is my example with 3 images, the 3 images are in column AA, inside the cell in each row and "aligned".

1698765297288.png


;)
 
Upvote 0
Here's an example of how they're not aligning in the row

1698765841974.png
 
Upvote 0
Could you resend as it looks to be the same as post #1
It looks like the same code, but I already made changes so it really works well.
Just delete your macro and try the updated macro from post #2.
 
Upvote 0
You could at least give me the benefit of the doubt and test my macro.

However, I like to help 😇 , I put my macro back in.
VBA Code:
Sub URLPictureInsert_V1()
  Dim Pshp As Shape
  Dim xRg As Range, rng As Range, cell As Range
  Dim xCol As Long
  Dim filenam As String
  
  On Error Resume Next
  Application.ScreenUpdating = False
  Set rng = ActiveSheet.Range("Z4:Z219")
  For Each cell In rng
    filenam = cell
    If filenam <> "" And Dir(filenam) <> "" Then
      ActiveSheet.Pictures.Insert(filenam).Select
      Set Pshp = Selection.ShapeRange.Item(1)
      If Pshp Is Nothing Then GoTo lab
      xCol = cell.Column + 1
      Set xRg = Cells(cell.Row, xCol)
      With Pshp
        .LockAspectRatio = msoFalse
        .Width = xRg.Width - 2
        .Height = xRg.Height - 2
        .Top = xRg.Top + 1
        .Left = xRg.Left + 1
      End With
    End If
lab:
    Set Pshp = Nothing
    Range("A2").Select
  Next
  Application.ScreenUpdating = True
End Sub

🫡
 
Upvote 0
You could at least give me the benefit of the doubt and test my macro.

However, I like to help 😇 , I put my macro back in.
VBA Code:
Sub URLPictureInsert_V1()
  Dim Pshp As Shape
  Dim xRg As Range, rng As Range, cell As Range
  Dim xCol As Long
  Dim filenam As String
 
  On Error Resume Next
  Application.ScreenUpdating = False
  Set rng = ActiveSheet.Range("Z4:Z219")
  For Each cell In rng
    filenam = cell
    If filenam <> "" And Dir(filenam) <> "" Then
      ActiveSheet.Pictures.Insert(filenam).Select
      Set Pshp = Selection.ShapeRange.Item(1)
      If Pshp Is Nothing Then GoTo lab
      xCol = cell.Column + 1
      Set xRg = Cells(cell.Row, xCol)
      With Pshp
        .LockAspectRatio = msoFalse
        .Width = xRg.Width - 2
        .Height = xRg.Height - 2
        .Top = xRg.Top + 1
        .Left = xRg.Left + 1
      End With
    End If
lab:
    Set Pshp = Nothing
    Range("A2").Select
  Next
  Application.ScreenUpdating = True
End Sub

🫡
I do appreciate your assistance and I did try both and just tried the recent posted one again but the images are still not row aligning

1698772684538.png

1698772729564.png


These all need to shift down so they sit in 212 - 214 centrally
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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