How to save file in subfolder

BlokeMan

Board Regular
Joined
Aug 9, 2011
Messages
125
Hi,

I have a macro that search for existing files, creates and saves. What I want is it's going to save the file to the existing subfolders which is JAN to DEC based on the date (which is month) and it's going to do filesearch all the subfolder for existing file.

Thanks

Code:
Private Sub Workbook_Open()
Dim OpenName, PON As String, CODE As String, Response As String, PONMessage As String, MyString As String


Application.ScreenUpdating = False

OpenName = ActiveWorkbook.Name  'Check file name
PONMessage = "You have not entered a PON Number. To enter a PON press 'Yes', to exit sheet press 'No'."
If OpenName = "Polybag Load Reconciliation.xls" Then  ' If file is the template

    Do While PON = ""
        PON = InputBox("Please enter the PON of the new batch: ", "Enter PON") ' Enter PON Number
        If PON = "" Then ' Check if entry or cancel has been hit
            Response = MsgBox(PONMessage, vbYesNo, "PON Error") ' Error message explaining that they either need to enter a valid PON or exit sheet
            If Response = vbYes Then ' if 'YES' then continue loop
            Else
                ActiveWorkbook.Close savechanges:=False ' Close template without saving
            End If
        End If
          
        If Len(PON) = 6 Then            ' If PON contains 6 characters then
            If IsNumeric(PON) = True Then ' If the string contains only numbers then
                Response = MsgBox("You have entered PON Number " & PON & ". Is this correct?", vbYesNo, "PON Confirmation")
                If Response = vbNo Then   ' If they click the 'no' button - reset PON
                    PON = ""
                End If
            Else                          ' Otherwise explain the entry must only contain numbers
                MsgBox "Sorry, your entry must only contain numbers."
                PON = ""
            End If
        Else                              ' Otherwise explain a PON requires 6 numbers
            MsgBox "Sorry, a PON needs to have 6 numbers."
            PON = ""
        End If
    Loop
    Dim FullPath As String              ' Set a variable for the full path of the current file

    FullPath = ActiveWorkbook.Path      ' Assign the current path to the variable
    
    Set fs = Application.FileSearch     ' Set file search object
    With fs                             ' With the fs object
        .NewSearch                      ' Clear the previous object settings
        .Filename = FullPath & "\" & PON & " test.xls" ' Look for any files named with the entered PON
        .MatchTextExactly = True        ' Ensure an exact match
        If .Execute > 0 Then            ' If the search finds a file with that PON -
                                        ' Send message explaining the file already exists
            MsgBox "Sorry, a file with this name has already been created. Please open the existing file."
                                        ' Close the template without saving
            ActiveWorkbook.Close savechanges:=False
                                        ' Else save the workbook with the New PON as normal
        Else
    
            Do While CODE = ""
                CODE = InputBox("Please enter the CODE of the new batch: ", "Enter CODE") ' Enter product CODE
                If CODE = "" Then
                    MsgBox ("You must enter a product code.") ' Error message for no product code input
                End If
            Loop
            Worksheets("PAP Yield").Activate
            ActiveSheet.Unprotect Password:="Recon"
            Cells(1, 2).Select
            Selection.Locked = False
            Cells(1, 2) = PON  ' Puts value of PON into worksheet
            Selection.Locked = True
            Cells(1, 5).Select
            Selection.Locked = False
            Cells(1, 5) = CODE ' Puts value of CODE into worksheet
            Selection.Locked = True
            Cells(1, 7).Select
            Selection.Locked = False
            Cells(1, 7) = Date ' Enters the date into worksheet
            Selection.Locked = True
            Cells(1, 9).Select
            Selection.Locked = False
            Cells(1, 9) = Time ' Enters the Time into worksheet
            Selection.Locked = True
            Cells(5, 2).Select 'Parks cursor
            ActiveSheet.Protect Password:="Recon"

            ActiveWorkbook.SaveAs (FullPath & "\" & PON & " test.xls")
        End If
    End With
End If

End Sub
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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