Hello all,
I am new to coding VBA and I am stuck on two aspects of the code. I am trying to insert an image under data that is on the excel sheet(the image is coming from a file on my computer), automatically delete the previous image without deleting a logo that is on the worksheet, and center the image horizontally. Inserting and placing the image under the data on the table works perfectly and sizes perfectly, but I am struggling to center the image horizontally on the page and delete the image. I have tried multiple methods for both, but I have not been successful. I can't seem to delete the image without deleting the logo. Below is my code.
Thank you for reading. Any help is much appreciated
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myPict As Picture
Dim PictureLoc As String
Dim last_row As Long
Dim Copyrange As String
Dim targetCell As Range
Dim HELP As String
Set targetCell = ActiveSheet.Range("A35")
last_row = Cells(Rows.Count, 1).End(xlUp).Row + 2
Copyrange = "B" & last_row & ":" & "B35"
HELP = ((targetCell.MergeArea.Width - myPict.Width) / 2)
If Target.Address = Range("B6").Address Then
'ActiveSheet.Pictures.Delete
PictureLoc = Range("O8").Value & "INSERT PATH HERE" & Range("B6").Value & ".PNG"
Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
With myPict
'Resize picture to fit in the range....
.ShapeRange.LockAspectRatio = msoTrue
.Left = (targetCell.Left + "HELP")
'ActiveSheet.Cells(last_row, "A").Left
.Top = ActiveSheet.Cells(last_row, "B").Top
.Height = ActiveSheet.Range(Copyrange).Height
'.Width = ActiveSheet.Range("B18:J15").Width
'.Placement = 1
'.PrintObject = True
End With
End If
MsgBox (myPict.Width)
MsgBox (targetCell.MergeArea.Width)
MsgBox (targetCell.Left)
MsgBox (HELP)
End Sub
I am new to coding VBA and I am stuck on two aspects of the code. I am trying to insert an image under data that is on the excel sheet(the image is coming from a file on my computer), automatically delete the previous image without deleting a logo that is on the worksheet, and center the image horizontally. Inserting and placing the image under the data on the table works perfectly and sizes perfectly, but I am struggling to center the image horizontally on the page and delete the image. I have tried multiple methods for both, but I have not been successful. I can't seem to delete the image without deleting the logo. Below is my code.
Thank you for reading. Any help is much appreciated
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myPict As Picture
Dim PictureLoc As String
Dim last_row As Long
Dim Copyrange As String
Dim targetCell As Range
Dim HELP As String
Set targetCell = ActiveSheet.Range("A35")
last_row = Cells(Rows.Count, 1).End(xlUp).Row + 2
Copyrange = "B" & last_row & ":" & "B35"
HELP = ((targetCell.MergeArea.Width - myPict.Width) / 2)
If Target.Address = Range("B6").Address Then
'ActiveSheet.Pictures.Delete
PictureLoc = Range("O8").Value & "INSERT PATH HERE" & Range("B6").Value & ".PNG"
Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
With myPict
'Resize picture to fit in the range....
.ShapeRange.LockAspectRatio = msoTrue
.Left = (targetCell.Left + "HELP")
'ActiveSheet.Cells(last_row, "A").Left
.Top = ActiveSheet.Cells(last_row, "B").Top
.Height = ActiveSheet.Range(Copyrange).Height
'.Width = ActiveSheet.Range("B18:J15").Width
'.Placement = 1
'.PrintObject = True
End With
End If
MsgBox (myPict.Width)
MsgBox (targetCell.MergeArea.Width)
MsgBox (targetCell.Left)
MsgBox (HELP)
End Sub