Searching for value using InputBox

Steve41553

New Member
Joined
Apr 8, 2014
Messages
27
I’m trying to search for a specific value in a list.This code looks for zeroes and it works
Code:
Selection.Find(What:="0.00 ", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
This code asks for a specific value and generates error 91:
Code:
f_val# = InputBox(prompt:="Enter value to search for.")
'    Application.FindFormat.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
'    Cells.Find(What:=f_val#, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=True).Activate
I’ve tried removing the find number format line and entering a specific number into the code instead of the variable f_val#. Grateful for suggestions.
 
Last edited by a moderator:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You could create a variable (remember to declare the variable correctly) then have something like:
InputData = InputBox("Enter value to search for")

Then use the variable (InputData) in your existing code (i.e. replace the What:="0.00 " with What:= InputData
 
Upvote 0
Using Find with formatted cells, especially ones that pad the cell contents, is extremely troublesome when you specify to look in values, rather than formulas, since it looks for the displayed value, which in this case will have leading and trailing spaces (and the number of those will vary based on column width for the same number!) If you can, look in formulas instead of values, or use a partial cell match rather than a whole cell match. If you can't do any of that, I'd suggest using something like Match that isn't dependent on the display.
 
Upvote 0
I'm posting this reply from home. Maybe members can read this one. I blame the systems at work.



Thanks for the replies. RoryA's suggestion gave me what I needed. I removed the formatting, searched and then reinstated it.

Code:
Columns("S:S").Select    


Selection.NumberFormat = "General"

'

f_val# = InputBox(prompt:="Enter value to search for.")

'

Cells.Find(What:=f_val#, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

'   

Columns("S:S").NumberFormat = "#,##0.00_);[Red](#,##0.00)"

I shall need to select column S and then format it (two lines of code) rather than saying Columns("S:S").NumberFormat = "General" so that I'm not searching in wrongly formatted parts of the sheet.
 
Last edited by a moderator:
Upvote 0
I'm posting this reply from home. Maybe members can read this one. I blame the systems at work.

If you use IE at work, try setting it to use compatibility mode for this site.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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