Find Value in Column (Text and Numbers)

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

Trying to find a Value in a Column using code below

The column contains text, numbers that are actually text and some numbers. Need it to find Text, find numbers if they are text and numbers
Also need it to show the value to the right of it in col D in a message box

VBA Code:
  If Not IsError(Application.Match(valSearch, ws.Range("B:B"), 0)) Then
    MsgBox "Found"
    Exit Sub
  End If
    MsgBox "Not Found"
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try using the range find method instead of application match
VBA Code:
Dim ws As Worksheet, rng As Range, valsearch As Variant
valsearch = 345
Set ws = Worksheets("Sheet2")
Set rng = ws.Range("B:B").Find(valsearch, , xlValues, xlWhole, , , False, False, False)
If Not rng Is Nothing Then
    MsgBox "found"
    Exit Sub
Else
    MsgBox "not found"
End If
 
Upvote 0
Hi

Trying to find a Value in a Column using code below

The column contains text, numbers that are actually text and some numbers. Need it to find Text, find numbers if they are text and numbers
Also need it to show the value to the right of it in col D in a message box

Hi,
see if this update to your code does what you want

VBA Code:
Dim valSearch   As Variant, m As Variant

    valSearch = "1234"
    
    If IsNumeric(valSearch) Then valSearch = Val(valSearch)
    m = Application.Match(valSearch, ws.Range("B:B"), 0)
    
    If Not IsError(m) Then
        MsgBox ws.Cells(CLng(m), "D").Value & Chr(10) & "Found"
    Else
        MsgBox "Not Found"
    End If

Dave
 
Upvote 0
Try using the range find method instead of application match
VBA Code:
Dim ws As Worksheet, rng As Range, valsearch As Variant
valsearch = 345
Set ws = Worksheets("Sheet2")
Set rng = ws.Range("B:B").Find(valsearch, , xlValues, xlWhole, , , False, False, False)
If Not rng Is Nothing Then
    MsgBox "found"
    Exit Sub
Else
    MsgBox "not found"
End If
Thanks, Just need to show the Value in D if the Value is found

Should it make a difference if valsearch = 345 or valsearch = "345"
Also Does it make a difference if I changed to as String instead of as Variant as that seemed to work as well
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Dim valSearch   As Variant, m As Variant

    valSearch = "1234"
   
    If IsNumeric(valSearch) Then valSearch = Val(valSearch)
    m = Application.Match(valSearch, ws.Range("B:B"), 0)
   
    If Not IsError(m) Then
        MsgBox ws.Cells(CLng(m), "D").Value & Chr(10) & "Found"
    Else
        MsgBox "Not Found"
    End If

Dave

Hi

Doesnt seem to work if the value appears as a number but is actually text
 
Upvote 0
Sorry, I missed the bit about adding D to the message box
Excel Formula:
Dim ws As Worksheet, rng As Range, valsearch As Variant
valsearch = 345
Set ws = Worksheets("Sheet2")
Set rng = ws.Range("B:B").Find(valsearch, , xlValues, xlWhole, , , False, False, False)
If Not rng Is Nothing Then
    MsgBox rng.Offset(, 2).Value & Chr(10) & "found"
    Exit Sub
Else
    MsgBox "not found"
End If
I would personally leave the declaration as variant. It should work with string, but there may be exceptions that are not coming to mind. Variant should allow for all possibilities.
 
Upvote 0
Solution
Sorry, I missed the bit about adding D to the message box
Excel Formula:
Dim ws As Worksheet, rng As Range, valsearch As Variant
valsearch = 345
Set ws = Worksheets("Sheet2")
Set rng = ws.Range("B:B").Find(valsearch, , xlValues, xlWhole, , , False, False, False)
If Not rng Is Nothing Then
    MsgBox rng.Offset(, 2).Value & Chr(10) & "found"
    Exit Sub
Else
    MsgBox "not found"
End If
I would personally leave the declaration as variant. It should work with string, but there may be exceptions that are not coming to mind. Variant should allow for all possibilities.
Thanks for the explanation and update :)
 
Upvote 0
Hello, does anyone have a good way to do this conditional with function:

(if text "Lead" is found on cell G2, then sum X2+1, (if text "Field", is found on cell G2, then B2+1.5, B2) )

trying to do this formula in one cell and copy it down to multiple on a raw datasheet

Thanks


excell sample data.png
 
Upvote 0
Hello, does anyone have a good way to do this conditional with function:

(if text "Lead" is found on cell G2, then sum X2+1, (if text "Field", is found on cell G2, then B2+1.5, B2) )

trying to do this formula in one cell and copy it down to multiple on a raw datasheet

Thanks


View attachment 51114
Oh and if the job title includes the words "lead" and "Field" then column X= 2.5 , based on logic above
 
Upvote 0
@Caliexceldude as this is a totally different question to the op, please start a thread of your own. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,086
Members
448,944
Latest member
sharmarick

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