Macro help

Jemma Atkinson

Well-known Member
Joined
Jul 7, 2008
Messages
509
Hi, i have the below code which creates folders if they do not exist and then copies worksheets to a new workbook. I need to adjust this code, i require to resave active workbook as Summary dd-mm-yy and delete all sheets apart from these.

([{"Workings","Test1","Test2"}]).

Code:
Sub test1()
Dim myName As String, myFolder As String, e
Dim fso As Object, temp As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
myFolder = "C:\Users\Jemma\Documents\Jemma\VBA & Excel\" & Year(Date) & "\" & Format$(Date, "mmm") _
            & "\Client_Copies"
For Each e In Split(myFolder, "\")
    temp = temp & IIf(temp = "", "", "\") & e
    If fso.FolderExists(temp) = False Then fso.CreateFolder (temp)
Next
myName = Format$(Date, "mmddyy") & "_Copy" & ThisWorkbook.Name
With Workbooks.Add
    ThisWorkbook.Sheets([{"Workings","Test1","Test2"}]).Copy _
    before:=.Sheets(1)
    .SaveAs myFolder & "\" & myName
End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Jemma

I take it to mean that you would be really grateful if someone could give up their time to help you in solving your problem:rolleyes:

Maybe this will work?

Code:
Sub test1()
Dim myName As String, myFolder As String, e
Dim fso As Object, temp As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
myFolder = "C:\Users\Jemma\Documents\Jemma\VBA & Excel\" & Year(Date) & "\" & Format$(Date, "mmm") _
            & "\Client_Copies"
For Each e In Split(myFolder, "\")
    temp = temp & IIf(temp = "", "", "\") & e
    If fso.FolderExists(temp) = False Then fso.CreateFolder (temp)
Next
myName = "Summary" & Format$(Date, "mmddyy") 
With Workbooks.Add
    ThisWorkbook.Sheets([{"Workings","Test1","Test2"}]).Copy _
    before:=.Sheets(1)
    .SaveAs myFolder & "\" & myName
End With
Application.ScreenUpdating = True
End Sub

Mark
 
Upvote 0
Hey Mark, thats it, and I would be very grateful if you can help me;)

ok i tried the below, but it still creates a new workbook and copies the sheets across rather than saving over the active workbook and deleting all sheets except for those 3 mentioned.


Jemma

I take it to mean that you would be really grateful if someone could give up their time to help you in solving your problem:rolleyes:

Maybe this will work?

Code:
Sub test1()
Dim myName As String, myFolder As String, e
Dim fso As Object, temp As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
myFolder = "C:\Users\Jemma\Documents\Jemma\VBA & Excel\" & Year(Date) & "\" & Format$(Date, "mmm") _
            & "\Client_Copies"
For Each e In Split(myFolder, "\")
    temp = temp & IIf(temp = "", "", "\") & e
    If fso.FolderExists(temp) = False Then fso.CreateFolder (temp)
Next
myName = "Summary" & Format$(Date, "mmddyy") 
With Workbooks.Add
    ThisWorkbook.Sheets([{"Workings","Test1","Test2"}]).Copy _
    before:=.Sheets(1)
    .SaveAs myFolder & "\" & myName
End With
Application.ScreenUpdating = True
End Sub

Mark
 
Upvote 0
Do you mean like this ?
Rich (BB code):
Sub test1()
Dim myName As String, myFolder As String, e
Dim fso As Object, temp As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
myFolder = "C:\Users\Jemma\Documents\Jemma\VBA & Excel\" & Year(Date) & "\" & Format$(Date, "mmm") _
            & "\Client_Copies"
For Each e In Split(myFolder, "\")
    temp = temp & IIf(temp = "", "", "\") & e
    If fso.FolderExists(temp) = False Then fso.CreateFolder (temp)
Next
myName = "Summary " &  Format(Date, "ddmmyy") & ".xls"
ThisWorkbook.Sheets([{"Workings","Test1","Test2"}]).Copy
ActiveWorkbook.SaveAs myFolder & "\" & myName
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Seiya, i need to resave over the active workbook which is this workbook i am running this macro from as i need to keep this macros when file is resaved.

Do you mean like this ?
Rich (BB code):
Sub test1()
Dim myName As String, myFolder As String, e
Dim fso As Object, temp As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
myFolder = "C:\Users\Jemma\Documents\Jemma\VBA & Excel\" & Year(Date) & "\" & Format$(Date, "mmm") _
            & "\Client_Copies"
For Each e In Split(myFolder, "\")
    temp = temp & IIf(temp = "", "", "\") & e
    If fso.FolderExists(temp) = False Then fso.CreateFolder (temp)
Next
myName = "Summary " &  Format(Date, "ddmmyy") & ".xls"
ThisWorkbook.Sheets([{"Workings","Test1","Test2"}]).Copy
ActiveWorkbook.SaveAs myFolder & "\" & myName
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Sub test1()
Dim myName As String, myFolder As String, e
Dim fso As Object, temp As String
Application.ScreenUpdating = False
Set fso = CreateObject("Scripting.FileSystemObject")
myFolder = "C:\Users\Jemma\Documents\Jemma\VBA & Excel\" & Year(Date) & "\" & Format$(Date, "mmm") _
            & "\Client_Copies"
For Each e In Split(myFolder, "\")
    temp = temp & IIf(temp = "", "", "\") & e
    If fso.FolderExists(temp) = False Then fso.CreateFolder (temp)
Next
myName = "Summary " & Format$(Date, "mmddyy") & ".xls"
Application.DisplayAlerts = False
With ThisWorkbook
    For Each ws In .Sheets
        Select Case ws.Name
            Case "Workings","Test1","Test2"
            Case Else: ws.Delete
        End Select
    Next
    Application.DisplayAlerts = True
    .SaveAs myFolder & "\" & myName
End With
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,556
Messages
6,120,190
Members
448,949
Latest member
keycalinc

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