Solved

Modifying VBA Search Code

Posted on 2011-03-21
2
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

724 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