VBA code to align a picture to the RIGHT

shophoney

Active Member
Joined
Jun 16, 2014
Messages
286
Hi,

Below is my VBA code to grab a jpg picture from the web. Currently it inserts it CENTRED, how can I justify it to the RIGHT?

Sub URLPictureInsert()
Call delete_picture

Dim Pshp As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
Set rng = ActiveSheet.Range("R6:R30")
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 - 17
Set xRg = Cells(cell.Row, xCol)
With Pshp
.LockAspectRatio = msoFalse
.Width = 15
.Height = 15
.Top = xRg.Top + (xRg.Height - .Height) / 2
.Left = xRg.Left + (xRg.Width - .Width) / 2
End With
lab:
Set Pshp = Nothing
Range("R6").Select
Next
Application.ScreenUpdating = True
End Sub
Public Sub delete_picture()
For Each shp In ActiveSheet.Shapes
If Not Intersect(shp.TopLeftCell, [A6:A30]) Is Nothing Then shp.Delete
Next
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try replacing this line
VBA Code:
.Left = xRg.Left + (xRg.Width - .Width) / 2

with this line
VBA Code:
.Left = xRg.Left + xRg.Width - .Width
 
Upvote 0
I made some changes to your code. You should not use the On Error Resume Next statement, because if an error occurs, you will not know what the error is, or in which line of code.
It is also not good practice to use the GoTo statement.

Try this:

VBA Code:
Sub URLPictureInsert()
  Dim Pshp As Shape
  Dim xRg As Range, rng As Range, cell As Range
  Dim xCol As Long
  
  Application.ScreenUpdating = False
  
  Call delete_picture
  Set rng = ActiveSheet.Range("R6:R30")
  For Each cell In rng
    If cell.Value <> "" Then
      If Dir(cell.Value) <> "" Then
        ActiveSheet.Pictures.Insert(cell.Value).Select
        Set Pshp = Selection.ShapeRange.Item(1)
        If Not Pshp Is Nothing Then
          xCol = cell.Column - 17
          Set xRg = Cells(cell.Row, xCol)
          With Pshp
            .LockAspectRatio = msoFalse
            .Width = 15
            .Height = 15
            .Top = xRg.Top + (xRg.Height - .Height) / 2
            .Left = xRg.Left + (xRg.Width - .Width)
          End With
        End If
        Set Pshp = Nothing
      End If
    End If
  Next
  Range("R6").Select
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,128
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