Exit Sub depending on no of sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro to open a source workbook and to copy from sheet1 to sheet 1 on the destination workbook

The maximum number of sheets on the source workbook is 3


However if there is only 1 sheet on the source workbook , then only macro Open_FPSTAT4 must be activated, if here are two sheets then Open_FPSTAT4 & Open_FPSTAT5 must be activated, If 3 sheets then Open_FPSTAT4, Open_FPSTAT5 & Open_FPSTAT6 must be activated


It would be appreciated if someone could amend my code regarding the above


Code:
 Sub Extract_Int()

Open_FPSTAT4
Open_FPSTAT5
Open_FPSTAT6

End Sub


Sub Open_FPSTAT4()
ChDir ("C:\extract")

Clear_Sheet1
Dim nb As Workbook, tw As Workbook, ts As Worksheet
A = Application.GetOpenFilename
If A = False Or IsEmpty(A) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(A)

nb.Sheets(1).UsedRange.Copy Destination:=ThisWorkbook.Sheets(1).Range("A1")  


 ChDir ("C:\my documents")

 End Sub 


Sub Open_FPSTAT5()
ChDir ("C:\extract")

Dim nb As Workbook, tw As Workbook, ts As Worksheet
A = Application.GetOpenFilename
If A = False Or IsEmpty(A) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(A)
If nb.Worksheets.Count < 2 Then
Exit Sub
Else
nb.Sheets(2).UsedRange.Copy Destination:=ThisWorkbook.Sheets(2).Range("A1")  'change to suit ones needs

End If

 ChDir ("C:\my documents")
 
 End Sub

Sub Open_FPSTAT6()
ChDir ("C:\extract")

Dim nb As Workbook, tw As Workbook, ts As Worksheet
A = Application.GetOpenFilename
If A = False Or IsEmpty(A) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(A)


nb.Sheets(3).UsedRange.Copy Destination:=ThisWorkbook.Sheets(3).Range("A1") 

End If


 ChDir ("C:\my documents")
 
 End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Rather than calling all the macros from Extract_Int, only call the first & then add this to the end of that macro (ie FPSTAT4)
Code:
If Sheets.Count >= 2 Then Open_FPSTAT5
With the same sort of thing at the end of FPSTAT5
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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