I have created a column of ActiveX command buttons using a macro, this works well. I have then created a macro which imports pictures down the column to the right of the buttons. However, I want to allocate one of the buttons to each picture import so I do not have to see all photos at one time. The ActiveX buttons are currently dormant.
The code I am using for the picture import is:
Sub InsertPic()
Dim pic As String 'file path of pic
Dim myPicture As Picture 'embedded pic
Dim rng As Range 'range over which we will iterate
Dim cl As Range 'iterator
Set rng = Range("G2:G6")
For Each cl In rng
pic = cl.Offset(0, -1)
Set myPicture = ActiveSheet.Pictures.Insert(pic)
'
With myPicture
.ShapeRange.LockAspectRatio = msoFalse
.Width = cl.Width
.Height = cl.Height
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
End With
'
Next
End Sub
The button creation code is:
Sub createButtons()
Dim theButton As OLEObject
Dim rngRange As Range
Dim i As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set rngRange = Sheets(1).Range("$B2")
For i = 0 To 4
If rngRange.Offset(i, 0).Value <> "" Then
With rngRange.Offset(i, 1)
Set theButton = ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Left:=.Left, _
Top:=.Top, _
Height:=.Height, _
Width:=.Width)
theButton.Name = "cmd" & rngRange.Offset(i, 0).Value
theButton.Object.Caption = rngRange.Offset(i, 0).Value
End With
End If
Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
How would I go about changing the code above to get the pictures assigned to the buttons?
Bookmarks