VBA If Statement with Conditions causing issue with Dates

ian9999

New Member
Joined
Apr 5, 2008
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hi all

I am looking for any help I can get on the below

I'm trying to write code behind a user form that operates when you selected a contract number via a dropdown list, it then populates the textboxes, comboxes and listboxes where appropriate on the user form.

Some of the fields might either return a date hopefully in UK format DD/MM/YYYY or maybe a word

We are also using a list to change the name of the destination of the data via a counter and vlookup, the below code is from the combobox

The issue we are having is the code isn't populating the boxes correctly, either nothing is appearing or the format of the dates are coming through as numbers

I hope this makes sense, if not please ask and I will explain further

Looking for any advice you could offer

Regards

Ian

Private Sub CboContractNumber_Change()

Dim ContractLive
Dim ContractSP
Dim ContractLiveCount As Integer
Dim ContractSPCount As Integer
Dim Ctrl As Control
Dim Fieldname
Dim Fieldname2
Dim Counter
Dim Textbox
Counter = 1

For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "TextBox" Then
Ctrl.Text = ""
End If
Next Ctrl

ContractSP = Application.WorksheetFunction.IfError(Application.VLookup(CboContractNumber.Value, Sheets("LookupNEW").Range("BA1:BC4000"), 2, False), Application.WorksheetFunction.IfError(Application.VLookup(CboContractNumber.Value, Sheets("LookupNEW").Range("BB1:BC4000"), 1, False), 0))

Do Until Counter > 80

Fieldname2 = Sheets("LookupNEW").Range("BG" & Counter).Value
Textbox = Sheets("LookupNEW").Range("BH" & Counter).Value

Fieldname = WorksheetFunction.Match(Fieldname2, Sheets("Master Data NEW").Range("A2:EZ2"), 0)

If Application.VLookup(ContractSP, Sheets("Master Data NEW").Range("A:EZ"), Fieldname, False) = 0 Then
ElseIf IsDate(Application.VLookup(ContractSP, Sheets("Master Data NEW").Range("A:EZ"), Fieldname, False) = Format("mm/dd/yyyy")) = True Then
Me.Controls(Textbox) = CDate(Application.VLookup(ContractSP, Sheets("Master Data NEW").Range("A:EZ"), Fieldname, False))

Else

Me.Controls(Textbox) = Application.VLookup(ContractSP, Sheets("Master Data NEW").Range("A:EZ"), Fieldname, False)

End If
Counter = Counter + 1
Loop

End Sub
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Sorry to ask but any thoughts if the code is incorrectly set out
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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