I have a databes of dates with start date and end date.
I want to count only month beetween year 01/2005 and 01/2006.
e.g. from DB: Start 09/2004 end 05/2005, so I need only months from 01/2005 to 05/2005.
I tried some if and lookup Excel funkctions but didn't work.
Can somebody help?
https://ibb.co/jXJHYQ
Assuming a start date is on cell F10 and the end date is on cell F11, you can get the number of months by typing this into any cell:
"=(YEAR(F11)-YEAR(F10))*12+MONTH(F11)-MONTH(F10)"
Related
I just want to create list of months automatically between start and end dates in which if the starting date or the end date have only dates then it comes as date from to in excel.
If A1 contains the starting date, and A2 is the enddate, then
=TEXT(IF(EDATE($A$1,ROW() - 5) <= $A$2, EDATE($A$1,ROW() - 5), ""), "mmmyy")
does what you ask for. Just apply it for sufficiently many cells.
I have a column with dates that are stored as d.m.yyyy. h:mm:ss.
And the cell format isn't even date it's general.
To use it as a date I need to replace that dot with "".
So I used this code:
Format(Date, "d.m.yyyy h:mm:ss")
But it changed all dates to todays date.
The call of Date returns the current date. You have to use the cell value instead, like Format(Cells(row,column), "d.m.yyyy h:mm:ss"), after you replaced the dot at the end of the year value.
I am trying to filter data in a pivot table. I need to set Monday as the first day of the week. The default is Sunday. Do you know how to set it?
Edit1: I am trying to do dynamic filtering using THIS WEEK. As the default is Sunday, the result is not what I exactly expect.
Edit2: Current solution using VBA. I am still looking for a neater solution.
Sub YmThisWeek()
'
' YmFinal Macro
'
'
Dim startDate, endDate As Date
endDate = Date
startDate = endDate - 7 ' 7 DAYS IN A WEEK
'Pick the star of the week
'vbSunday 1 Sunday (default used)
'vbMonday 2 Monday
'vbTuesday 3 Tuesday
'vbWednesday 4 Wednesday
'vbThursday 5 Thursday
'vbFriday 6 Friday
'vbSaturday 7 Saturday
Do While (startDate < endDate - 1) And Not Weekday(startDate) = 2 '<- week start day
startDate = startDate + 1
Loop
ActiveSheet.PivotTables("pvtTbl").PivotFields("Date").ClearAllFilters
ActiveSheet.PivotTables("pvtTbl").PivotFields("Date").PivotFilters.Add Type:= _
xlDateBetween, Value1:=Format(startDate, "dd/mm/yyyy"), Value2:=Format(endDate, "dd/mm/yyyy")
End Sub
Good question. And I don't have the perfect answer.
But this workaround might get you going.
Click on 'Sunday'
Drag it to the end of the Pivot table
You could do this with VBA. But a NON-VBA method you could use involves adding an extra column to your data. Label it THIS WEEK. Then populate that with a formula that returns TRUE or FALSE depending on your definition of the week. For example, if THIS WEEK is from the most recent Monday to the following Saturday, compared with a date stored in cell F1, then the formula would be:
=AND(A2>=($F$1+1-WEEKDAY($F$1-1)),A2<=($F$1+7-WEEKDAY($F$1-1)))
I used a cell reference for testing purposes, and I would advise you to do the same until you have things working the way you want. You can subsequently either change your cell so it contains =TODAY(), or you can substitute TODAY() in the formula.
You would then drag THIS WEEK to the filter area and filter on TRUE Whenever you Refresh your Pivot Table, the filter will update.
I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy
But, when I try to do formulas based on the dates, something is clearly amiss.
For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.
For each record I tried a formula to parse it apart.=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
That did not help.
I also selected the column and switched it from general to date format
I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run =year(right(A1,4)) I get 1903 instead of 2013. When I run =date(right(A1,4),mid(A1,3,2),left(A1,2)) the formula returns 2/10/3192
It's very simple why your formulas doesn't work corectly. When yor're using somehting like this: RIGHT(A2,4), your value from A2 translates to 41579 for 1/11/2013 (Excel stores all dates as integers and all times as decimal fractions. You can read more here). Next formula should work well:
=DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy"),4,2),LEFT(TEXT(A2,"dd/mm/yyyy"),2))
Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column):
=A2*1
and drag it down. Then copy values from this temp column and paste them using "Paste special->Values" in colunm A (where should be date format as well)
Or you can use this simple macro:
Sub test()
With Range("A2:A100")
.NumberFormat = "dd/mm/yyyy"
.Value = .Value
End With
End Sub
I have a macro set up that will clear content on a spreadsheet. At the end of this Macro, I want to select specific cells that already have dates in them and then replace the current dates with current date +1. After searching the web I found the DateAdd function, but I am pretty new to VBA and I am having difficulty writing the function correctly. After selecting the necessary cells, how would I go about changing the dates to the next day?
Taking your question literally, you could do this:
' Here goes the code where you select the date cells you want to increment
' ...
' Now increment them by 1 day:
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + 1 ' adds 1 day
Next cell
The unit of the Date data type is a day. So adding 1 adds one day.
Here's an example of how to use DateAdd:
Range("A1").value = DateAdd("d", 1, CDate(Range("A1")))
This, of course, assumes a valid date is in A1. It will increment that date by a single day.
"d" means we are adding a day. Here are the other intervals for adding years, months, etc.
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week
h - Hour
n - Minute
s - Second
Notice I use CDate. That simply converts the value of range("a1") into a date. It will throw an error if A1 cannot be parsed as a date.
Of course, you can also use this method to subtract days:
Range("A1").value = DateAdd("d", -3, CDate(Range("A1")))
This subtracts three days for the date in A1.
If you have date in cell a1 and want increment one day, it should be as simple as
range("a1").value = range("a1").value + 1