How to use a textbox in a userform for search for last duplicate value in textbox1

frankrai

New Member
Joined
Jun 26, 2018
Messages
3
Hi guys,
I am new to this forum so I hope I am doing this correct.
I need some help. I am trying to make a VBA code to find duplicate number and use the last one in that column.
ie. I have columns "A", "B", "C", "D", "E", "F"

in Column "D" i have a lot number that is 11,101,102,103 etc There will be several 100s, serveal101s and so on. They will and should always be grouped together, so I just want to be able to typoe in a value into textbox1 and search for the last number in that group and return the infor from that row into the other textboxs. ie textbox2, textbox3 etc.

I hope I didn't make it to complicated. I searched for over a couple of weeks and could not find anything to this. I tried useing the Lastrow, xlPrevious, xlEnd. but I can't get it to work.

Any help would be greatly appreciated.

Private Sub CommandButton1_Click()Dim i As Long
Set obj = ThisWorkbook.Sheets("Sheet1")
With obj.UsedRange
Set c = .Find(TextBox1.Text) '.End(xlLast)
'For x = 1 To 2

Me("TextBox2") = obj.Cells(c.Row, 1).Value
Me("TextBox3") = obj.Cells(c.Row, 2).Value
Me("TextBox4") = obj.Cells(c.Row, 3).Value
Me("TextBox5") = obj.Cells(c.Row, 4).Value
Me("TextBox6") = obj.Cells(c.Row, 5).Value
Me("TextBox7") = obj.Cells(c.Row, 6).Value
'Next x
End With
End Sub
Private Sub UserForm_Click()


End Sub
Private Sub CommandButton2_Click()
Me("TextBox1") = ""
Me("TextBox2") = ""
Me("TextBox3") = ""
Me("TextBox4") = ""
Me("TextBox5") = ""
Me("TextBox6") = ""
Me("TextBox7") = ""
End Sub

____________________________________________________________

[TABLE="width: 384"]
<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 64"]#1[/TD]
[TD="class: xl63, width: 64"]#2[/TD]
[TD="class: xl63, width: 64"]#3[/TD]
[TD="class: xl63, width: 64"]#4[/TD]
[TD="class: xl63, width: 64"]#5[/TD]
[TD="class: xl63, width: 64"]#6[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]100[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]201[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]10[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]100[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]202[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD="align: right"]12[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]100[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]203[/TD]
[/TR]
[TR]
[TD="align: right"]4[/TD]
[TD="align: right"]13[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]100[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]204[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]101[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]205[/TD]
[/TR]
[TR]
[TD="align: right"]6[/TD]
[TD="align: right"]15[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]101[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]206[/TD]
[/TR]
[TR]
[TD="align: right"]7[/TD]
[TD="align: right"]13[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]101[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]207[/TD]
[/TR]
[TR]
[TD="align: right"]8[/TD]
[TD="align: right"]16[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]101[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]208[/TD]
[/TR]
[TR]
[TD="align: right"]9[/TD]
[TD="align: right"]23[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]102[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]209[/TD]
[/TR]
[TR]
[TD="align: right"]10[/TD]
[TD="align: right"]25[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]102[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]210[/TD]
[/TR]
[TR]
[TD="align: right"]11[/TD]
[TD="align: right"]27[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]102[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]211[/TD]
[/TR]
[TR]
[TD="align: right"]12[/TD]
[TD="align: right"]23[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]102[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]212[/TD]
[/TR]
[TR]
[TD="align: right"]13[/TD]
[TD="align: right"]25[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]103[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]213[/TD]
[/TR]
[TR]
[TD="align: right"]14[/TD]
[TD="align: right"]23[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]103[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]214[/TD]
[/TR]
[TR]
[TD="align: right"]15[/TD]
[TD="align: right"]26[/TD]
[TD="class: xl65"]BB[/TD]
[TD="align: right"]103[/TD]
[TD="class: xl64, align: right"]12-Jun[/TD]
[TD="align: right"]215[/TD]
[/TR]
[TR]
[TD="align: right"]16[/TD]
[TD="align: right"]24[/TD]
[TD="class: xl65"]AA[/TD]
[TD="align: right"]103[/TD]
[TD="class: xl64, align: right"]14-Jun[/TD]
[TD="align: right"]216[/TD]
[/TR]
[TR]
[TD="align: right"]17[/TD]
[TD="align: right"]2[/TD]
[TD="class: xl65"]BB[/TD]
[TD="align: right"]104[/TD]
[TD="class: xl64, align: right"]14-Jun[/TD]
[TD="align: right"]217[/TD]
[/TR]
[TR]
[TD="align: right"]18[/TD]
[TD="align: right"]2[/TD]
[TD="class: xl65"]BB[/TD]
[TD="align: right"]104[/TD]
[TD="class: xl64, align: right"]14-Jun[/TD]
[TD="align: right"]218[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Some .find parameters are persistent, so unless stated whatever was used last will be used again.

See if something like this works for you.
Code:
Private Sub CommandButton1_Click()

    Dim i As Long, c As Range

With Sheets("Sheet1").UsedRange
    Set c = .Columns(4).Find(What:=Me.Textbox1.Value, _
                       LookIn:=xlValues, _
                       LookAt:=xlWhole, _
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlPrevious, _
                       MatchCase:=False)
    If Not c Is Nothing Then
        'c.Select
        Me.TextBox2.Value = .Cells(c.Row, 1).Value
        Me.TextBox3.Value = .Cells(c.Row, 2).Value
        Me.TextBox4.Value = .Cells(c.Row, 3).Value
        Me.TextBox5.Value = .Cells(c.Row, 4).Value
        Me.TextBox6.Value = .Cells(c.Row, 5).Value
        Me.TextBox7.Value = .Cells(c.Row, 6).Value
    End If
End With

End Sub
 
Upvote 0
Yes it worked. Great! Thank you. I am going to try to incorpoate it into the project I am building for my business. Again thank you much
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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