I have two dates in cell "A1" (Start Date) and "B1" (End Date). I change the date to get the real figures between the dates using SUMIFS formula.
There is Date Column in the Sheet with Time.
So what i want is that whenever the "A1" (Start Date) changes its time should always be initial like 00:00:00 and when i change the date for "B1" (End Date) it should always be like 23:59:59 is there any way to achieve this using VBA.
Your help will be appreciated.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
With Range("B1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
End If
Numeric time 23:59:59 is not equal to 0.9999999, it is 0.999988425925926
But use native date/time handling for this and avoid smart numbers:
Range("A1").Value = DateValue(Range("A1").Value)
Range("B1").Value = DateValue(Range("B1").Value) + TimeSerial(23, 59, 59)
I think this could work, if i understood your question correct.
It will make sure A1 is integer and make B1 integer then add 0.99999.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
Range("A1").value = cint(range("A1").value)
Range("B1").value = cint(range("B1").value) + 0.99999999
End if
Just make sure the cells is set up to display as date and time.
If I understand you correctly, you don't need to bother with the time portion of the dates or go throught the mechanics of trying to change it.
You just need to properly construct your SUMIFS formula.
Range to sum: sumRange
Dates in your table: myDates
When you enter a date in A1 or B1, it will be entered as if it were at the beginning of the day.
If your dates in myDates are just dates, you can use the formula:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<=" & endDate)
However, if your dates in myDates also include a time, then you can modify the formula slightly:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<" & endDate+1)
And you can use the 2nd formula in either instance
Edit: I just noticed you posting your formula as a comment
I assume you have entered JUST the date (no time) in I1 and J1
Your formula as posted will work UNLESS C:C includes times with the dates. If it does include times with the dates, then try:
=SUMIFS(D:D,E:E,"<>Pending for Payment",B:B,H2,C:C,">="&$I$1,C:C,"<"&$J$1+1)
By adding 1 to the date, we move it to the start of the day after; so we change the <= to just < and encompass the full range of dates you desire.
Related
I have this monthly work report sheet. And I don't know how to make some autofill for column B which will provide a number of a day for a right name of a week day in column A.
I was only able to find formula which gave me date of first work day in first week of the month.
This will take month and year from B2 and B3 cells and generate first
day in month:
=TEXT(DATEVALUE(CONCATENATE(1;B2;B3));"dd.mm.yyyy")
This will find from first day in mnoth the first working day.
=TEXT(WORKDAY(EOMONTH(R2;-1); 1);"dd.mm.yyyy")
But the first problem is that the each next month a first work day starts in different name of a week.
And second problem is that the each week has a "Sum:" after each friday.
Please, can someone provide me any solution via formulas or macro?
This is really poorly written tbh, but maybe you can make use of it.
Basically, just select the cell that has the first day of the month and enter the date:
The code will then use the current date and work its way down your list until it runs into Sum: at the bottom or the current month ends and put in the date using dd.mm format (feel free to change this in the code).
If it finds Sum: to its left, it will add 3 days to its current date.
Otherwise, it will add 1 day to its current date.
Worth noting that it looks for the name of the current month in cell B2
If you want to add a period after the format (like you showed in your screenshot), just add an extra . to the line Format(aDate, "dd.mm")
It would become ActiveCell.Value = Format(aDate, "dd.mm.")
Sub GenerateDates()
Dim aDate
aDate = InputBox("Enter Date of Current Cell", "Enter Date of Current Cell")
If IsDate(aDate) Then
aDate = DateValue(aDate)
Do While ActiveCell.Value = ""
ActiveCell.Value = Format(aDate, "dd.mm")
ActiveCell.Offset(1, 0).Select
If ActiveCell.Offset(0, -1).Value = "Sum:" Then
aDate = DateAdd("d", 3, aDate)
ActiveCell.Offset(1, 0).Select
Else
aDate = DateAdd("d", 1, aDate)
End If
If MonthName(Month(aDate)) <> Range("B2").Value Then Exit Sub
Loop
Else
MsgBox "Invalid Date", vbCritical, "Invalid Date!"
Exit Sub
End If
End Sub
Of course, this could be modified to assume that you select the first of the month on the cell - or it could attempt to figure out the first of the month for you to begin by starting at the top, but this just seemed simple enough.
Display the date in reference to the weekdays
Let's use an Excel formula:
=LET(
WeekRange; A6:A34;
WeekInterval; 6;
ThisMonth; MATCH(B2; {"January";"February";"March";"April";"May";"June";"July";"August";"September";"October";"November";"December"};);
ThisYear; B3;
WeekNames; {"Monday";"Tuesday";"Wednesday";"Thursday";"Friday"};
FirstDay; DATE(ThisYear; ThisMonth; 1);
LastDay; EOMONTH(FirstDay; 0);
FirstWorkDay; WORKDAY(EOMONTH(FirstDay; -1); 1);
WeekShift; WEEKDAY(FirstWorkDay; 2);
WeekInMonth; QUOTIENT(ROW(WeekRange) - INDEX(ROW(WeekRange); 1); WeekInterval);
WorkDays; IFNA(FirstWorkDay - WeekShift + MATCH(WeekRange;WeekNames;0) + 7*WeekInMonth; "");
TEXT(WorkDays; "[<" & FirstDay & "] ;[>" & LastDay & "] ;dd.mm."))
Details:
WeekInterval - the number of cells between the same weekdays
WeekShift - the weekday of the first workday of the month starting from Monday
WeekInMonth - a week number in WeekRange
IFNA(...; "") - put an empty string next to the "Sum:"
"[<FirstDay] ;[>LastDay] ;dd.mm." - display a space when a date is outside the month (keep spaces after closing square brackets ])
I use semicolon ; as a separator due to my locale settings.
This formula has to be placed once next to the first Monday of the WeekRange (in your case in a cell B6). The result will be spilled on weekdays.
I have the following data set in general formatting.
Bunch of years with no day nor months provided.
What I want to achieve, is to display the date in yyyy format.
I made a standard DateSerial function to convert the data to date properly.
Private Function toDate(ByVal text As String) As Date
toDate = DateSerial(Int(text), 1, 1)
End Function
For Each cell in ws.Range("A21:A" & lr)
cell = toDate(cell)
cell.NumberFormat = "yyyy"
Next cell
Now, technically this works, but if you click inside the cell, it
obviously shows the date only as 01-01-year - that is to be expected
with usage of dateserial
But I was wondering, would it be possible given my data to somehow
omit the Days and Months and only display the result as a year?
Basically, I need want to convert the data to a Date data-type without specifying days and months
Entering the years and formatting the date as "YYYY" could be done like this:
Sub TestMe()
With ThisWorkbook.Worksheets(1)
.Range("A1") = 2011
.Range("A2") = 2013
.Range("A3") = 2011
Dim myCell As Range
For Each myCell In .Range("A1:A3")
myCell = DateSerial(myCell, 1, 1)
myCell.NumberFormat = "yyyy"
Next myCell
End With
End Sub
The "trick", is that DateSerial(myCell, 1, 1) takes the year from the cell and adds 1 for day and January for month. Thus, every year is represented as the date 1.January from that year.
More about the Date in VBA and Excel - VBA doesn't return the correct Date with Now()
In my data, the date-time format is 15-02-2019 19:56 in a single column.
I want to separate the date and time separate column.
Note that when using dates/date-times then what Excel shows in the cell is not what Excel actually saves as value in the cell. The value that Excel saves for that date is actually 43511,83056 which is the number of days since 1900-01-01. So time is a part of one day and is therefore represented by the part after the comma of that value.
So you can either use …
=INT(B1) to get only the date (and format the cell as date)
=B1-INT(B1) to get only the time (and format the cell as time)
Or just use the same value as in column B (=B1 in C and D) and just format one as date and one as time.
In VBA you can use the same technique:
Option Explicit
Public Sub SplitDateAndTime()
Dim MyDateTime As Date
MyDateTime = Range("B1").Value
'get date
Range("C1").Value = Int(MyDateTime)
Range("C1").NumberFormat = "YYYY-MM-DD"
'get time
Range("D1").Value = MyDateTime - Int(MyDateTime)
Range("D1").NumberFormat = "hh:mm:ss"
End Sub
Assuming your date and time in cell A1,
use the below formula in B1
=--TEXT(A1,"dd/MMM/yyyy")
and use the below formula in C1
=--TEXT(A1,"hh:mm:ss")
and select B1 and C1 and customize your date and time format.
I have to write a vba code that convert all dates in a year into days in a week eg. Monday, Tuesday, Wednesday....Sunday and the value of all days in a week eg.Monday must be in a string format. (See image inserted)
I tried using the .NumberFormat function but it does not work since the format is still under the date format although the values displayed are correct (please look at the formula bar of F10). Below is the code I have written:
Sub convertdate()
Range("A7:A371").Copy
'copy all the dates throughout the year'
Range("F7:F371").PasteSpecial xlPasteFormulasAndNumberFormats
'paste it into F column'
Worksheets("Sheet1").Range("F7:F371").NumberFormat = "dddd"
'convert the dates into days'
Sum1 = Application.SumIf(Range("F7:F371"), "Monday", Range("B7:B371"))
'example of calculating the total rainfall of all Mondays throughout the year'
Worksheets("Sheet1").Range("G22").FormulaArray = Sum1
End Sub
The formula bar from cell F7:F371 should display the string value "Monday","Tuesday" etc depending on the dates rather than the date itself.The reason of converting it into a string is so that it could be use in a SUMIF function later on to calculate the total rainfall of all Mondays,Tuesday etc.
Appreciate if anyone could help. Thank you.
A formula can do this.
In cell F7 enter =TEXT(A7,"dddd") and drag down.
This will format the date to show the full day name as a string.
https://support.office.com/en-us/article/TEXT-function-20d5ac4d-7b94-49fd-bb38-93d29371225c
Try something like this. It will loop through all the dates in Column A (starting in row 7) and put the associated Weekday name in Column F:
Sub test()
Dim i As Long
Dim lRow As Long
With ActiveSheet
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 7 To lRow
.Cells(i, 6).Value = WeekdayName(Weekday(.Cells(i, 1).Value, 1), False, 1)
Next i
End With
End Sub
I'm having trouble modifying the time to a specific time in a cell with both date and time with VBA.
The cell is formatted as "d/mm/yyyy h:mm AM/PM" and I'm taking a cell with time only in it formatted "h:mm AM/PM" and trying to use this bit of code to update the time portion (t1 is used thoughout the rest of the sub..)
Public t1 As String
t1 = Range("H1").Value
Range("D3").Select
ActiveCell = DateValue(ActiveCell) & " " & TimeValue(t1)
This seems to work on and off.. 6AM for instance will update the time to 12:25, and 10AM flat-out wont work at all. I figure I'm just doing something wrong but I've searched up and down for this and it seems like it's not a common problem.. Does anyone have any better ideas?! This is driving me nuts
The value in cell H1 does change, so i do need to pull the value from that in one way or another.
You mentioned the cell formatting; note that the cell formatting doesn't matter when you are merely retrieving the value as in: dt = ActiveCell
Note also that it is possible to store a date as text =istext(H1) returns true. In this case changing the formatting won't make any difference. Or you can store it as a value in the usual fashion. =isNumber(H1) returns true. Either way won't make any difference when using DateValue("H1")
I suggest declaring dt and tm as dates so that if the assignment dt = Range("H1").Value succeeds you know you actually have a date. Then you won't get a mismatch error when adding the dateValue + timeValue.
Try this
Dim dt As Date
Dim tm as Date
dt = Range("H1").Value
tm = ActiveCell.value
Range("D3").value = DateValue(dt) + TimeValue(tm) 'returns a date/time as a value
Maybe something like this:
Sub Macro1()
t1 = Range("H1").Value
Range("D3").Select
ActiveCell.Value = ActiveCell + t1
End Sub