excelhelp6323
New Member
- Joined
- Mar 3, 2015
- Messages
- 2
Hello,
A macro that I use to insert pictures of several items into cells at once is no longer working now that I am using Excel 2013. I have virtually no experience with coding, so any help is much appreciated. Here is the code:
Option Explicit
Sub AddItemPhoto()
Dim photoFolder
'Dim dbs As Database
'Dim rst As Recordset
'Dim wrkjet As Workspace
Dim ItemCol, PhotoCol, ItemColNo, PhotoColNo
Dim wksRange As Range
Dim firstRow, lastRow, I
Dim photoHight As Single, photoWidth As Single, rowH As Single, colW As Single
Dim photoRatioH As Single, photoRatioW As Single, photoRatio As Single
Dim strSQL
'TO Do: Change to the folder the photo files saved
photoFolder = "P:\images\IMAGES\"
ItemCol = InputBox("Column Index of ItemNo (ex. A,B,..):")
If ItemCol = "" Then Exit Sub
PhotoCol = InputBox("Column Index of Photo (ex. A,B,..):")
If PhotoCol = "" Then Exit Sub
firstRow = Int(InputBox("First Row Number:", , 2))
If firstRow = "" Then Exit Sub
lastRow = Int(InputBox("Last Row Number:", , ActiveCell.SpecialCells(xlLastCell).Row))
If lastRow = "" Then Exit Sub
ItemColNo = Asc(UCase(ItemCol)) - 64
PhotoColNo = Asc(UCase(PhotoCol)) - 64
Columns(PhotoCol).ColumnWidth = 20
colW = 84
For I = firstRow To lastRow
If InStr(Cells(I, ItemCol).Value, "/") > 0 Or InStr(Cells(I, ItemCol).Value, ":") > 0 Or InStr(Cells(I, ItemCol).Value, ".") > 0 Or InStr(Cells(I, ItemCol).Value, "*") > 0 Then
Else
If Dir(photoFolder & Trim(Cells(I, ItemColNo).Value) & ".jpg") <> "" Then
If Rows(I).RowHeight < 93.75 Then
Rows(I).RowHeight = 93.75
End If
rowH = Rows(I).RowHeight - 5
Cells(I, PhotoCol).Select
InsertPictureInRange photoFolder & Trim(Cells(I, ItemColNo).Value) & ".jpg", Cells(I, PhotoCol)
End If
End If
Next I
End Sub
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Height = h
.Width = w
If p.Height > h Then p.Height = h
End With
Set p = Nothing
End Sub
Sub DeletePictures()
'
' DeletePictures Macro
Dim Sh As Shape
Dim iSheetCount As Integer
Dim iSheet As Integer
Dim intOption
intOption = MsgBox("Are you sure you want to delete all of pictures in this sheet?", vbYesNo + vbDefaultButton2)
If intOption = vbNo Then
Exit Sub
End If
iSheetCount = ActiveWorkbook.Worksheets.Count
For iSheet = 1 To iSheetCount
With Worksheets(iSheet)
For Each Sh In .Shapes
If Sh.Type = msoPicture Or Sh.Type = 11 Then Sh.Delete
Next Sh
End With
Next iSheet
'
'
End Sub
A macro that I use to insert pictures of several items into cells at once is no longer working now that I am using Excel 2013. I have virtually no experience with coding, so any help is much appreciated. Here is the code:
Option Explicit
Sub AddItemPhoto()
Dim photoFolder
'Dim dbs As Database
'Dim rst As Recordset
'Dim wrkjet As Workspace
Dim ItemCol, PhotoCol, ItemColNo, PhotoColNo
Dim wksRange As Range
Dim firstRow, lastRow, I
Dim photoHight As Single, photoWidth As Single, rowH As Single, colW As Single
Dim photoRatioH As Single, photoRatioW As Single, photoRatio As Single
Dim strSQL
'TO Do: Change to the folder the photo files saved
photoFolder = "P:\images\IMAGES\"
ItemCol = InputBox("Column Index of ItemNo (ex. A,B,..):")
If ItemCol = "" Then Exit Sub
PhotoCol = InputBox("Column Index of Photo (ex. A,B,..):")
If PhotoCol = "" Then Exit Sub
firstRow = Int(InputBox("First Row Number:", , 2))
If firstRow = "" Then Exit Sub
lastRow = Int(InputBox("Last Row Number:", , ActiveCell.SpecialCells(xlLastCell).Row))
If lastRow = "" Then Exit Sub
ItemColNo = Asc(UCase(ItemCol)) - 64
PhotoColNo = Asc(UCase(PhotoCol)) - 64
Columns(PhotoCol).ColumnWidth = 20
colW = 84
For I = firstRow To lastRow
If InStr(Cells(I, ItemCol).Value, "/") > 0 Or InStr(Cells(I, ItemCol).Value, ":") > 0 Or InStr(Cells(I, ItemCol).Value, ".") > 0 Or InStr(Cells(I, ItemCol).Value, "*") > 0 Then
Else
If Dir(photoFolder & Trim(Cells(I, ItemColNo).Value) & ".jpg") <> "" Then
If Rows(I).RowHeight < 93.75 Then
Rows(I).RowHeight = 93.75
End If
rowH = Rows(I).RowHeight - 5
Cells(I, PhotoCol).Select
InsertPictureInRange photoFolder & Trim(Cells(I, ItemColNo).Value) & ".jpg", Cells(I, PhotoCol)
End If
End If
Next I
End Sub
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
' inserts a picture and resizes it to fit the TargetCells range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = ActiveSheet.Pictures.Insert(PictureFileName)
' determine positions
With TargetCells
t = .Top
l = .Left
w = .Offset(0, .Columns.Count).Left - .Left
h = .Offset(.Rows.Count, 0).Top - .Top
End With
' position picture
With p
.Top = t
.Left = l
.Height = h
.Width = w
If p.Height > h Then p.Height = h
End With
Set p = Nothing
End Sub
Sub DeletePictures()
'
' DeletePictures Macro
Dim Sh As Shape
Dim iSheetCount As Integer
Dim iSheet As Integer
Dim intOption
intOption = MsgBox("Are you sure you want to delete all of pictures in this sheet?", vbYesNo + vbDefaultButton2)
If intOption = vbNo Then
Exit Sub
End If
iSheetCount = ActiveWorkbook.Worksheets.Count
For iSheet = 1 To iSheetCount
With Worksheets(iSheet)
For Each Sh In .Shapes
If Sh.Type = msoPicture Or Sh.Type = 11 Then Sh.Delete
Next Sh
End With
Next iSheet
'
'
End Sub