As mentioned in the comments, this doesn't require VBA you can leverage Excel Names and Power Query directly.
Steps:
- Add cell names to
from
and to
dates in Excel
- Add the from date to a cell and add a name to that cell:
fromDate
- Add the to date to a cell and add a name to that cell:
toDate
- Add those cells to power query
- Select the cell and click Data | From table/range
- Drill down to the first cell (right click)
- Use fromDate and toDate queries in the table date column filter
- You can write the code or use the interface and adapt it: =
Table.SelectRows(#"Changed Type", each ([Date] >= fromDate and [Date] <= toDate))
M code:
Table1
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Date] >= fromDate and [Date] <= toDate))
in
#"Filtered Rows"
fromDate
let
Source = Excel.CurrentWorkbook(){[Name="fromDate"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
Column1 = #"Changed Type"{0}[Column1]
in
Column1
toDate
let
Source = Excel.CurrentWorkbook(){[Name="toDate"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
Column1 = #"Changed Type"{0}[Column1]
in
Column1
Table1 Query
fromDate Query
Check the screencast
Let me know if it works