Select Sheet Name on a sheet to use in VBA

ElRoober

New Member
Joined
Feb 11, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi Everybody,

Hopefully a very simple one...........

Below I have some code that allows me to open a workbook elsewhere based on a location (M12) on a Sheet called 'Address'. What I can't figure out is how to then select the Sheet I am after in what would be cell E13 on the sheet address as well. Its currently called and coded as "VAT Return" but the name changes hence why I want to use a cell reference in the code. Is this possible as I cant seem to figure it out.

Dim sourceBook As Workbook
Application.ScreenUpdating = False
Set sourceBook = Workbooks.Open(Sheets("Address").Range("M12").Value)
sourceBook.Sheet.Name("VAT Return").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
sourceBook.Close
Application.ScreenUpdating = True

Many Thanks

D
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
See if this helps.

Rich (BB code):
Sub CopySheet()

    Dim sourceBook As Workbook
    Dim srcWBName As String
    Dim srcShtName As String
    
    Application.ScreenUpdating = False
    
    With ThisWorkbook.Worksheets("Address")
        srcWBName = .Range("M12").Value
        srcShtName = .Range("E13").Value
    End With
    
    Set sourceBook = Workbooks.Open(srcWBName)
    With ThisWorkbook
        sourceBook.Worksheets(srcShtName).Copy After:=.Sheets(.Sheets.Count)
    End With
    sourceBook.Close
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
See if this helps.

Rich (BB code):
Sub CopySheet()

    Dim sourceBook As Workbook
    Dim srcWBName As String
    Dim srcShtName As String
   
    Application.ScreenUpdating = False
   
    With ThisWorkbook.Worksheets("Address")
        srcWBName = .Range("M12").Value
        srcShtName = .Range("E13").Value
    End With
   
    Set sourceBook = Workbooks.Open(srcWBName)
    With ThisWorkbook
        sourceBook.Worksheets(srcShtName).Copy After:=.Sheets(.Sheets.Count)
    End With
    sourceBook.Close
   
    Application.ScreenUpdating = True

End Sub
Thank you so much for your help with this; this is great :)
 
Upvote 0
See if this helps.

Rich (BB code):
Sub CopySheet()

    Dim sourceBook As Workbook
    Dim srcWBName As String
    Dim srcShtName As String
   
    Application.ScreenUpdating = False
   
    With ThisWorkbook.Worksheets("Address")
        srcWBName = .Range("M12").Value
        srcShtName = .Range("E13").Value
    End With
   
    Set sourceBook = Workbooks.Open(srcWBName)
    With ThisWorkbook
        sourceBook.Worksheets(srcShtName).Copy After:=.Sheets(.Sheets.Count)
    End With
    sourceBook.Close
   
    Application.ScreenUpdating = True

End Sub
Thank you so much for looking at this for me; this is great and much appreciated :)
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,716
Members
449,464
Latest member
againofsoul

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