Try changing
PrevDate.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 36, False)
to
PrevDate.Text = Format(Application.VLookup(PrevName.Text, Range("LatestData"), 36, False), "yyyy/mm")
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I've designed a VBA UserForm. I want several things to happen once the name is chosen from the combobox.
Watch: Application.VLookup(PrevName.Text, Range("LatestData"), 36, False) : Error 2042 : Variant/Error : Data_UF.ComboName_AfterUpdate
Watch : PrevDate.Text : "" : String : Data_UF.ComboName_AfterUpdate
Sorry, as a newbie this means absolutely nothing to me! Here is the rest of the code...
Private Sub ComboName_AfterUpdate()
With Data_UF
'Once ComboName is selected from drop-down look up the provider name in table named "Lookup" and enter it into the Userform automatically.
TxtProvider.Text = Application.VLookup(ComboName.Text, Range("Lookup"), 2, False) 'This works
'Once ComboName is selected, complete the PrevName text box in the section marked "Previous Score".
PrevName.Text = ComboName.Text 'This works
'Once ComboName completed in the "Previous score" section, complete the other text boxes to show the previous record.
PrevDate.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 36, False) 'date format is yyyy/mm/dd
PrevContract.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 38, False) 'format was a tickbox so query reads true/false
PrevPlacements.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 37, False) 'format was a tickbox so query reads true/false
PrevScore1.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 8, False)
PrevScore2.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 9, False)
PrevScore3.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 10, False)
PrevScore4.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 11, False)
PrevScore5.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 12, False)
PrevScore6.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 13, False)
Prevfinance.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 14, False)
PrevStaffing.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 15, False)
PrevManagement.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 16, False)
PrevScore10.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 17, False)
PrevComments.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 35, False)
PrevScore.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 29, False)
End With
End Sub
I'm grateful for any help with this. Thanks
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Try changing
PrevDate.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 36, False)
to
PrevDate.Text = Format(Application.VLookup(PrevName.Text, Range("LatestData"), 36, False), "yyyy/mm")
No - you should stick with False to force an exact match. Are all entries validated (on both the sheet and the userform)? Since you got the "a messagebox says was not found in [workbookname]LatestData!H3:H49." message, that means that an exact match of the userform's entry was not found in that range. So something is wrong with either the entry on the userform, or in H3:H49.....
Make sure tha LatestData is a global name or resides on the active sheet.
For troubleshooting, change your code to this:
Private Sub ComboName_AfterUpdate()
Dim v As Variant
With Data_UF
'Once ComboName is selected from drop-down look up the provider name in table named "Lookup" and enter it into the Userform automatically.
TxtProvider.Text = Application.VLookup(ComboName.Text, Range("Lookup"), 2, False) 'This works
Set v = Range("LatestData").Columns(1)
If IsError(v) Then
Msgbox "LatestData is not correctly formed"
Exit Sub
End If
v = Application.Match(PrevName.Text, Range("LatestData").Columns(1), False)
If IsError(v) Then
Msgbox PrevName.Text & " was not found in " & Range("LatestData").Columns(1).Address(False, False, xlA1, True)
Exit Sub
End If
'Once ComboName is selected, complete the PrevName text box in the section marked "Previous Score".
PrevName.Text = ComboName.Text 'This works
'Once ComboName completed in the "Previous score" section, complete the other text boxes to show the previous record.
PrevDate.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 36, False) 'date format is yyyy/mm/dd
PrevContract.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 38, False) 'format was a tickbox so query reads true/false
PrevPlacements.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 37, False) 'format was a tickbox so query reads true/false
PrevScore1.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 8, False)
PrevScore2.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 9, False)
PrevScore3.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 10, False)
PrevScore4.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 11, False)
PrevScore5.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 12, False)
PrevScore6.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 13, False)
Prevfinance.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 14, False)
PrevStaffing.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 15, False)
PrevManagement.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 16, False)
PrevScore10.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 17, False)
PrevComments.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 35, False)
PrevScore.Text = Application.VLookup(PrevName.Text, Range("LatestData"), 29, False)
End With
End Sub
Hi,
H3:H49 should match the values in ComboName. They should match exactly as they have been fed from a drop-down list. Would VLookup using True be better?
Hi,
The issue was my lookup column was not the first column in the range (I saw this on a website and it suddenly clicked). I've reordered my table and your coding is now working - so thanks for this.
The only issue I have is that the previous date is now showing as a code e.g. 44216 when I just need it to show YYYY/MM. (the original date text box fills with TxtDate.Text = Format(Now(), "yyyy/mm"). Any ideas please?
Many thanks
Loopilu