How to COUNTIFS with cells formatted as date - excel

Excuse me, again about COUNTIFS
Dim a As String
a = ct.Range("B4").Text ' B4 is formatted as date
Range("C6").Value = Application.WorksheetFunction.CountIfs(rDat, a, rSec, "1")
rDat and rSec are defined ranges (on another sheet).
rDat has the same format as ct.B4 (i.e. date)
I got "0" as a result, but it is wrong.
I tried to remove date format, and format as text - it works.
But I need date format in rDat and B4.

Dim a As Date
a = ct.Range("B4") ' do not convert B4 to text
Range("C6").Value = Application.WorksheetFunction.CountIfs(rDat, a, rSec, "1")

Related

How to separate date-time stamp into 2 columns?

I have a spreadsheet with a column containing two different datetime formats that I'm trying to split into a standardised column. The two formats are as such:
21/09/2017 7:39
14/07/17 13:47
I've tried splitting out the datetime components individually, but it appears the YEAR function doesn't like parsing the two digit dates. As a ghetto workaround, I've done a find and replace, but this isn't sustainable. Any excel geniuses out there have any ideas or functions I should be looking at?
Cheers
Since you wanted a VBA response to your question, here it is.
You will need to make adjustments to your range, as this is only going to pull off of A1 (you didn't specify the original range with the text dates, nor the ranges of the new date and times).
Option Explicit
Sub SplitDateAndTime()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
Dim RawVal As String, RawDate As String, RawTime As String
RawVal = ws.Range("A1").Value
RawDate = Split(RawVal, " ")(0)
RawTime = Split(RawVal, " ")(1)
Dim NewDate As Date, NewTime As Date
NewDate = CDate(RawDate)
NewTime = CDate(RawTime)
ws.Range("A1") = NewDate
ws.Range("B1") = NewTime
End Sub
Please note this formula assumes all of your dates will be after the year 2000. If that's not true, let me know and I'll figure something else out.
It looks like your first date is being recognised as a proper datetime, while your second is a text value.
Here is what I've worked out:
There is an =IFERROR formula in both the Date and Time columns. The first formula in each works on the datetime format, and the second works on the text format and can be dragged down to work on any cell reference.
This is the formula in B1:
=IFERROR(DATE(TEXT(A2,"yyyy"),TEXT(A2,"mm"),TEXT(A2,"dd")), DATE(MID(MID(A2,FIND("/",A2)+1,5),FIND("/",MID(A2,FIND("/",A2)+1,5))+1,2)+2000,MID(A2,FIND("/",A2)+1,2),LEFT(A2,FIND("/",A2)-1)))
And this is the formula in C1:
=IFERROR(TIME(TEXT(A2,"hh"),TEXT(A2,"mm"),0),TIME(LEFT(MID(A2,FIND(":",A2)-2,5),FIND(":",MID(A2,FIND(":",A2)-2,5))-1),MID(MID(A2,FIND(":",A2)-2,5),FIND(":",MID(A2,FIND(":",A2)-2,5))+1,256),0))
To get the Date assuming that your data in on column A
=DATEVALUE(MID(A1,1,FIND(" ",A1)-1))
T0 get the Time assuming that your data in on column A
=TIMEVALUE(MID(A1,FIND(" ",A1)+1,5))

Cannot convert date string to datevalue in Excel

I have some dates in a string in a column.
Because the format seems to be M/DD/YYYY I sometimes get a VALUE error when using DATEVALUE.
How can I convert a column to make sure all dates are correct.
For example, the following 2 cells
9/29/2006 12:49:58.956 AM DATEVALUE gives an error
9/12/2008 5:36:59.356 PM DATEVALUE converts to 39791
You need to select the range (just one colunm) with the dates... Go to:
(in Excel 2010)
Data >>> Text to Columns
Inside the dialog box select:
Delimited >>> Next
Just select Tab checkbox.
And heres is the magic!
You need to define the arrangement for your dates...
In the Date field:
Choose the what you need (as you say in your question is M/D/Y)
Destination field:
Make sure that is the same of the data you want to format.
And finish.
The problem you are having is probably that the data is getting interpreted as a general field, and shows up as, say 49:59.0 - which isn't text, which is what Datevalue() expects as input; for both inputs, Datevalue(CellID) words if you prepend the text with a ' - which ensures it is treated as text.
it seems that your system settings use dd/mm/yyyy for short date format. and datevalue function uses this settings, so its try to read the day from the first part of the string and the month from the second part. as in your example 9/29/2006 12:49:58.956 AM there is no month with 29 then it gives error, and in the second example it gives 39791 = 9 december 2008 and NOT 12 september 2008.
If you change the short date format in your system settings to mm/dd/yyyythen datevalue function will work correctly, but this not acceptable, so we need to replace month with day and day with month to get correct date.
I don't know if we can do this with Excel formula but we can do that simply with VBA.
So try this code:
Sub Test()
Dim d1 As Variant, d2 As Variant, td As Date
Dim Rng As Range, CL As Range
Set Rng = Range("A1:A2") ' change this to your range
For Each CL In Rng
d1 = Split(CL.Value, " ")
d2 = Split(d1(0), "/")
td = DateSerial(d2(2), d2(0), d2(1))
CL.Offset(0, 1) = td
CL.Offset(0, 1).NumberFormat = "mm/dd/yyyy" ' change the Date format as you need
'CL.Offset(0, 1).NumberFormat = "dd/mm/yyyy"
Next
End Sub

How would I extract the formatted text from a cell in VBA?

Lets say A1 is a date formatted to "YYMMDD" and I enter 7/7/2014 in the cell.
The cells string changes to "140707" but the function bar still shows up as "7/7/2014"
When I try to write this code:
Dim dateCell As String
dateCell = Cells(1, "A")
dateCell will equal to "7/7/2014" and not "140707"
I want the cell to be formatted that way so that when anyone puts in a date, it'll automatically change it to yymmdd. Now how would I get the text dateCell to equal 140707 and not 7/7/2014?
I would greatly appreciate it!!
Thanks
You can see the three different ways to get at the value in a cell here. Put a date in A1, and format it as you format it.
Public Sub test()
Dim r As Range
Set r = Range("a1")
MsgBox ("Value:[" & r.Value & "] Value2:[" & r.Value2 & "] Text:[" & r.Text & "]")
End Sub
In your case you want the Text.
In VBA, you can declare a variable as type Date instead of String which will solve that part of the problem.
In Excel, you need to specify a custom number format to "yyMMdd". Excel will understand that this cell is a date from the value that is in it and extract the proper year, month and day and display it as you specified.

Wrong VBA date format in Excel cell

I'm trying to print out different date formats in my Exel sheet using VBA. However, I can't seem to output the "yyyy-mm-dd" format, or the "mmm dd, yyyy" formats. The cells don't seem to print out the correct formats. For instance:
'Declare new date variables
Dim LongDateFmt As String
Dim ShortDateFmt As String
Dim MediumDateFmt As String
Dim NewDateFmt As String
'Change the date to the new submission date
MediumDateFmt = Format(Date, "yyyy-mm-dd")
Range("A1").Value = MediumDateFmt
LongDateFmt = Format(Date, "Long Date")
Range("A2").Value = LongDateFmt
ShortDateFmt = Format(Date, "Short Date")
Range("A3").Value = ShortDateFmt
NewDateFmt = Format(Date, "MMMM dd, yyyy")
Range("A4").Value = NewDateFmt
A1 prints out 13/06/2013, A2 prints 13-Jun-13, A3 prints out 13/06/2013 and A4 prints 13-Jun-13 as well.
Is this a settings problem or is my code wrong?
you ought to change the cell format and not use formatted strings
with Range("A1")
.Value = Date
.Numberformat = "yyyy-mm-dd"
end with
for instance
Format doesn't set the format of a cell to what you want, it simply gives you a string of a specified format. Inserting that string into a cell won't necessarily give you the results you want since the current formatting of the cell may have an impact.
If you want to change formatting of the cell itself, you need to set its NumberFormat property, with something like:
Range("A1").NumberFormat = "yyyy-mm-dd"

Excel VBA Format cell as custom "h"

I have a cell with a time value as 1:23:45 AM and would like to display just the hour value or use it for other calculations. Need to do this using vba. No, I cannot format the column manually.
Thanks,
Sam
To use VBA to format the cell to only display the hour:
With Worksheets("Sheet1").Cells(1, 1)
.NumberFormat = "H"
End With
To read the hour value in VBA (regardless of how the cell is formatted):
Dim hourValue As Integer
With Worksheets("Sheet1").Cells(1, 1)
If (IsDate(Format(.Value, "hh:mm:ss"))) Then
hourValue = Hour(.Value)
Else
// handle the error
End If
End With
If you want to use the displayed value in code without performing the conversion in VBA then use the Text property of the cell (Range) rather than the Value property. You'll need to be sure that the cell is formatted correctly first:
Dim hourValue As Integer
With Worksheets("Sheet1").Cells(1, 1)
If ((.NumberFormat = "H") And _
(IsDate(Format(.Value, "hh:mm:ss")))) Then
hourValue = CInt(.Text)
Else
// handle the error
End If
End With
If you just want to use the hour value in a worksheet formula then use the HOUR() worksheet function - e.g. =HOUR(A1)
Finally, if you cannot format the column manually because the sheet is protected then you won't be able to format it in VBA either
I am not sure whether your cell is a time field or not. If it is, can you do the following?
dim h as integer
h = Hour(Cell(1,1))

Resources