How to save specific hidden sheets to a new workbook?

phyxius117

New Member
Joined
May 14, 2024
Messages
3
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hello all,

I have made the following VBA codes to save specific sheets of a workbook to a new workbook. However when I hide the sheets it no longer works. I want to keep those sheets hidden in the current workbook but not in the new workbook.

Here is the code:
VBA Code:
Sub Export()

Dim DCname As String
    DCname = ActiveWorkbook.Sheets("Reference").Range("R2").Value

Dim FiscalYear As String
    FiscalYear = ActiveWorkbook.Sheets("Reference").Range("R8").Value

Dim Period As String
    Period = ActiveWorkbook.Sheets("Reference").Range("R11").Value

Dim FileName As String
    FileName = DCname & "_SQL Data_FY" & FiscalYear & "_P" & Period

Worksheets(Array("Route Totals", "Stops")).Copy

    With ActiveWorkbook.Worksheets("Route Totals")
    .UsedRange.Value = .UsedRange.Value
    End With
    
    With ActiveWorkbook.Worksheets("Stops")
    .UsedRange.Value = .UsedRange.Value
    End With
    
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

End Sub

Thank you all for any help I can get!!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How about this?


VBA Code:
Sub Export()

  Dim DCname As String
    DCname = ActiveWorkbook.Sheets("Reference").Range("R2").Value

  Dim FiscalYear As String
    FiscalYear = ActiveWorkbook.Sheets("Reference").Range("R8").Value

  Dim Period As String
    Period = ActiveWorkbook.Sheets("Reference").Range("R11").Value

  Dim FileName As String
    FileName = DCname & "_SQL Data_FY" & FiscalYear & "_P" & Period

  Dim Sht As Worksheet
  Worksheets(Array("Route Totals", "Stops")).Copy
  For Each Sht In ActiveWorkbook.Worksheets
    Sht.Visible = xlSheetVisible
  Next Sht

  With ActiveWorkbook.Worksheets("Route Totals")
    .UsedRange.Value = .UsedRange.Value
  End With
    
  With ActiveWorkbook.Worksheets("Stops")
    .UsedRange.Value = .UsedRange.Value
  End With
    
  ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

End Sub
 
Upvote 0

Forum statistics

Threads
1,217,053
Messages
6,134,312
Members
449,865
Latest member
dhpaul1031

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