Dear All,
Am calling a function from a sub by passing a string (picture path) and a range variable (Cell address).
The function then inserts the picture and pasts it over range
Issue: The function gives 1004 error on <range>.pastespecial command but not every time. and pressing F5 in debug mode make it run again till next error.
Kindly guide if there is some coding error or Logical error
Application:
In B Column, there are some image path. say from B2 to B6.
select B1 to make it active cell and call macro which will loop till B6.
Pictures will be pasted on Column A for each respective row.
Am calling a function from a sub by passing a string (picture path) and a range variable (Cell address).
The function then inserts the picture and pasts it over range
Issue: The function gives 1004 error on <range>.pastespecial command but not every time. and pressing F5 in debug mode make it run again till next error.
Kindly guide if there is some coding error or Logical error
Application:
In B Column, there are some image path. say from B2 to B6.
select B1 to make it active cell and call macro which will loop till B6.
Pictures will be pasted on Column A for each respective row.
VBA Code:
Sub Insert_image_on_left()
Dim i As Long
Dim intRowCount As Integer
Dim Image As String
Dim X As Integer
Dim Y As String
Dim p As Object
Dim col_selected As Integer
Application.ScreenUpdating = False
If ActiveCell.Text = "" Then Exit Sub
image_col = ActiveCell.Column - 1
last_row = ActiveCell.End(xlDown).Row
ActiveCell.Offset(0, -1).ColumnWidth = 50
ActiveCell.Offset(0, -1).RowHeight = 130
For i = ActiveCell.Row To last_row
Cells(i, image_col).RowHeight = 150
Call InsertPictureinCell(Cells(i, image_col + 1).Text, ActiveSheet.Cells(i, image_col))
Next i
Application.ScreenUpdating = True
End Sub
Sub InsertPictureinCell(PictureFileName As String, TargetCell As Range)
' inserts a picture at the top left position of TargetCell
Dim picobject As Object, t As Double, l As Double, w As Double, h As Double
Dim PicWtoHRatio As Single
Dim CellWtoHRatio As Single
If PictureFileName = "" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
Set picobject = thisworkbook.ActiveSheet.Pictures.Insert(PictureFileName)
picobject.Cut
TargetCell.PasteSpecial '<< This line gives random error of 1004 method of range class failed.
Set picobject = Nothing
End Sub