Solved

Modifying VBA Search Code

Posted on 2011-03-21
2
348 Views
Last Modified: 2013-11-27
I have the following code in use on a userform within my workbook.

Basically it searches by a particular column for a numeric value and then pouplates the userform with the contents of that row.

Private Sub cmdSearch_Click()
   Dim rw As Long             'row the search value was found on
   Dim myVal As Long          'converted search value
 
   On Error Resume Next
      'validate user input
      If txtSearch.Value = "" Or _
         Not IsNumeric(CLng(txtSearch.Value)) Then
         MsgBox "Enter valid SIR number!"
         txtSearch.SetFocus
         Exit Sub
      End If
 
      'convert the TexBox value to  data type long and get row number
      myVal = CLng(txtSearch.Value)
      On Error Resume Next
 
       With Sheets("SIRs").Range("C:C")
         Set cell = .Find(myVal, LookIn:=xlValues)
         rw = cell.Row
       End With
   On Error GoTo 0

   On Error GoTo 0
   If rw = 0 Then
      MsgBox "SIR Not Found"
   Else
      Call PopulateTextBoxes(rw)
   End If
End Sub

Open in new window


I now want to create a new search function whereby it searches another column (E) but for a non-numeric value this time. Naively I assumed I could copy the above code, tailor it for column E and remove the following piece of code

Or _
         Not IsNumeric(CLng(txtSearch.Value)) 

Open in new window


Unfortunately it keeps returning a 'SIR not Found' result.

can anyone help please?
0
Comment
Question by:laffs_efc
2 Comments
 
LVL 7

Accepted Solution

by:
Pr1z earned 125 total points
Comment Utility
Line 3 defines a data type of Long ==> change that to string
Line 15 is a cast to a long numeric data type ==> use cstr() or simply(trim()

Hope that helps

Priz
0
 

Author Comment

by:laffs_efc
Comment Utility
Awesome! Cheers Pr1z!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now