• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Copy picture/object in a range with VBA macro|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Copy picture/object in a range with VBA macro|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosCopy picture/object in a range with…
sp_PrintTopic sp_TopicIcon
Copy picture/object in a range with VBA macro
Avatar
Gigi Ion

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
February 10, 2020
sp_UserOfflineSmall Offline
1
December 26, 2021 - 6:58 am
sp_Permalink sp_Print sp_EditHistory

Hello!

I have a code that looks for a certain string in a excel file, and when it finds it, it copies the specified range around the cell that contains the string and pastes it into another sheet.

My problem: I would like to copy too a possible picture that would be in the specified range, not just the text and formats..

Dim Sir As String

Sir = VBA.InputBox("Input search string")

Application.ScreenUpdating = False

For Sh = 2 To ActiveWorkbook.Worksheets.Count

Sheets(Sh).Select

Dim FoundCell As Range

Dim LastCell As Range

Dim FirstAddr As String

With Range("B1:B5000")

Set LastCell = .Cells(.Cells.Count)

End With

Set FoundCell = Range("b1:b5000").Find(What:=Sir, After:=LastCell)

If Not FoundCell Is Nothing ThenFirstAddr = FoundCell.Address

End If

Do Until FoundCell Is Nothing

Sheets(Sh).Range("C6").Copy

With Sheets(1).Range("n1000000").End(xlUp).Offset(1, -12)

.PasteSpecial xlPasteValues

.PasteSpecial xlPasteFormats

End With

FoundCell.Offset(-6, -1).Range("A1:N29").Select

Selection.Copy

With Sheets(1).Range("n1000000").End(xlUp).Offset(2, -13).Select

.PasteSpecial xlPasteValues

.PasteSpecial xlPasteFormats

End With

'a

'a

Debug.Print

FoundCell.Address

Set FoundCell = Range("b1:b5000").FindNext(After:=FoundCell)

If FoundCell.Address = FirstAddr ThenExit

Do

End If

Loop

Next Sh

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1844
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
December 30, 2021 - 12:34 am
sp_Permalink sp_Print

Hi Gigi,

To copy a cell that contains an image, you have to make sure that the image is included completely in that cell, not spread across multiple cells.

The code to copy a cell that includes a shape from A1 to D1 is simple:

Range("A1").Copy Destination:=Range("D1")

Avatar
Gigi Ion

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
February 10, 2020
sp_UserOfflineSmall Offline
3
December 30, 2021 - 11:37 pm
sp_Permalink sp_Print

OK, Catalin, thanks for your answer!

Currently I do this - non-automated: select the image ---> Ctrl + C ---> select the destination sheet ---> select the destination cell ---> Ctrl + P ....
I imagine that someone could write a sequence of code to test if there is an image in the Range in the previous post {FoundCell.Offset(-6, -1).Range("A1:N29")  } (to be copied), to select it, to copy-paste it to the destination sheet in a position relatively identical to the initial position.

Happy New Year!!!

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1844
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
December 31, 2021 - 3:30 pm
sp_Permalink sp_Print

Hi Gigi,

In vba, you cannot search a range for shapes, we can only search in sheet shapes (For each Shp in Sh.Shapes).

I provided the easiest way to copy the images:

Range("A1:N29").Copy Destination:=Range("D1")

I assume that you wanted only values, so you can paste again values only:

Selection.Copy

With Sheets(1).Range("n1000000").End(xlUp).Offset(2, -13).Select

.PasteSpecial xlPasteValues

End With

It is possible though to search through each sheet shape and check if Shp.Top and Shp.Left fit between the range positions.

Dim Shp as Shape, Cell1 as Range, LastCell as Range, NewShape as Shape

Set Cell1=Range("A1:N29").Cells(1)

Set LastCell=Range("A1:N29").Cells(Range("A1:N29").Cells.Count)

For Each Shp in Sh.Shapes

if Shp.Top>Cell1.Top And Shp.Top<LastCell.Top+LastCell.RowHeight And Shp.Left>Cell1.Left And Shp.Left<LastCell.Left+LastCell.ColumnWidth then

Shp.Copy

DestSheet.Paste

Set NewShape=DestSheet.Shapes(DestSheet.Shapes.Count)

'set the position based on original shape position

NewShape.Top=Shp.Top

NewShape.Left=Shp.Left

End If

Next

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Mynda Treacy, Robert Hernandez, Lakisha Hall, Chris Ritcey, QSolutions Group
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
Lisa Myers
David von Kleek
Ronald White
Ginette Guevremont
Taryn Ambrosi
Mark Davenport
Christy Nichols
Harald Endres
Ashley Hughes
Herbie Key
Forum Stats:
Groups: 3
Forums: 24
Topics: 6528
Posts: 28594

 

Member Stats:
Guest Posters: 49
Members: 32818
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.