Solved

Modifying VBA Search Code

Posted on 2011-03-21
2
356 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
ID: 35182763
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
ID: 35188619
Awesome! Cheers Pr1z!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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 and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

820 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