How to isolate a specific row on a sheet using three variables?

chipsworld

Board Regular
Joined
May 23, 2019
Messages
164
Office Version
  1. 365
Good day all,
Need your help once again.
I found the base code from the below on the web and thought it would work, but apparently I need some morre help.

The original code worked great for two variables, but in order to do what I need to do, I need a third.

The issue is if the second variable occurs more than once, it will always go to that instance and not look for another one.

Here is what I have...I'm sure someone out there can make sense of it.
The parts in red are my add in attempt to include a third variable.
As you can probably tell, the whole idea is to capture the date and time someone returns an item and put that date/time into the line where they checked it out.

The code works beautifully without my changes if the item being checked out is only checked out once. ITs that second, third, etc that kill it...

VBA Code:
Private Sub INBOUND()
Dim rngFound As Range
Dim strFirst As String
Dim strNam As String
Dim strAsset As String
Dim rw As Long
Dim timestmp As String
Dim ws As Worksheet
Dim strtimo As String

    
    Set ws = ThisWorkbook.Worksheets("ASSET Loging")
    timestmp = Now
    strNam = Me.txtnam.Value
    strAsset = Me.cmbasstype.Value
    strtimo = Me.txtdat.Value
    
    With ws
    Set rngFound = .Columns("C").Find(strNam, Cells(Rows.Count, "C"), xlValues, xlWhole)
    If Not rngFound Is Nothing Then
        strFirst = rngFound.Address
        
        Do
            If LCase(.Cells(rngFound.Row, "A").Text) = LCase(strAsset) [B][COLOR=rgb(226, 80, 65)]And LCase(.Cells(rngFound.Row, "E").Text) = LCase(strtimo) [/COLOR][/B]Then
                
                        'Found a match
                        rw = rngFound.Row
                        If ws.Cells(rw, "F").Value = "" Then
                        ws.Cells(rw, "F").Value = timestmp
                        Else: MsgBox "This item has already been marked as returned!", vbOKOnly
                        Exit Sub
                        End If

            End If
            Set rngFound = .Columns("C").Find(strNam, rngFound, xlValues, xlWhole)
        Loop While rngFound.Address <> strFirst
    End If
    End With
    
    MsgBox "Item has been marked as received!", vbOKOnly
    
    Set rngFound = Nothing

End Sub
 
Not able to test it but this is what I came up with.
Two comparisons take place. If the value of column A matches variable strAsset AND the date&time in column E is equal or less than date&time in variable dtImo, then there is a final match.

VBA Code:
Private Sub INBOUND()
    Dim rngFound    As Range
    Dim strFirst    As String
    Dim strNam      As String
    Dim strAsset    As String
    Dim rw          As Long
    Dim timestmp    As Date
    Dim ws          As Worksheet
    Dim dtImo       As Date

    Set ws = ThisWorkbook.Worksheets("ASSET Loging")

    timestmp = Now
    strNam = Me.txtnam.Value
    strAsset = Me.cmbasstype.Value
    dtImo = CDate(Me.txtdat.Value)

    With ws
        Set rngFound = .Columns("C").Find(strNam, .Cells(.Rows.Count, "C"), xlValues, xlWhole)
        If Not rngFound Is Nothing Then

            strFirst = rngFound.Address
            Do
                If StrComp(rngFound.Offset(0, -2).Text, strAsset, vbTextCompare) = 0 And _
                           rngFound.Offset(0, 2).Value2 <= dtImo Then

                    'Found a match
                    rw = rngFound.Row
                    If .Cells(rw, "F").Value = "" Then
                        .Cells(rw, "F").Value = timestmp
                    Else
                        MsgBox "This item has already been marked as returned!", vbOKOnly
                        Exit Sub
                    End If

                End If
                Set rngFound = .Columns("C").FindNext(rngFound)
            Loop While rngFound.Address <> strFirst
        End If
    End With

    MsgBox "Item has been marked as received!", vbOKOnly

    Set rngFound = Nothing
End Sub
 
Upvote 0
Solution

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Not able to test it but this is what I came up with.
Two comparisons take place. If the value of column A matches variable strAsset AND the date&time in column E is equal or less than date&time in variable dtImo, then there is a final match.

VBA Code:
Private Sub INBOUND()
    Dim rngFound    As Range
    Dim strFirst    As String
    Dim strNam      As String
    Dim strAsset    As String
    Dim rw          As Long
    Dim timestmp    As Date
    Dim ws          As Worksheet
    Dim dtImo       As Date

    Set ws = ThisWorkbook.Worksheets("ASSET Loging")

    timestmp = Now
    strNam = Me.txtnam.Value
    strAsset = Me.cmbasstype.Value
    dtImo = CDate(Me.txtdat.Value)

    With ws
        Set rngFound = .Columns("C").Find(strNam, .Cells(.Rows.Count, "C"), xlValues, xlWhole)
        If Not rngFound Is Nothing Then

            strFirst = rngFound.Address
            Do
                If StrComp(rngFound.Offset(0, -2).Text, strAsset, vbTextCompare) = 0 And _
                           rngFound.Offset(0, 2).Value2 <= dtImo Then

                    'Found a match
                    rw = rngFound.Row
                    If .Cells(rw, "F").Value = "" Then
                        .Cells(rw, "F").Value = timestmp
                    Else
                        MsgBox "This item has already been marked as returned!", vbOKOnly
                        Exit Sub
                    End If

                End If
                Set rngFound = .Columns("C").FindNext(rngFound)
            Loop While rngFound.Address <> strFirst
        End If
    End With

    MsgBox "Item has been marked as received!", vbOKOnly

    Set rngFound = Nothing
End Sub
OK...after a couple of small tweaks, it is working like a charm! Never would have gotten there without you!!! Thank you so much.

Here is the final product that works...

VBA Code:
Private Sub INBOUND()
   Dim rngFound    As Range
    Dim strFirst    As String
    Dim strNam      As String
    Dim strAsset    As String
    Dim rw          As Long
    Dim timestmp    As Date
    Dim ws          As Worksheet
    Dim dtImo       As Date

    Set ws = ThisWorkbook.Worksheets("ASSET Loging")

    timestmp = Now
    strNam = Me.txtnam.Value
    strAsset = Me.cmbasstype.Value
    dtImo = CDate(Me.txtdat.Value)

    With ws
        Set rngFound = .Columns("C").Find(strNam, .Cells(.Rows.Count, "C"), xlValues, xlWhole)
        If Not rngFound Is Nothing Then

            strFirst = rngFound.Address
            Do
                If StrComp(rngFound.Offset(0, -2).Text, strAsset, vbTextCompare) = 0 And _
                           rngFound.Offset(0, 2).Value2 = dtImo And rngFound.Offset(0, 4).Value = "" Then

                    'Found a match
                    rw = rngFound.Row
                    If .Cells(rw, "F").Value = "" Then
                        .Cells(rw, "F").Value = timestmp
                    Else
                        MsgBox "This item has already been marked as returned!", vbOKOnly
                        Exit Sub
                    End If

                End If
                Set rngFound = .Columns("C").FindNext(rngFound)
            Loop While rngFound.Address <> strFirst
        End If
    End With

    MsgBox "Item has been marked as received!", vbOKOnly

    Set rngFound = Nothing
End Sub
 
Upvote 0
One quick question...Does Excel always work from the bottom up or is there a way in the code to control that and force it go top down?
 
Upvote 0
No, Excel doesn't work from the bottom up on default. Nevertheless, I don't quite understand what you mean, because the FindNext method always works from left to right and top to bottom, regardless of the initial search direction (xlNext or xlPrevious).
 
Upvote 0
Gotcha...now that I think about it, that makes sense. My code always seems to find the first instance of a match instead of the last.

Thank you again for the help. Everything works perfectly now, and I learned something! Always a bonus.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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