Auto Populating a Date Text box with the latest date depending on the combo box selection

KTSARA

New Member
Joined
Nov 18, 2019
Messages
34
Hello Experts,
I am quite novice to VBA and still learning. So, any help from you would be highly appreciated.

I have an excel sheet (Sheet 4) with Machine codes (Column A) and Service Dates (Column B). Same machine code can be repeated in column A, but always with a unique service date. In my user form there is a combo box called MachineCodeComboBox and a text box called LastServiceDateTextBox.
I want to search for the machine code selected by the user from Sheet 4 and automatically fill the LastServiceDateTextBox with the latest service date of that particular machine code.

I used the below code to search for the machine code but, I couldn't figure out a way to insert the Max Function.

VBA Code:
Private Sub MachineCodeComboBox_Change()
 With Me
        .LastServiceDateTextBox = Application.WorksheetFunction.VLookup(Me.MachineCodeComboBox, Sheet4.Range("A:B"), 2, False)
   End With
End Sub

Your expert opinions regarding this questions are highly appreciated.
Thanks a lot in advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Is it correct to assume that if we find the last entry of a particular Machine code then it has the last date in its adjacent cell (in col B)?
 
Upvote 0
Is it correct to assume that if we find the last entry of a particular Machine code then it has the last date in its adjacent cell (in col B)?
Hello,
Thank you very much for taking your time to read and reply my question. Yes, last entry of the machine code would have its latest service date.
 
Upvote 0
Ok, try this:
But this is untested, so if it doesn't work then please upload your workbook (without sensitive data) to a free site (such as dropbox.com or google drive), then put the link here.
VBA Code:
Private Sub MachineCodeComboBox_Change()
     Dim c As Range
     If Me.MachineCodeComboBox.Value <> "" Then
        Set c = Sheet4.Range("A:A").Find(What:=Me.MachineCodeComboBox.Value, LookIn:=xlValues, _
        lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
        If Not c Is Nothing Then
           Me.LastServiceDateTextBox = c.Offset(, 1).Value
        Else
           Me.LastServiceDateTextBox = ""
        End If
     End If
End Sub
 
Upvote 0
Ok, try this:
But this is untested, so if it doesn't work then please upload your workbook (without sensitive data) to a free site (such as dropbox.com or google drive), then put the link here.
VBA Code:
Private Sub MachineCodeComboBox_Change()
     Dim c As Range
     If Me.MachineCodeComboBox.Value <> "" Then
        Set c = Sheet4.Range("A:A").Find(What:=Me.MachineCodeComboBox.Value, LookIn:=xlValues, _
        lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
        If Not c Is Nothing Then
           Me.LastServiceDateTextBox = c.Offset(, 1).Value
        Else
           Me.LastServiceDateTextBox = ""
        End If
     End If
End Sub
Hello Akuini,
This works perfectly. Thank you so much for taking your valuable time to answer my question very quickly. You are indeed a genius.
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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