I am very new to VBA coding and am trying to fix an error in code that someone else wrote. I put #### around the code the highlights yellow in the debugger. Any help would be highly appreciated.
Code:
Sub InsertPicture(sPicName As String)
Dim intRandom As Integer, iSamePicCount As Integer
Dim strPicName As String
Call ClearClipboard
Application.CutCopyMode = False
'Figures out the maximum number assigned to the same picture on the ActiveSheet and adds 1
For I = 1 To ActiveSheet.Pictures.count
If InStr(ActiveSheet.Pictures(I).Name, sPicName) > 0 Then
If Right(ActiveSheet.Pictures(I).Name, Len(ActiveSheet.Pictures(I).Name) - Len(sPicName)) > iSamePicCount Then ####iSamePicCount = Right(ActiveSheet.Pictures(I).Name, Len(ActiveSheet.Pictures(I).Name) - Len(sPicName))####
End If
Next I
'Copies and pastes the picture from the "Pictures" Tab
Pictures.Shapes.Item(sPicName).CopyPicture
ActiveCell.PasteSpecial
'Determines and then sets the name of the new picture as the picture name a number larger than the largest already used
sPicName = sPicName & iSamePicCount + 1
ActiveSheet.Pictures(ActiveSheet.Pictures.count).Name = sPicName
With ActiveSheet.Shapes
.Item(sPicName).Left = ActiveCell.Left + (ActiveCell.Width / 2) - (ActiveSheet.Shapes.Item(sPicName).Width / 2)
.Item(sPicName).Top = ActiveCell.Top + (ActiveCell.Height / 2) - (ActiveSheet.Shapes.Item(sPicName).Height / 2)
End With
ActiveCell.Offset(RowOffset:=1).Activate
End Sub
Last edited by a moderator: