Paste into variable named worksheet

kbd230

New Member
Joined
Dec 29, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi,

In my macro I need to copy paste a data range from one workbook to another. The data source workbook is always the same name but the destination workbook changes periodically and has no naming convention.
The destination workbook is also not the workbook in which the VBA code runs, so I can't use 'ThisWorkbook'. The destination workbook does however have a sheet which is always called the same, but I can't find out how (if possible) activating a sheet which is within a variable-named workbook.

Here's my code:

Sub Macro17()
'
' Macro17 Macro
'
Windows("datasource.csv").Activate
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("destination.xlsx").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This code loops through all workbooks that are open and searches each one to find the worksheet name "CPU" and then activate and selelcts that worksheet to allow you to paste to that worksheet.
Obviously changethe name to the workhseet name you are looking for or even set it dynamically:
VBA Code:
Sub findsheet()
nametofind = "CPU"
Dim wb As Workbook
Dim ws As Worksheet
exitloop = False
For Each wb In Application.Workbooks
 wb.Activate
 For Each ws In Worksheets
    If ws.Name = nametofind Then
     exitloop = True
     ws.Select
     ''' you can put your paste code here!!
         
     Exit For
    End If
Next ws
If exitloop Then Exit For
Next wb

End Sub
 
Upvote 0
Solution
This code loops through all workbooks that are open and searches each one to find the worksheet name "CPU" and then activate and selelcts that worksheet to allow you to paste to that worksheet.
Obviously changethe name to the workhseet name you are looking for or even set it dynamically:
VBA Code:
Sub findsheet()
nametofind = "CPU"
Dim wb As Workbook
Dim ws As Worksheet
exitloop = False
For Each wb In Application.Workbooks
 wb.Activate
 For Each ws In Worksheets
    If ws.Name = nametofind Then
     exitloop = True
     ws.Select
     ''' you can put your paste code here!!
        
     Exit For
    End If
Next ws
If exitloop Then Exit For
Next wb

End Sub

Thank you, also for the explanation so I learn by doing, works like a charm!
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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