[VBA] why is my match function not looping through correctly?

nidenikolev

New Member
Joined
Jun 6, 2018
Messages
20
I'm trying to locate a row # with data I need to update based on two text boxes within a userform. Code is here:
VBA Code:
    Private Sub cmdSendData_Click()
    
        Set wb = Workbooks.Open("\\TABLE.xlsx")
        Dim wsTgt As Worksheet: Set wsTgt = wb.Worksheets("Sheet1")
        Dim recRow As Range
    
        'See if there's a match on an existing row
        '  adjust function to suit...
        Set recRow = MatchRow(wsTgt.Range("A1").CurrentRegion, _
                              txtStore.Text, _
                              txtCandidateName.Text)
    
        'If there's no existing row to update then add a new row at the bottom
        If recRow Is Nothing Then Set recRow = wsTgt.Range("A50000").End(xlUp).Offset(1, 0)
    
        With recRow.EntireRow
            .Cells(1).Value = Me.txtTodays_Date.Text
            '....
            .Cells(33).Value = Me.txtMgrJustification.Text
        End With
            
        
        'wb.Close savechanges:=True
        'wb.Saved = True
            
        End Sub
        
        'Return a row from a table based on matches in two columns
        '   returns nothing if no match
        Function MatchRow(tableRange As Range, match1, match2) As Range
            Dim rw As Range
            For Each rw In tableRange.Rows
                'adjust the column numbers/match types as needed
                If rw.Cells(4).Value = match1 Then
                    If rw.Cells(17).Value = match2 Then
                        Set MatchRow = rw
                        Exit Function
                    End If
                End If
            Next rw
        End Function

in my data, store and candidate name are found in row 6, but the function keeps looping through and not matching to row 6. What am I missing here?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Perhaps a subtle mismatch in the data, extra spaces in the table that are not in the text submitted to the function, different case (your function is case sensitive).
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,058
Members
449,206
Latest member
Healthydogs

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