Creating an array up till the search value

miisstina

New Member
Joined
Sep 27, 2014
Messages
10
Hello,

Is there a way to create an array up until a search value (so the value someone puts into an input box).
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Vaguely speaking, yes. I would need a lot more information as to what you are trying to do though to give any specifics.
 
Upvote 0
Vaguely speaking, yes. I would need a lot more information as to what you are trying to do though to give any specifics.

So i have days of the week in one column and prices right besides it. I iniatilly have to search for a what day of the week i am searching for. Once that day is found, the code needs to be able to find the maximum price up to and including that day of the week and display that in a message box. I can do the first part, but I just dont understand how to find the maximum price.... I hope my explanation is sufficient.
 
Upvote 0
I believe something like this should do it.

Code:
Sub MaxinRange()
Dim LookupRange As Range
Dim ValRange As Range
    'this is just a sample of how I am setting the custom Date to search for
    Do
    userInp = InputBox("Enter the date to search for")
    If IsDate(userInp) Then
        userDate = DateValue(userInp)
    Else
        MsgBox userInp & " does not appear to be a valid date", vbExclamation, "Please re-enter"
    End If
    Loop Until userDate <> 0
    Set LookupRange = Range("A2:A200")  'the range of dates (however you are setting it)
    
    myR = 0
    On Error Resume Next
    myR = LookupRange.Find(userDate).Row 'the row of userDate
    On Error GoTo 0
    
    If myR = 0 Then 'find failed
        MsgBox "The date " & userDate & " was not found.", vbCritical, "Error"
        Exit Sub
    End If
    
    Set ValRange = Range(Cells(LookupRange.Row, LookupRange.Column + 1), Cells(myR, LookupRange.Column + 1))
    
    MsgBox WorksheetFunction.Max(ValRange)
    
End Sub

NOTE: If you meant that the price beside the date to the LEFT inside of to the RIGHT, change
Set ValRange = Range(Cells(LookupRange.Row, LookupRange.Column + 1), Cells(myR, LookupRange.Column + 1))

to

Set ValRange = Range(Cells(LookupRange.Row, LookupRange.Column - 1), Cells(myR, LookupRange.Column - 1))
 
Upvote 0
It is a variable that holds a number which represents the ROW where your search criteria is found.
If your search criteria is not found, it remains defaulted to 0

It is set in this line of code
Code:
myR = LookupRange.Find(userDate).Row 'the row of userDate
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,231
Members
448,951
Latest member
jennlynn

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