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.
Related
I have a financial sheet and I want to record my monthly final balance each month but automatically. This is my sheet:
I tried this code:
Public Sub DATA()
If Today > 31 / 3 / 2021 Then
Range("L10").Value = Range("I16").Value
Else
End If
End Sub
The Idea is to copy/paste a value from one cell to another only in the last day of the month and keep this value there forever. For example:
at 31/03/2021, I would run the macro and it would copy/paste the final balance of this month to the "Março" cell in the right, and this value would stay there.
I'm new to VBA so, can anybody tell me what I'm doing wrong? Thanks!
From comments:
what I'm trying to do is to copy the value of an specific cell to another one, only when the day is the last of the month.
Use WorksheetFunction.EoMonth with 0 as the second argument:
If Date = WorksheetFunction.EoMonth(Date, 0) Then
Range("L10").Value = Range("I16").Value
End If
I have a refreshable table in excel and I want to filter the rows by a couple of date ranges. Each row has a date and other information.
I want to find the rows that are in the first date range (F1:F2) and are not in the second date range (H1:H2).
The table is refreshable, and can change size. It currently spans A3:X6146. The table is a query, so it will change sizes when a separate date range is used to find the table values.
I don't have much VBA experience, so this problem is tripping me up. Any ideas?
Thanks
EDIT:
I'll try to make the issue clearer.
I have a table that is created via a query that pulls in data that falls between the Starting Date and the Ending Date, 1/1/2016 and 12/31/2017 here. It lists each time an item was purchased, so each one can be listed multiple times.
I want to find which items were purchased (listed in the table) between the Active Date Range start and end dates (cells F1 and F2), and NOT purchased between the Inactive Date range (cells H1 and H2).
Starting Date: 1/1/2016 Active Date Range Start: 3/1/2016 Inactive Date Start: 3/2/2017
Ending Date: 12/31/2017 Active Date Range End: 3/1/2017 Inactive Date End: 9/22/2017
item date
1 9/21/2017
2 9/20/2017
3 9/20/2017
Yes, I can say to you what I would do.
Create one additional column to keep the result value, if is in or out of the date range. Then
dim t() as string, lin as long, linf as long
linf=Range("F65536").End(xlUp).Row 'or any other more precise way to get the final line
redim t(1 to linf-2,1 to 1)
'range dates - are they on the worksheet?
dim rg_dates as Range,r as range,b as boolean
set rg_dates=Sheets("xxxx").Range("B1:B4") ' just an example, the range would be B1:C4 but use only the first column - see below
For lin=3 to linf
b=False
For each r in rg_dates
If cells(lin,"F").value>= r.Cells(1,1) and cells(lin,"G").Value<=r.Cells(1,2).value then
b=true
Exit for
End If
Next r
If b then t(lin-2,1)="Y" else t(lin-2,1)="N"
Next l
Range("Z3:Z" & linf).Value = T
'Then just filter the table.
There would be then many things to do to keep it error free, and how to apply it at the concrete situation. Hopefully with what I wrote above you can get an idea about things you can do using VBA. If you are using code to filter the table you can do all this invisible to the user, creating an extra column for the filter criteria, filtering, and then deleting the whole column..
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)"
I have a table of values matched to fortnightly dates like this:
1/1/17 3123
15/1/17 3422
29/1/17 3645
12/2/17 3941
and I want to extract values to a new table for the end of each month, taking the value of the last date in that month if not available.
eg.
31/1/17 3645
Any ideas on how to do this?
You can use the MATCH function, like this.
Private Sub TestMatch()
Dim Dt As Date
Dt = CDate("1/5/16")
Debug.Print WorksheetFunction.Match(CLng(Dt), Range("Dates"), 1)
End Sub
Where "Dates" is a column of dates sorted in ascending order, and Dt is the first day of the month following the month of the date of which you wish to extract the last available day.
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