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.
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
I have a directory with subdirectories that contains monthly files archived.
I want to get the data from files from subdirectory counting 21 previous days from today and if is not in my current subdirectory, then skip to previous subdirectory.
The way I declared, brings me the content that I need, however with exactly the currently month, number corresponding of month, and year.
Sub getNumber(CM As String, shift As String, fileDate As String)
Dim newDate: newDate = Format(DateAdd("M", 0, Now), "MM-MMMM")
filePath ="\\bt\depthr\cement\trailer\Plans\Day Shift\05-May\trailer_flow_5-5-22 Day Shift Plan.xlsb\" & newDate & "\"
fileName = CM & "_" & "flow" & "_" & fileDate & "Plans" & ".xlsb"```
How can I correct it to bring me previous 21 days and skip to previous subdirectory if the 21 days includes previous month?
Thank you.
This will give you 21 days back date in VBA.
Dim newD As Date
newD = Date - 21
MsgBox newD
I have a folder with files, all named by date. I have the file name (date) that I'm looking for, in cell E2. The cell has already been formatted so that it's in the same format as the file names. Here's what I have so far:
Sub Step2Importsheet()
Sheets.Add Type:= _
"E:\MyFolder\Manipulated Data\Test\" & Range("E2").Text & ".csv"
End Sub
This code works great if the date in cell E2 exists as a file in the folder.
Now here's my problem: In some cases, I have a date listed in E2 that does not exist as a file in the folder. I want to expand the code so that if it doesn't exist, it looks for the next sequential date until it finds a file. (In most cases this will be one or two dates after the date in E2 but it might go as far as five days out. It will never hit an indefinite loop).
Appreciate any and all help!
Something like this should work for you:
Sub LookForFile()
Dim sBaseFolder As String
Dim objFSO As Object
Dim datFileName As Date
Set objFSO = CreateObject("FileSystemObject")
sBaseFolder = "E:\MyFolder\Manipulated Data\Test\"
' Initalize date
datFileName = CDate(Range("E2"))
Do While Not objFSO.FileExists(sBaseFolder & GetFileNameFromDate(datFileName) & ".csv")
datFileName = DateAdd("d", 1, datFileName)
' Maybe place a limit here in case file doesn't exist
Loop
' datFileName should now contain the date of a matching file
End Sub
Function GetFileNameFromDate(ByVal p_date As Date)
' Returns YY-MM-DD format
GetFileNameFromDate = Year(p_date) & "-" & Right("0" & Month(p_date), 2) & "-" & Right("0" & Day(p_date), 2)
End Function
The GetFileNameFromDate function lets you specify the file name format since I imagine it's not a direct date but probably a string with the slashes removed.
Thanks everyone! The code below works well. I had to use Range instead of Date because I had to convert it to Text for it to be able to work as a file name.
Dim newdate As Range
Set newdate = Range("E2")
Do Until Dir("E:\MyFolder\Manipulated Data\Test\" & newdate.Text & ".csv") <> vbNullString
newdate = newdate + 1
Loop
Sheets.Add Type:="E:\MyFolder\Manipulated Data\Test\" & newdate.Text & ".csv"
For whatever reason, everytime I try to use the Day(Now) function in VBA, it keeps displaying "1/9/1900". The Date function displays correctly, so I'm not sure what the issue here is.
Sub Test()
Dim datDay As Date
datDay = Day(Now)
MsgBox datDay
End Sub
Here's an image of the error.
The Day will be an integer somewhere between 1 and 31, depending on, well, the "day" part of the date returned by the DateTime.Now function.
The way dates are stored, they're essentially Double values, with the integer part being a number of days, and the decimal part being the time of day.
Debug.Print Format(CDate(0), "yyyy-mm-dd")
Output: 1899-12-30
We are June 10th, so the date value of 10 corresponds to January 9, 1900.
You want to store the value returned by Day, Month, and Year functions, into Long integer variables; not Date.
Dim datDay As Long
datDay = DateTime.Day(DateTime.Date) ' datDay is 10 because DateTime.Date is 2019-06-10.
Note: while unqualified Day, Date, Month, and Year (and others) functions work perfectly fine, it's probably a good idea to qualify them with the module they are declared in (VBA.DateTime), to avoid potentially confusing ambiguities, e.g. Date is both the name of a property of the DateTime module, and it's also a data type (Dim foo As Date), and the two have very different meanings.
Try:
Option Explicit
Sub Test()
Dim datDay As Date
datDay = Date
MsgBox "Whole date: " & datDay & vbNewLine & _
"Month: " & Month(Date) & " (" & Format(Date, "mmmm") & ")" & vbNewLine & _
"Day: " & Day(Date) & " (" & Format(Date, "dddd") & ")"
End Sub
Result:
Replace
datDay = Day(Now)
with
datDay = Day(Now())
Not sure if this will fix the problem, but =Day(Now()) works correctly when typed directly into a cell.
Your problem is datDay is typed as a Date. =Day(Now()) returns just 10, as today is June 10th. As a full Date value, this is 1/10/1900, since Excel indexes day 0 as 1/0/1900.
I've been banging my head against the wall trying to figure out a dynamic filename problem.
I'm trying to extract the 6-digit date from a cell(J2) that includes time data, and use it to save my filename. The cell is formatted as "General".
The only formula I can get to work is this manual formula using an unoccupied cell (W2), and then deleting (ugly, I know):
Range("W2").Formula = "=DateValue(J2)"
RefDate = Format(Range("W2"), "m-d-yy")
NameofFile = "On Time Departure " & RefDate
Range("W2").Delete
The cell data is this
1/8/2015 2:00:00.000000 AM
I've tried nesting a DateValue function inside a Format function, but can't get it to work.
Any ideas?
Try this:
Dim RefDate As Date
RefDate = Range("J2").Value
NameofFile = "On Time Departure " & Format(RefDate, "m-d-yy")
EDIT
If J2 contains a string and not a date, like Christmas007 noted, try this:
Dim curDate As String
Dim RefDate As Date
curDate = Range("J2").Value
RefDate = DateValue(Left(curDate, InStr(curDate, " ")))
NameofFile = "On Time Departure " & Format(RefDate, "m-d-yy")
I have a column of dates that converted from MS Project into excel as strings.
The format that converted is similar to the following: "March 31, 2014 8:00AM"
I want to convert "March 31, 2014 8:00AM" into type Date but, since it isn't formated MM-DD-YYYY it is not letting me.
Any suggestions?
If the value is in cell A2, for example, you could do the following:
=DATE(YEAR(A2),MONTH(A2),DAY(A2))
For a pure VBA solution, expanding on #cirrusone's suggestion, try this:
Public Sub lime()
Dim dt As Date
' Convert the date string into a Date value.
' Assumes your date string is in cell A1 in the active worksheet of the active
' workbook.
dt = CDate(Cells(1, 1))
' This will print the date with ".." as separators to show that it worked, i.e.
' it should be able to understand every part of the date string passed to it
' after having converted it to a Date value.
MsgBox DatePart("d", dt) & ".." & DatePart("m", dt) & ".." & DatePart("yyyy", dt) & ".." & _
DatePart("h", dt) & ".." & DatePart("n", dt) & ".." & DatePart("s", dt)
End Sub
I put some comments to explain what it's doing. DatePart is a useful function to extract bits of the date (and useful to know in general).