Sheet names

cmazur71

Board Regular
Joined
Aug 7, 2003
Messages
63
I have a routine that copies the contents of one workbook sheet onto another. Sometimes the source workbook sheet is named "Export", sometimes it is named "Data".
How can I code this so that the data from the source workbook sheet is always copied, regardless if the sheet is named "Export" or "Data"

Here is my code...it will copy the data from a sheet named "Export"

Set currentworkbook = ThisWorkbook
Set sourceworkbook = Workbooks.Open(RQTReport)
sourceworkbook.Sheets("Export").Copy before:=currentworkbook.Sheets("QuoteTemplate")
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You havent made it clear why you would use the sheet called Export or the sheet called Data. Are you saying only one or the other will exist?
 
Upvote 0
You havent made it clear why you would use the sheet called Export or the sheet called Data. Are you saying only one or the other will exist?
Yes, it one or the other. There are 2 different data sources that I need to pull from...one is Export and one is Data. They contain the same info, but the sheet names will be different.
 
Upvote 0
Consider something like this:

VBA Code:
Function SheetExists(sSheet As String) As Boolean

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    If ws.Name = sSheet Then
        SheetExists = True
        Exit Function
    End If
Next
SheetExists = False
    
End Function

Sub whichsheet()

Dim arr, i As Long, sh As Worksheet

arr = Array("Export", "Data")

For i = LBound(arr) To UBound(arr)
    If SheetExists(CStr(arr(i))) Then
        Set sh = Sheets(arr(i))
        Exit For
    End If
Next

If Not sh Is Nothing Then MsgBox sh.Name

End Sub

That will put the sheet into the variable sh
 
Upvote 0
Consider something like this:

VBA Code:
Function SheetExists(sSheet As String) As Boolean

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    If ws.Name = sSheet Then
        SheetExists = True
        Exit Function
    End If
Next
SheetExists = False
   
End Function

Sub whichsheet()

Dim arr, i As Long, sh As Worksheet

arr = Array("Export", "Data")

For i = LBound(arr) To UBound(arr)
    If SheetExists(CStr(arr(i))) Then
        Set sh = Sheets(arr(i))
        Exit For
    End If
Next

If Not sh Is Nothing Then MsgBox sh.Name

End Sub

That will put the sheet into the variable sh
Is there any other way...I cannot figure out how to embed your code into mine. I don't know what to do with this function
 
Upvote 0
You could do something like this:

VBA Code:
For Each sh In sourceworkbook.Worksheets
    If sh.Name = "Export" Or sh.Name = "Data" Then
        sh.Copy before:=currentworkbook.Sheets("QuoteTemplate")
        Exit For
    End If
Next
 
Upvote 0
Solution

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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