incrementing and storing a result without using cell

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
556
Office Version
  1. 2016
Platform
  1. Windows
I have a macro which searches a wb based on a date and returns the value from column b and the row the date is located in.

I need to run this several times and at present am doing it by writing the following for each occasion and renaming "Ans" each time. The DteRng remains constant

DteLookup = Format(DateAdd("d", 1, Now()), "dd/mm/yyyy")
set DteRng = ws.Range("a5:a" & lrow)

Set Ans= DteRng.Find(What:=DteLookup, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

Would there be a way to put this in a loop and store each "Ans" so that it incremented by 1 eg on the first run it would be Ans1 on the 2nd Ans2 and so on storing them until i need to reference them later in the macro (without storing them temporarily in the sheet)
Something like

for i = 1 to 10
DteLookup = Format(DateAdd("d", (i), Now()), "dd/mm/yyyy")
set DteRng = ws.Range("a5:a" & lrow)

Set Ans(i)= DteRng.Find(What:=DteLookup, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
next i

Ive tried the above without sucess.

Thanks in advance for any advice / pointers you can give
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You could store your returns in an array; for example, by declaring ans as an array with 10 rows...

VBA Code:
Dim ans(10) As Variant

For i = 1 To 10
    DteLookup = Format(DateAdd("d", (i), Now()), "dd/mm/yyyy")
    Set DteRng = ws.Range("a5:a" & lrow)
    Set ans(i) = DteRng.Find(What:=DteLookup, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
Next i

For i = 1 To 10
    Debug.Print ans(i)
Next i

Cheers,

Tony
 
Upvote 0
Solution
Thanks Tonyyy exactly what I needed. I was just missing the numbers in the Dim.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,037
Members
449,062
Latest member
mike575

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top