Export and save IF a specific sheet exists

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
Here's my code:

Code:
Function Export()
Dim Fname As String, ws As Worksheet
Fname = "Vesting Review - " & Sheets("Instructions").Range("D5").Value
Sheets(Array("VDR", "SP", "DDR", "VOR", "TDR")).Copy
For Each ws In ActiveWorkbook.Worksheets
    With ws.UsedRange
        .Value = .Value
    End With
Next ws
With ActiveWorkbook
    .SaveAs ThisWorkbook.Path & "\" & (Fname)
    .Close
End With
End Function

It works as expected. However, all sheets will not always exist. Sometimes individual sheets do not exist. How can I account for that?

Can this somehow include an IF(ISREF...) copy sheet1, sheet2, etc.? Not sure best way to accomplish. Ty in advance.
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Can't see the purpose of the copy command.
 
Upvote 0
How about
Code:
Sub MyExport()
   Dim Fname As String, ws As Worksheet
   Dim Ary As Variant
   Dim i As Long
   Fname = "Vesting Review - " & Sheets("Instructions").Range("D5").Value
   Ary = Array("VDR", "SP", "DDR", "VOR", "TDR")
   For i = 0 To UBound(Ary)
      If Not Evaluate("isref('" & Ary(i) & "'!A1)") Then
         MsgBox "sheet " & Ary(i) & " does not exist"
         Exit Sub
      End If
   Next i
   Sheets(Ary).Copy
   For Each ws In ActiveWorkbook.Worksheets
       With ws.UsedRange
           .Value = .Value
       End With
   Next ws
   With ActiveWorkbook
       .SaveAs ThisWorkbook.Path & "\" & (Fname)
       .Close
   End With
End Sub
 
Upvote 0
Thanks Fluff! Is there any way for it to say if sheet X does not exist, continue on to sheet Y, etc., instead of ending the function? Appreciate your help!
 
Upvote 0
How about
Code:
Sub MyExport()
   Dim Fname As String, ws As Worksheet
   Dim Ary As Variant, tmp As Variant
   Dim i As Long
   Fname = "Vesting Review - " & Sheets("Instructions").Range("D5").Value
   Ary = Array("VDR", "SP", "DDR", "VOR", "TDR")
   tmp = Ary
   For i = 0 To UBound(Ary)
      If Not Evaluate("isref('" & Ary(i) & "'!A1)") Then
         tmp = Filter(tmp, Ary(i), False, vbTextCompare)
      End If
   Next i
   Sheets(tmp).Copy
   For Each ws In ActiveWorkbook.Worksheets
       With ws.UsedRange
           .Value = .Value
       End With
   Next ws
   With ActiveWorkbook
       .SaveAs ThisWorkbook.Path & "\" & (Fname)
       .Close
   End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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