I have a textbox wherein I input number as months (like "01" for January) but I need the actual month name instead of the number in my VBA statements. Can you please help me convert the number in the textbox into Actual Month Name.
I have tried the statement below but it is not working:
Dim MoName As String
MoName = Format(Month(Monthtxtbx.Text), "MMMM")
Range("Cells(1,1):Cells(LastCol, Lastcolumn)").AutoFilter Field:=3, Criteria1:=MoName
Use MoName = MonthName(Monthtxtbx.Text) to get the month name.
Alternatively, you could dummy up a date so that you could use the "mmmm" format as:
MoName = Format(DateSerial(2000,Monthtxtbx.Text,1), "MMMM")
but that would be the long way to achieve something simple.
Related
I have 2 cells in Range B:B, which I wish to apply countif formula to via VBA in Excel.
27/09/2017
13/06/2018
I have tried
MsgBox (WorksheetFunction.CountIf(Range("B:B"), "27/09/2017"))
This returns 0
MsgBox (WorksheetFunction.CountIf(Range("B:B"), CDate("27/09/2017")))
This returns 1
I am trying to figure out how I can get the macro to return both date values, via
MsgBox (WorksheetFunction.CountIf(Range("B:B"), "<=" & CDate("13/06/2018")))
However this returns 0
This seems to indicate that I do not quite understand how to filter dates in countif via strings. Would it be possible to share a alternative?
I hope to implement a text input box where the user can input a date string which then applies the countif on
You need to convert the date into a double first to compare the actual value of the date independent from the format.
"<=" & CDbl(CDate("13/06/2018"))
Why is this?
If you concatenate a string with a date like in "<=" & CDate("13/06/2018") the date is converted into a string and the result is "<=13/06/2018" (here the date is a string not a value).
If you convert the date into a double CDbl(CDate("13/06/2018") the result is 43264 which is the serial number that represents the date.
Dates in Excel are stored as serial numbers counting from 1900-01-01 so 2018-06-13 is day 43264 since 1900-01-01.
So with "<=" & CDbl(CDate("13/06/2018")) you actually compare the cell value to "<=43264". So you compare to a value and not a text. This works because if a cell is formatted as date Excel actually stores the serial value but shows the formatted string for user compatibility.
Conclusion
If you want to compare dates always compare their values not their strings.
Also see: How to use dates and times in Excel.
I was wondering if there is any possibility to write a VBA code where the column A should always have a date format like this: 12.10.2017 (not 12/10/2017 or 12-10-2017). If anything else is written in the column A like "12" or "car" the entry should be deleted. It has to accept only the date format mentioned above.
I used data validation for this, with length 10 and the date format to take only "." into consideration, but I want to do it as a VBA code instead.
Thanks!
A valid date is a long representing the number of days since the 1st january 1900. So a valid date would be 45603. You can display this date in any format you wish using the format codes d, m and y . So to display the date as dd.mm.yyyy then set that numberformat in the cells in column A. Your problem though is that Excel will only accept a date entered as either a long or in a built in date format (using /, - or space as a separator). You could allow the users to enter a text string in the format dd.mm.yyyy and then convert that string into a date and then reject it if the conversion didn't result in a valid date - but wouldn't it be easier to just train your users to enter dates correctly?
I think that I try to do something what is impossible. I have in range A1:A5 some dates. These cells have to have date format, because I’d like to check if they are bigger or smaller than certain date. Moreover I’d like to display these dates in a range A1:A5 in a following format: yyyy.mm. I think that there is no way to kill two birds with one stone.
Public Sub test()
Range("A1:A5").NumberFormat = "yyyy.mm"
Range("A1:A5") = Format(Date, "yyyy.mm")
If IsDate("A2") Then
Range("B2") = "OK"
End If
End Sub
Range("B1:B5").NumberFormat = "yyyy.mm" change the cells format on custom (yyyy.mm) but the dates are still displayed in unchanged form (yyyy.mm.dd)
Range (“A1:A5”) = Format(Date, “yyyy.dd”) displays the dates yyyy.mm but the year and month has been changed to 1905.07. Moreover the dates do not have Date format but Custom.
Is is really not possible to display a date like yyyy.mm but in date format (so that in formular bar the date is displayed as dd.mm.yyyy or another way around) to be able to compare them with different dates?
Best regards,
Neke
First of all, using points within dates can be a problem, because "2017.05" is interpreted by Excel as a number.
You can reformat the dates into "yyyy/mm" by using something like this:
For Each r In Range("A1:A5")
r.Value = VBA.Format(r, "yyyy/mm")
Next r
I have a excel sheet in which a column has date date in the format "yyyyMMdd" and I want to format it as "yyyy/MM/dd".
For this I tried to use following line inside macro, but it's converting cell data as "###.....#" instead of changing date format.
Sheet1.Range("C3", "C302").NumberFormat = "yyyy/mm/dd"
...
result = "#####...#"
...
Can someone tell me why it's happening? Is there any other way for doing this?
If a date/time cell appears full of # signs, it means that the column is too narrow to display the format.
Make the column wider to accommodate the full width of the selected date format.
See this screenshot. Both columns have the same format. Column A is too narrow to show the dates. Column B is wide enough.
Edit after discussing in chat:
The screen shot you posted in chat is this:
The "dates" you are referring to are not dates. They are numbers that are way higher than what Excel uses for dates in this millenium.
Excel stores dates as whole numbers, starting as 1 for 1/1/1900. What you show in your screenshot are numbers way higher than Excel dates.
Your number 20150930 is NOT what Excel considers Sep-30-2015. For Excel, that date would be the number 42277, which you can perfectly format as that date.
The reason that your "dates" formatted with your format string come out as ##### is that the numbers are way higher than what Excel can interpret as dates.
You will need to convert your numbers to real Excel dates, which you can do with a simple formula. With your first "date" number in cell A1, you can use the formula
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
to return a value that Excel regards as a true date for Sep-30-2015 in this screenshot:
So, the reason for all the # signs is that the numbers you are trying to format as dates are too big for dates in Excel's algorithms.
With all the good answers, I will add simple vba solution...
Option Explicit
Sub FormatDate()
Dim xlRng As Range
Dim xlShtRng As Range
'//- Date format 20160112
Set xlShtRng = [A3:A10] '//- or [A3, A6, A10]
For Each xlRng In xlShtRng
xlRng.Value = DateSerial(Left(xlRng.Value, 4), Mid(xlRng.Value, 5, 2), Right(xlRng.Value, 2))
xlRng.NumberFormat = "yyyy/mm/dd" '//- 2016/01/12
Next
End Sub
Please try this..
=LEFT(A1,4)&"/"&MID(A1,5,2)&"/"&RIGHT(A1,2)
I want to filter SQL-table between start date and end date, I used before string variable then I use string.format to make the format mm/dd/yyyy, I tried now in VB.net 2015 the following code:
Dim S as String
s=inputbox("Enter Start date")
S=string.format(S,"mm/dd/yyyy")
But it doesn't work, can somebody give me a solution?
You could try this for handling the input value, assuming you only need the date value as a formatted string, since your question is about formatting a date:
Dim S As String
S = InputBox("Enter Start date")
If IsDate(S) = True Then
Dim d As Date = Date.Parse(S)
S = d.ToString("mm/dd/yyyy")
Else
'Handle the non date input here
End If
But I think you should consider #Plutonix comment, since we don't know exactly how you are sending the date to perform the filtering, or how your table fields are defined.
Regards!