VBA copy data to new sheet and save with a specific cell value

ceytl

Board Regular
Joined
Jun 6, 2009
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
I have a VBA script that copies a specified sheet to a new workbook.

The problem is I want to automatically save the new workbook with the file name "1st Order" & the cell value in B5

Here is my code:

VBA Code:
Sub SaveSplitOrder1()
    Dim NewName As String
    Dim nm As Name
    Dim ws As Worksheet

    With Application
        .ScreenUpdating = False
       

        On Error GoTo ErrCatcher
        Sheets(Array("1")).Copy
        On Error GoTo 0
       
        For Each ws In ActiveWorkbook.Worksheets
            ws.Cells.Copy
            Application.CutCopyMode = False
            Cells(1, 1).Select
            ws.Activate
        Next ws
        Cells(1, 1).Select
     
        .ScreenUpdating = True
    End With
    Exit Sub
   
ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub

Does anyone know how I can do that with my current code?

Thanks
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
Sub SaveSplitOrder1()
    
    Application.ScreenUpdating = False
    
    On Error GoTo ErrCatcher
    Sheets("1").Copy
    On Error GoTo 0
    
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\1st Order " & Range("B5").Value, FileFormat:=51
    
    Application.ScreenUpdating = True
    Exit Sub
    
ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub
 
Upvote 0
VBA Code:
Sub SaveSplitOrder1()
   
    Application.ScreenUpdating = False
   
    On Error GoTo ErrCatcher
    Sheets("1").Copy
    On Error GoTo 0
   
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\1st Order " & Range("B5").Value, FileFormat:=51
   
    Application.ScreenUpdating = True
    Exit Sub
   
ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub

Thankyou so much!

If I wanted to save another sheet named "2" how would I loop it?
 
Upvote 0
VBA Code:
Sub SaveSplitOrder1()
    
    Dim v As Variant, ws As Worksheet
    Application.ScreenUpdating = False
    
    For Each v In Array("1", "2")
    
        Set ws = Nothing
        On Error Resume Next
        Set ws = ThisWorkbook.Sheets(v)
        On Error GoTo 0
        
        If Not ws Is Nothing Then
            ws.Copy
            ActiveWorkbook.SaveAs ThisWorkbook.Path & "\1st Order " & Range("B5").Value, FileFormat:=51
        Else
            MsgBox v & " sheet does not exist within this workbook"
        End If
    
    Next v
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
VBA Code:
Sub SaveSplitOrder1()
   
    Dim v As Variant, ws As Worksheet
    Application.ScreenUpdating = False
   
    For Each v In Array("1", "2")
   
        Set ws = Nothing
        On Error Resume Next
        Set ws = ThisWorkbook.Sheets(v)
        On Error GoTo 0
       
        If Not ws Is Nothing Then
            ws.Copy
            ActiveWorkbook.SaveAs ThisWorkbook.Path & "\1st Order " & Range("B5").Value, FileFormat:=51
        Else
            MsgBox v & " sheet does not exist within this workbook"
        End If
   
    Next v
   
    Application.ScreenUpdating = True
End Sub
Thank you for your help!

One last question, if I wanted sheet name "2" to save as "2nd Order" and value of B5 what would I do?
 
Upvote 0
VBA Code:
Sub SaveSplitOrder1()
    
    Application.ScreenUpdating = False
    
    On Error GoTo ErrCatcher
    Sheets("1").Copy
    On Error GoTo 0
    
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\1st Order " & Range("B5").Value, FileFormat:=51
    
    On Error GoTo ErrCatcher
    Sheets("2").Copy
    On Error GoTo 0
    
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\2nd Order " & Range("B5").Value, FileFormat:=51
    
    Application.ScreenUpdating = True
    Exit Sub
    
ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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