Forum Discussion
Dbstedman
Apr 13, 2022Brass Contributor
VBA code to copy contents from a cell, and then replace an existing picture with a pic of data
I'm looking for VBA code that could copy cells and paste them as a picture (say Picture 1). I would then like to cut Picture 1 and replace another picture (Picture 2) with Picture 1, while keeping th...
- Apr 18, 2022
Dbstedman , in that case, replace the ActiveSheet too with the relevant sheet,
Sub CopyRangeToPreFormattedPicture() Dim picPreFormatted As ShapeRange Set picPreFormatted = Sheets("Sheet2").Shapes.Range("PicTemplate") Dim rng As Range Set rng = Sheets("Sheet1").Range("a1:d5") rng.Copy Sheets("Sheet2").Pictures.Paste Dim picRange As ShapeRange Set picRange = Sheets("Sheet2").Shapes.Range(Sheets("Sheet2").Shapes.Count) picPreFormatted.PickUp With picRange .Apply .Top = picPreFormatted.Top .Left = picPreFormatted.Left .Height = picPreFormatted.Height .Width = picPreFormatted.Width End With picPreFormatted.Delete picRange.Name = "PicTemplate" End Sub
amit_bhola
Apr 18, 2022Iron Contributor
Dbstedman , in that case, replace the ActiveSheet too with the relevant sheet,
Sub CopyRangeToPreFormattedPicture()
Dim picPreFormatted As ShapeRange
Set picPreFormatted = Sheets("Sheet2").Shapes.Range("PicTemplate")
Dim rng As Range
Set rng = Sheets("Sheet1").Range("a1:d5")
rng.Copy
Sheets("Sheet2").Pictures.Paste
Dim picRange As ShapeRange
Set picRange = Sheets("Sheet2").Shapes.Range(Sheets("Sheet2").Shapes.Count)
picPreFormatted.PickUp
With picRange
.Apply
.Top = picPreFormatted.Top
.Left = picPreFormatted.Left
.Height = picPreFormatted.Height
.Width = picPreFormatted.Width
End With
picPreFormatted.Delete
picRange.Name = "PicTemplate"
End Sub
Dbstedman
Apr 18, 2022Brass Contributor
That you, that part worked, but it didn't copy the formatting (specifically the glow format) nor does it put it in the same location
- amit_bholaApr 19, 2022Iron Contributor
Dbstedman , so finally did it work? Was the destination picture named PicTemplate while you checked it for another sheet case?
Anyways, attaching the file which was working at my end. Hope it helps.
- DbstedmanApr 19, 2022Brass ContributorYes! I made a few more minor adjustments but it is working now. Thanks for the help!