SOLVED

MACRO - Create Folder/Subfolder based on Today and single cell values - Create *.xlsx in that folder

%3CLINGO-SUB%20id%3D%22lingo-sub-3376597%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376597%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1390592%22%20target%3D%22_blank%22%3E%40JoAvg%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Macro1()%0A%20%20%20%20Dim%20sPath%20As%20String%0A%20%20%20%20Dim%20sDate%20As%20String%0A%20%20%20%20Dim%20sCity%20As%20String%0A%20%20%20%20Dim%20sAddress%20As%20String%0A%20%20%20%20sPath%20%3D%20CreateObject(%22WScript.Shell%22).SpecialFolders(%22Desktop%22)%0A%20%20%20%20sDate%20%3D%20Format(Date%2C%20%22dd%20mm%20yyyy%22)%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5CDelivery%5C%22%20%26amp%3B%20sDate%0A%20%20%20%20If%20Dir(sPath%2C%20vbDirectory)%20%3D%20%22%22%20Then%0A%20%20%20%20%20%20%20%20MkDir%20sPath%0A%20%20%20%20End%20If%0A%20%20%20%20sCity%20%3D%20Worksheets(%22Formulas%22).Range(%22A5%22).Value%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5C%22%20%26amp%3B%20sCity%0A%20%20%20%20If%20Dir(sPath%2C%20vbDirectory)%20%3D%20%22%22%20Then%0A%20%20%20%20%20%20%20%20MkDir%20sPath%0A%20%20%20%20End%20If%0A%20%20%20%20sAddress%20%3D%20Worksheets(%22Formulas%22).Range(%22A6%22).Value%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5C%22%20%26amp%3B%20sAddress%0A%20%20%20%20If%20Dir(sPath%2C%20vbDirectory)%20%3D%20%22%22%20Then%0A%20%20%20%20%20%20%20%20MkDir%20sPath%0A%20%20%20%20End%20If%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376622%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376622%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1390592%22%20target%3D%22_blank%22%3E%40JoAvg%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Macro2()%0A%20%20%20%20Dim%20wbkT%20As%20Workbook%0A%20%20%20%20Dim%20wshS%20As%20Worksheet%0A%20%20%20%20Dim%20wshT%20As%20Worksheet%0A%20%20%20%20Dim%20sPath%20As%20String%0A%20%20%20%20Dim%20sDate%20As%20String%0A%20%20%20%20Dim%20sCity%20As%20String%0A%20%20%20%20Dim%20sAddress%20As%20String%0A%20%20%20%20Dim%20sFile%20As%20String%0A%20%20%20%20Set%20wshS%20%3D%20ThisWorkbook.Worksheets(%22Formulas%22)%0A%20%20%20%20Set%20wbkT%20%3D%20Workbooks.Add(xlWBATWorksheet)%0A%20%20%20%20Set%20wshT%20%3D%20wbkT.Worksheets(1)%0A%20%20%20%20wshT.Range(%22A53%3AI54%22).Value%20%3D%20wshS.Range(%22A53%3AI54%22).Value%0A%20%20%20%20sPath%20%3D%20CreateObject(%22WScript.Shell%22).SpecialFolders(%22Desktop%22)%0A%20%20%20%20sDate%20%3D%20Format(Date%2C%20%22dd%20mm%20yyyy%22)%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5CDelivery%5C%22%20%26amp%3B%20sDate%0A%20%20%20%20sCity%20%3D%20wshS.Range(%22A5%22).Value%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5C%22%20%26amp%3B%20sCity%0A%20%20%20%20sAddress%20%3D%20wshS.Range(%22A6%22).Value%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5C%22%20%26amp%3B%20sAddress%0A%20%20%20%20sFile%20%3D%20%22Station.xlsx%22%0A%20%20%20%20sPath%20%3D%20sPath%20%26amp%3B%20%22%5C%22%20%26amp%3B%20sFile%0A%20%20%20%20wbkT.SaveAs%20Filename%3A%3DsPath%2C%20FileFormat%3A%3DxlOpenXMLWorkbook%0A%20%20%20%20wbkT.Close%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376761%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376761%22%20slang%3D%22en-US%22%3EThese%20worked%20both%20like%20a%20charm.%3CBR%20%2F%3EThank%20you%20a%20ton!!!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376791%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376791%22%20slang%3D%22en-US%22%3EGreat%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376813%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376813%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%20Hans%2C%3C%2FP%3E%3CP%3EIf%20possible%2C%20I%20would%20like%20a%20slight%20modification%20to%20the%20second%20code.%3C%2FP%3E%3CP%3EThe%20code%20copies%26nbsp%3Band%20pastes%20the%20range%20at%20the%20same%20place%3A%20i.e.%20A53%3AI54.%3C%2FP%3E%3CP%3EAny%20chance%20it%20pastes%20at%20A1%3F%3C%2FP%3E%3CP%3EAlso%2C%20if%20possible%2C%20can%20the%20formatting%20also%20be%20pasted%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20again.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376873%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376873%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1390592%22%20target%3D%22_blank%22%3E%40JoAvg%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EChange%20the%20line%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3E%20%20%20%20wshT.Range(%22A53%3AI54%22).Value%20%3D%20wshS.Range(%22A53%3AI54%22).Value%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Eto%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3E%20%20%20%20wshS.Range(%22A53%3AI54%22).Copy%0A%20%20%20%20wshT.Range(%22A1%22).PasteSpecial%20Paste%3A%3DxlPasteValues%0A%20%20%20%20wshT.Range(%22A1%22).PasteSpecial%20Paste%3A%3DxlPasteFormats%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376899%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376899%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYOU%20ARE%20A%20BEAST!!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETHANK%20YOU!!!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3376322%22%20slang%3D%22en-US%22%3EMACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20folder%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3376322%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone%2C%20newcomer%20here.%3C%2FP%3E%3CP%3EI%20have%20a%20book%20with%20two%20main%20sheets%3A%26nbsp%3BOverview%20with%20ref's%20to%20sheet%26nbsp%3BFormulas%26nbsp%3Bwhich%20is%20a%20bunch%20of%20index%2Flookup%2Fsearch%20functions.%3CBR%20%2F%3EI%20am%20in%20need%20of%20two%20macros%20in%20Overview%20sheet%2C%20that%20perform%20the%20following%20tasks.%3C%2FP%3E%3CP%3EMacro%201%3CBR%20%2F%3EIn%20a%20Desktop%20folder%20named%20Delivery%2C%20the%20macro%3A%3CBR%20%2F%3Ea)%20Creates%20a%20folder%20with%20Today's%20date%3CBR%20%2F%3Eb)%20Inside%20DATE%20folder%20it%20creates%20a%20subfolder%20named%20after%20the%20City%20of%20the%20assignment%20is%20at%20(cell%20Formulas!A5)%3CBR%20%2F%3Ec)%20Inside%20CITY%20folder%20it%20creates%20a%20subfolder%20named%20after%20the%20address%20of%20the%20assignment%26nbsp%3B(cell%20Formulas!A6)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20end%20result%20would%20look%20something%20like%20this%3A%3CBR%20%2F%3EC%3A%5CUsers%5Cuser%5CDesktop%5CDelivery%5C15%2005%202022%20%5CCity1%20%5CAddress1%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EI%20review%20about%2020-40%20assignments%20from%2011%20cities%20daily%2C%20so%20this%20means%20that%3A%3CBR%20%2F%3Ea)%20The%20macro%20works%20on%20every%20reviewed%20assignment%20individually%2C%20not%20on%20a%20range%3CBR%20%2F%3Eb)%20The%20date%20folder%20will%20-of%20course-%20be%20created%20once%3CBR%20%2F%3Ec)%20The%20addresses%20must%20reside%20in%20their%20respective%20city%20folders%2C%20so%20if%20City%20found%20more%20than%20once%2C%20any%20duplicates%20should%20be%20avoided.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMacro%202%3C%2FP%3E%3CP%3EThe%20second%20macro%2C%20is%20one%20that%20copies%20the%20range%20FORMULAS!A53%3AI54%2C%20creates%20a%20new%20Station.xlsx%20file%20and%20saves%20in%20the%20respective%20folder%20I%20am%20reviewing.%20For%20example%20if%20in%2016%2005%202022%20I%20am%20reviewing%20Address22%20in%20City4%20%2C%20the%20end%20result%20would%20be.%3CBR%20%2F%3EC%3A%5CUsers%5Cuser%5CDesktop%5CDelivery%5C16%2005%202022%20%5CCity4%20%5CAddress22%20%5CStation.xlsx%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20two%20Macros%20should%20work%20independent%20from%20one%20another%2C%20because%20I%20do%20not%20want%20to%20use%20the%20second%20on%20every%20review.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated.%3CBR%20%2F%3ECheers!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3376322%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3377308%22%20slang%3D%22en-US%22%3ERe%3A%20MACRO%20-%20Create%20Folder%2FSubfolder%20based%20on%20Today%20and%20single%20cell%20values%20-%20Create%20*.xlsx%20in%20that%20fo%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3377308%22%20slang%3D%22en-US%22%3EHey%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello everyone, newcomer here.

I have a book with two main sheets: Overview with ref's to sheet Formulas which is a bunch of index/lookup/search functions.
I am in need of two macros in Overview sheet, that perform the following tasks.

Macro 1
In a Desktop folder named Delivery, the macro:
a) Creates a folder with Today's date
b) Inside DATE folder it creates a subfolder named after the City of the assignment is at (cell Formulas!A5)
c) Inside CITY folder it creates a subfolder named after the address of the assignment (cell Formulas!A6)

 

The end result would look something like this:
C:\Users\user\Desktop\Delivery\15 05 2022 \City1 \Address1

I review about 20-40 assignments from 11 cities daily, so this means that:
a) The macro works on every reviewed assignment individually, not on a range
b) The date folder will -of course- be created once
c) The addresses must reside in their respective city folders, so if City found more than once, any duplicates should be avoided.

 

Macro 2

The second macro, is one that copies the range FORMULAS!A53:I54, creates a new Station.xlsx file and saves in the respective folder I am reviewing. For example if in 16 05 2022 I am reviewing Address22 in City4 , the end result would be.
C:\Users\user\Desktop\Delivery\16 05 2022 \City4 \Address22 \Station.xlsx

 

The two Macros should work independent from one another, because I do not want to use the second on every review.

 

Any help would be greatly appreciated.
Cheers!

8 Replies

@JoAvg 

Sub Macro1()
    Dim sPath As String
    Dim sDate As String
    Dim sCity As String
    Dim sAddress As String
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    sDate = Format(Date, "dd mm yyyy")
    sPath = sPath & "\Delivery\" & sDate
    If Dir(sPath, vbDirectory) = "" Then
        MkDir sPath
    End If
    sCity = Worksheets("Formulas").Range("A5").Value
    sPath = sPath & "\" & sCity
    If Dir(sPath, vbDirectory) = "" Then
        MkDir sPath
    End If
    sAddress = Worksheets("Formulas").Range("A6").Value
    sPath = sPath & "\" & sAddress
    If Dir(sPath, vbDirectory) = "" Then
        MkDir sPath
    End If
End Sub
best response confirmed by JoAvg (Occasional Contributor)
Solution

@JoAvg 

Sub Macro2()
    Dim wbkT As Workbook
    Dim wshS As Worksheet
    Dim wshT As Worksheet
    Dim sPath As String
    Dim sDate As String
    Dim sCity As String
    Dim sAddress As String
    Dim sFile As String
    Set wshS = ThisWorkbook.Worksheets("Formulas")
    Set wbkT = Workbooks.Add(xlWBATWorksheet)
    Set wshT = wbkT.Worksheets(1)
    wshT.Range("A53:I54").Value = wshS.Range("A53:I54").Value
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    sDate = Format(Date, "dd mm yyyy")
    sPath = sPath & "\Delivery\" & sDate
    sCity = wshS.Range("A5").Value
    sPath = sPath & "\" & sCity
    sAddress = wshS.Range("A6").Value
    sPath = sPath & "\" & sAddress
    sFile = "Station.xlsx"
    sPath = sPath & "\" & sFile
    wbkT.SaveAs Filename:=sPath, FileFormat:=xlOpenXMLWorkbook
    wbkT.Close
End Sub
These worked both like a charm.
Thank you a ton!!!

@Hans Vogelaar 

 

Hello Hans,

If possible, I would like a slight modification to the second code.

The code copies and pastes the range at the same place: i.e. A53:I54.

Any chance it pastes at A1?

Also, if possible, can the formatting also be pasted?

 

Thank you again.

@JoAvg 

Change the line

    wshT.Range("A53:I54").Value = wshS.Range("A53:I54").Value

to

    wshS.Range("A53:I54").Copy
    wshT.Range("A1").PasteSpecial Paste:=xlPasteValues
    wshT.Range("A1").PasteSpecial Paste:=xlPasteFormats