Cannot make this script work!!!!!

iRobert

New Member
Joined
May 15, 2019
Messages
28
Good Saturday to everyone!
I have been trying to tweak this script for days, but I am no expert.
This script automatically kicks in every time the main Excel file is closed.
It should:
1 - Export a sheet called "Master" from the closing Excel file by creating a new one called Master(Progressive Number).xlsx
2 - Master should be exported into a subdirectory called "\Masters\.
3 - If directory does not exist it should be created.
4 - The name of the new file Master should have a prorssive number so that files keep adding like "Master1.xlsx", 'Master2.xlsx" an so on.

The script below does create the right "Masters" directory, but it creates only one file called "1.xlsx" and nothing else.
If the directory Masters is created the script does nothing else.

Can anyone help me?

Thanks
iR
Code:
Sub ExportMaster()
    Dim strFolder As String
    Dim strFile As String
    Dim m As Long
    Dim n As Long
    Dim wb As Workbook
    strFolder = ActiveWorkbook.Path & "\Masters\"
    strFile = Dir(strFolder & "Master" & "*.xlsx")
    If Dir(strFolder, vbDirectory) = "" Then
            MkDir strFolder
        End If
    Do While strFile <> ""
        m = Val(Mid(strFile, 7))
        If m > n Then
            n = m
        End If
        strFile = Dir
    Loop
    n = n + 1
    Worksheets("Master").Copy
    Set wb = ActiveWorkbook
    wb.SaveAs strFolder & n & ".xlsx"
    wb.Close
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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