VBA worksheet copy

plost33

Well-known Member
Joined
Oct 2, 2008
Messages
866
i use the following line of code to copy one sheet from my workbook. how can i change this is include two sheets: "Budget" and "Summary"?

ThisWorkbook.Worksheets("Budget").Copy
 
Hi,

Where is the code to paste the sheets? I can't see it in your original code (you are copying them, but not putting them anywhere).

Either way, you would put the hide bit after you have pasted the sheets.
 
Upvote 0

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.
here is my complete code:

Code:
Sub ExportSheet()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
    Dim Folder As String
    Dim locationname As String
    Dim Response, pw
    Dim NewLogRow As Long
    Dim formula As String
    Dim NameValue As String
    Dim FinalName As String
    Dim XXX As String
   
    XXX = Format(Date, "[$-409]yyyymmdd;@")
    locationname = Sheets("Budget").Range("A3").Value
    Folder = "C:\Users\tphythian\Documents\Cost Tracking Exports"
    Worksheets("Budget").Visible = True
   
   Sheets(Array("Summary", "Budget", "Transaction Log")).Copy
   'Sheets("Budget").Copy
    With ActiveSheet.UsedRange
        .Value = .Value
    End With
    With ActiveWorkbook
        .SaveAs Folder & "\" & locationname & " - " & XXX & " - Export" & ".xlsx"
        .Close
    End With
    
                   
    MsgBox "Sheet has been exported and saved in " & "Cost Tracking Exports" & " folder!"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
[\code]
 
Upvote 0
Greetings plost,

Your code seemed to run fine to me, though maybe a bit of qualifying wouldn't hurt. Here's with hiding a sheet in the created wb.
Rich (BB code):
Option Explicit
    
Sub ExportSheet()
Dim Folder          As String
Dim LocationName    As String
Dim XXX             As String
Dim wb              As Workbook
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    With ThisWorkbook
    
        XXX = Format(Date, "[$-409]yyyymmdd;@")
        LocationName = .Worksheets("Budget").Range("A3").Value
        
        Folder = ThisWorkbook.Path  ' "C:\Users\tphythian\Documents\Cost Tracking Exports"
        .Worksheets("Budget").Visible = True
        .Worksheets(Array("Summary", "Budget", "Transaction Log")).Copy
        Set wb = ActiveWorkbook
    End With
    
    With wb
        .ActiveSheet.UsedRange.Value = .ActiveSheet.UsedRange.Value
        'Debug.Print wb.ActiveSheet.Parent.Name
        .Worksheets("Transaction Log").Visible = xlSheetVeryHidden
        .SaveAs Folder & "\" & LocationName & " - " & XXX & " - Export" & ".xls" ' ".xlsx"
        .Close
    End With
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    MsgBox "Sheet has been exported and saved in " & "Cost Tracking Exports" & " folder!"
End Sub

BTW, if you use a forward solidus like [/code] your tags will work.

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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