Selecting Worksheet from a Range?

HalifaxFox

New Member
Joined
Jul 18, 2011
Messages
2
Hi,

I am fairly new to Non-Recorded Macros.

I have a Test.xls spreadsheet and am trying to create a macro that will select files and sheet names from different Ranges input in that Test.xls file. I want both the files and the specific tabs/worksheets from the files to be variable, and selected from the defined range.

Currently, I have the following code:

varWorkbook = "Variable Workbook Location" & Range("L1").Value
Workbooks.Open varWorkbook
Sheets("Sheet1").Select
Cells.Select
Selection.Copy
Windows("Test.xls").Activate
Sheets("01").Select
Range("A1").Select
ActiveSheet.Paste


The formula works for selecting the custom workbook based on the workbook name I input in cell L1 of Test.xls.

I want to duplicate that for the tab name from the workbook, to replace the Sheets("Sheet1").Select with whatever sheet name I put in, say, cell M1 of the Test.xls workbook.


Any advice would be much appreciated!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Code:
    Dim wbVar As Workbook, wsVar As Worksheet
    Dim wsDest As Worksheet
    Dim varWorkbook As String, varWorksheet As String
        
    varWorkbook = "[COLOR="Red"]C:\MyFolder\[/COLOR]" & Range("L1").Value
    varWorksheet = Range("M1").Value
    
    Application.ScreenUpdating = False
    
    Set wsDest = ThisWorkbook.Sheets("01")
    Set wbVar = Workbooks.Open(varWorkbook)
    Set wsVar = wbVar.Sheets(varWorksheet)
    
    wsVar.UsedRange.Copy Destination:=wsDest.Range("A1")
    
    [COLOR="Green"]' Close the varWorkbook[/COLOR]
    wbVar.Close SaveChanges:=False
    
    Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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