Home > Net >  Create a folder name with year and two digit month
Create a folder name with year and two digit month

Time:02-26

I'm trying to create a folder with a the year and month of my choosing. My issue is that the month name (in this case January) keeps appearing as 1 instead of 01. I've tried several variations of the Format function everywhere in my code but I still get 1 returned. See below for code

Sub CreateFolder()
    
    Dim sMonthName As String
    Dim iMonthNumber As Integer
   
    sMonthName = ThisWorkbook.Worksheets("Overview").Range("C2")
    
    iMonthNumber = Month(DateValue("01-" & sMonthName & "-1900"))
    
  MkDir "FILE PATH" & ThisWorkbook.Worksheets("Overview").Range("C3") & "." & iMonthNumber

End Sub

Cell C2 in the Overview Tab is where I have the month name, while cell C3 in the same tab is where I have the year. Is there a way to return the month as two digits? Let me know if you need any more information

Thank you.

CodePudding user response:

You can simplify a bit by creating a date containing both the month and year, and then using Format$:

With ThisWorkbook.Worksheets("Overview")
   Dim dt As Date
   dt = DateValue("01-" & .Range("C2").Value & "-" & .Range("C3").Value)
End With

MkDir "FILE PATH" & Format$(dt, "yyyy.mm")
  • Related