Paste chart to the first sheet with specific name beginning with ....

filido

New Member
Joined
Jun 7, 2019
Messages
21
Hi,

Is it possible to write a code line that pastes a picture from worksheet A to worksheet B, whose name starts with "Sheet.....". I have multiple sheets named "Sheet1", "Sheet2", "Sheet3" etc. and I only want to copy&paste one picture to the first sheet that comes up first. and I can't use Worksheets("Sheet1") because the number changes everytime since the macro deletes and creates sheets while looping through a new filter.

I'm looking for something like 'Paste the picture to this sheet if it is the first sheet that starts with name "Sheet". Or 'Paste the picture to a sheet whose name starts with "Sheet" and followed by the lowest number, e.g. in case of workbook containing "Sheet13", "Sheet14","Sheet15","Sheet16", the macro would paste the picture to "Sheet13".
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I can't use Worksheets("Sheet1") because the number changes everytime since the macro deletes and creates sheets while looping through a new filter.

But if you tell the macro to put the smallest sheet at the beginning of all the sheets. Then instead of Worksheets("Sheet1") you could use Worksheets(1).


But if the above is not possible, then try the following:

Code:
Sub Copy_Picture()
  Dim sh As Worksheet, n As Long, wmin As Long, s As Worksheet
  Set sh = Sheets("[COLOR=#ff0000]A[/COLOR]") 'Change "[COLOR=#ff0000]A[/COLOR]" to the name of the sheet that contains the picture.
  n = 0
  wmin = Sheets.Count + 1
  For Each s In Sheets
    If InStr(1, LCase(s.Name), LCase("Sheet")) Then
      n = Replace(LCase(s.Name), LCase("Sheet"), "")
      If n < wmin Then
        wmin = n
      End If
    End If
  Next
  If wmin < Sheets.Count + 1 Then
    sh.DrawingObjects(1).Copy
    Sheets("Sheet" & wmin).Paste
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,594
Messages
6,120,436
Members
448,964
Latest member
Danni317

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top