+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    11-18-2009
    Location
    Belgrade, Serbia
    MS-Off Ver
    Excel 2007
    Posts
    9

    VBA code for search with two criteria

    Hi friends,

    I have big problem, please help me. I am new in excel vba and I am learning.
    This is the problem:
    I have sheet like this:

    NumID NumName NumPrice
    12 klo1
    14 pri23
    15 edi31
    19 vru5
    25 klo1
    And in VBA I made user form with three textboxes and one button. Now I have to find row in which in column A is something what I enter in textbox1 and in column B is something that I enter in textbox2 (in a same time). In textbox3 I have to enter value which I want to place in column C in row which where is in column A value from textbox1 and in column B value from textbox2. After that I have to click on button and the value have to be entered in specific row which in column A and B fulfil values from textbox1 and 2.

    Please help me. I need that code. I try with:

    Code:
    Private Sub CommandButton1_Click()
        Dim FindString As String
        Dim rng As Range
    
        FindString = p1.Value
        
        With Sheets("Sheet1").Range("A:A")
            Set rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
            
            FindString = p2.Value
        
        With Sheets("Sheet1").Range("b:b")
            Set rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
                rng.Offset(0, 1).Value = p3.Value
            Else
                MsgBox "Nothing found"
            End If
        End With
        End If
        End With
    End Sub
    But I didn’t make it.
    Here are is an example
    In textbox1 I enter 25
    In textbox2 I enter klo1
    In textbox3 I enter 100
    And click, the result have to be like this:

    NumID NumName NumPrice
    12 klo1
    14 pri23
    15 edi31
    19 vru5
    25 klo1 100

    Could someone help me please?

    One more time, I want when I enter values in userform in textbox1,2,3 (and click the button) to find the row which in column A have value from textbox1 and in column B have textbox2 and in that row (which has claimed values) in column C fill value from textbox3.
    Last edited by auber; 11-18-2009 at 10:42 AM. Reason: mistake

  2. #2
    Registered User
    Join Date
    11-18-2009
    Location
    Belgrade, Serbia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VBA code for search with two criteria

    Sorry, my mistake, i will have put tags around code now.

  3. #3
    Forum Guru
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2003
    Posts
    2,286

    Re: VBA code for search with two criteria

    AAAAAAAAAAARAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaagrgarggrggrgraghaghr
    What lovely code.

    Use autofilter.

    Record a macro of applying autofilter, then specifying a criterion for columns A and B, then simply use .end(xlup/down) type statements to overwrite column C with the appropriate variable.

    This will be about a gazillion times faster - roughly

    CC

  4. #4
    Registered User
    Join Date
    11-18-2009
    Location
    Belgrade, Serbia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VBA code for search with two criteria

    Than you that was superb hint. It helps me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0