0

I want to create a button that opens an image.

This code works (if there's some text/numbers already inside column A, before you run it), but it will put the button on the left hand side of the B column.

How can I adjust the code so that the button goes to the right hand side of the A column instead?

Sub Macro1()
    Application.ScreenUpdating = False
    
        Dim cell As Range
        For Each cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
            If Not IsEmpty(cell) Then
                Dim ol As OLEObject
                Set ol = ActiveSheet.OLEObjects.Add( _
                                                    Filename:="C:\screenshot.png", _
                                                    Link:=False, _
                                                    DisplayAsIcon:=True)
                With ol
                    .Top = cell.Offset(0, 1).Top
                    .Left = cell.Offset(0, 1).Left
                    .ShapeRange.LockAspectRatio = msoFalse
                    .Height = 13
                    .Width = 10
                End With
            End If
        Next
    Application.ScreenUpdating = True
End Sub
Jase
  • 1,025
  • 1
  • 9
  • 34
  • Move (modify) `.Top = cell.Top` and `.Left = cell.Left + cell.Width - .Width` to the bottom of the `With statement`? – VBasic2008 May 16 '21 at 05:31
  • @VBasic2008 The `.Width = 10` statement must be execute first for correct calculation in `.Left = cell.Left + cell.Width - .Width` – Алексей Р May 16 '21 at 06:20
  • 1
    @Алексей Р : This is what I meant with *to the bottom of...*. I guess I should have said *after the line `.Width = 10` in the `With statement`*. Sorry. – VBasic2008 May 16 '21 at 06:26

0 Answers0