r/excel • u/WontonCardboard • 1d ago
solved VBA Index table by date offset.
Attempting to make a function that will Index lookup by specified date offset but I keep running into Value error. My employees are to add new rows to table each day to do data entry. I need this function in order to scrape specific rolling date windows for averages. t
Public Function DEXDOFF(array1 As ListObjects, arraydc As ListObjects, offset As Double, column As Double) As Double
'Function to index table by specified number of days offeset from present
DEXDOFF = Application.WorksheetFunction.Index(array1, Application.WorksheetFunction.Match(offset, arraydc, 0), column)
End Function
Typically input looks like =DEXDOFF('Table1[#All],'Table1[[#All],[Date]],(Today()-Day(offset),'column')
1
u/terminate18 2 1d ago
Option Explicit
Public Function DEXDOFF(tbl As Range, dateCol As Range, daysBack As Long, colNum As Long) As Variant
Dim r As Variant
Application.Volatile
r = Application.Match(CLng(Date) - daysBack, dateCol, 0)
If IsError(r) Then
DEXDOFF = CVErr(xlErrNA)
Else
DEXDOFF = Application.Index(tbl, r, colNum)
End If
End Function
Honestly though, if you’re after rolling averages you don’t need VBA at all:
=AVERAGEIFS(Table1[Amount], Table1[Date], ">="&TODAY()-7, Table1[Date], "<"&TODAY())
That gives you the last 7 days (not counting today), and the file can stay a normal xlsx. For a single lookup, =XLOOKUP(TODAY()-7, Table1[Date], Table1[Amount], "") does the same thing as the UDF.
1
u/WontonCardboard 1d ago
Solution Verified
1
u/reputatorbot 1d ago
You have awarded 1 point to terminate18.
I am a bot - please contact the mods with any questions
1
u/Decronym 1d ago edited 1d ago
Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread:
Decronym is now also available on Lemmy! Requests for support and new installations should be directed to the Contact address below.
Beep-boop, I am a helper bot. Please do not verify me as a solution.
3 acronyms in this thread; the most compressed thread commented on today has 16 acronyms.
[Thread #49551 for this sub, first seen 20th Oct 2026, 20:12]
[FAQ] [Full list] [Contact] [Source code]
•
u/AutoModerator 1d ago
/u/WontonCardboard - Your post was submitted successfully.
Solution Verifiedto close the thread.Failing to follow these steps may result in your post being removed without warning.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.