robertmwaring2
Board Regular
- Joined
- Mar 8, 2019
- Messages
- 132
- Office Version
- 365
- Platform
- Windows
Hello,
I have been trying for three days now (totaling more than 24 hours of time) trying to get an image inserted into a sheet using vba. I have a workbook that uses a userform to capture the address (directory) of the picture for each item entered into the userform. All the details for each item entered are stored in a table on one sheet (ActiveWorksheet.Sheet1). Once an item is entered, the user clicks a button to add the next item. Once all items are entered, the user clicks another button to create separate sheets for each item entered based on a template sheet (ActiveWorksheet.Sheet2). Sheet2 is copied (in the same workbook) "x" number of times where x is the number of items that were entered. On each of the new sheets, I am trying to have the picture selected from the form (address/directory for picture is in Sheet1) added (not inserted) to each sheet. I cannot seem to get it to work using the cell reference to Sheet1, only by typing the actually address into the code - what am I doing wrong? I have tried every imaginable way to accomplish this. The ultimate goal would be to have the picture inserted on the "new sheets" centered just shy of the borders of the merged cell range A7:H30 (Sheet2.range("A7:H30") for reference). Below is the closest I have been able to get to a functioning code, but it ONLY works as it is written below, which is not useful because each new page will have a different directory/address for its picture, hence the use of a cell reference.
I have uploaded the workbook here for better reference if needed. Please keep in mind this is not finished, so actually trying to some of the codes via buttons may result in errors.
Click here for Workbook
I have been trying for three days now (totaling more than 24 hours of time) trying to get an image inserted into a sheet using vba. I have a workbook that uses a userform to capture the address (directory) of the picture for each item entered into the userform. All the details for each item entered are stored in a table on one sheet (ActiveWorksheet.Sheet1). Once an item is entered, the user clicks a button to add the next item. Once all items are entered, the user clicks another button to create separate sheets for each item entered based on a template sheet (ActiveWorksheet.Sheet2). Sheet2 is copied (in the same workbook) "x" number of times where x is the number of items that were entered. On each of the new sheets, I am trying to have the picture selected from the form (address/directory for picture is in Sheet1) added (not inserted) to each sheet. I cannot seem to get it to work using the cell reference to Sheet1, only by typing the actually address into the code - what am I doing wrong? I have tried every imaginable way to accomplish this. The ultimate goal would be to have the picture inserted on the "new sheets" centered just shy of the borders of the merged cell range A7:H30 (Sheet2.range("A7:H30") for reference). Below is the closest I have been able to get to a functioning code, but it ONLY works as it is written below, which is not useful because each new page will have a different directory/address for its picture, hence the use of a cell reference.
VBA Code:
Sheet1.Range("TABLE1[MENU ITEM NAME]").COPY
Sheet4.Range("A6").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.DisplayAlerts = True
Dim SHEETCOUNT As Integer
Dim X As Integer
Dim IMGPATH As String
SHEETCOUNT = Sheet1.Range("TABLE1[MENU ITEM NAME]").Rows.COUNT
If SHEETCOUNT > 0 Then
For X = 1 To SHEETCOUNT
Sheet2.COPY After:=ActiveWorkbook.Sheets(Worksheets.COUNT)
ActiveSheet.Range("a4").Value = Sheet1.Range("a" & X).Offset(1, 0).Value
ActiveSheet.Name = Sheet1.Range("A" & X).Offset(1, 0).Value
ActiveSheet.PageSetup.CenterFooter = Sheet4.Range("B" & X).Offset(6, 0).Value
IMGPATH = Sheet1.Range("B" & X).Offset(1, 0).Value '"Z:\Family\Robert\JW\1.jpg" is visible in the editor despite receiving error image cannot be found when using the variable
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 108.75, 476.25, 354.75).Select
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.Fill.UserPicture "Z:\Family\Robert\JW\1.jpg"
Next X
End If
I have uploaded the workbook here for better reference if needed. Please keep in mind this is not finished, so actually trying to some of the codes via buttons may result in errors.
Click here for Workbook