I have a userform to input values to a spreadsheet. The form has four types of controls – Combo boxes, Text Boxes, a Label and Date Pickers.
The combo boxes are populated with customer names.
The label is populated with the most recent invoice number.
The form works wonderfully for data input, but I would like to be able to edit the data using the form. I would like the user to be able to input an invoice number and have the form populate all controls with values from that particular invoice, then edit and save the record. Kind of like a vlookup for forms I guess. If anyone can get me started I will work to figure out as much as I can on my own. Here's what I have so far:
Private Sub UserForm_Initialize()
'Populate Customer, Origin, Destination, and Invoice Combo Boxes
Dim custList As Variant
Dim originList As Variant
Dim destList As Variant
Dim invList As Variant
custList = Sheets("Cust Rates").Range("A5:A500")
CBCustomer.List = custList
originList = Sheets("Cust Rates").Range("A5:A500")
CBOrigin.List = originList
destList = Sheets("Cust Rates").Range("A5:A500")
CBDestination.List = destList
invList = Sheets("Inv History").Range("A5:A100000")
CBInvoiceLookUp.List = invList
'Gets invoice number from history sheet.
With Me
.InvNumber.Caption = Range("C2").Value
End With
End Sub
Private Sub Add_Record_Btn_Click()
'Turn off protection.
ActiveSheet.Unprotect
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inv History")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.InvNumber.Caption
.Cells(lRow, 2).Value = Me.DTInvDate.Value
.Cells(lRow, 3).Value = Me.LDescription.Value
.Cells(lRow, 4).Value = Me.CBOrigin.Value
.Cells(lRow, 5).Value = Me.CBDestination.Value
.Cells(lRow, 6).Value = Me.CBCustomer.Value
.Cells(lRow, 7).Value = Me.DTOriginDate.Value
.Cells(lRow, 8).Value = Me.DTOriginTime.Value
.Cells(lRow, 9).Value = Me.DTDestDate.Value
.Cells(lRow, 10).Value = Me.DTDestTime.Value
.Cells(lRow, 11).Value = Me.StandbyTime.Value
.Cells(lRow, 12).Value = Me.DriverName.Value
.Cells(lRow, 13).Value = Me.TruckNumber.Value
.Cells(lRow, 14).Value = Me.LoadedMileage.Value
.Cells(lRow, 15).Value = Me.UnloadedMileage.Value
.Cells(lRow, 16).Value = Me.HazardousChkBx.Value
.Cells(lRow, 17).Value = Me.Inspection.Value
.Cells(lRow, 18).Value = Me.PONumber.Value
.Cells(lRow, 19).Value = Me.CCAuthorization.Value
.Cells(lRow, 20).Value = Me.DriverComments.Value
.Cells(lRow, 21).Value = Me.ReceivedBy.Value
End With
'Copy Formulas from V, W, X, and Y to new row.
With Sheets("Inv History")
.Range("V5:V" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=VLOOKUP($F5,'Cust Rates'!$A$4:D999999,2,FALSE)*O5"
.Range("W5:W" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=VLOOKUP($F5,'Cust Rates'!$A$4:E999999,3,FALSE)*N5"
.Range("X5:X" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=VLOOKUP($F5,'Cust Rates'!$A$4:F999999,4,FALSE)*(N5+O5)"
.Range("Y5:Y" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=VLOOKUP($F5,'Cust Rates'!$A$4:F999999,5,FALSE)*(K4)"
.Range("Z5:Z" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=SUM(V5:Y5)"
End With
'Clear input controls.
Me.LDescription.Value = ""
Me.CBOrigin.Value = ""
Me.CBCustomer.Value = ""
Me.CBDestination.Value = ""
Me.StandbyTime.Value = ""
Me.DriverName.Value = ""
Me.TruckNumber.Value = ""
Me.LoadedMileage.Value = ""
Me.UnloadedMileage.Value = ""
Me.HazardousChkBx.Value = ""
Me.Inspection.Value = ""
Me.PONumber.Value = ""
Me.CCAuthorization.Value = ""
Me.DriverComments.Value = ""
Me.ReceivedBy.Value = ""
'Clears signature for new invoice.
Worksheets("BOL").Activate
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
If InStr(LCase(sh.Name), "ink") > 0 Then sh.Delete
Next sh
'Set protection to allow signature
ActiveSheet.Protect DrawingObjects:=False
'Activate Inv History worksheet and turn on protection.
Worksheets("Inv History").Activate
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
'Activate BOL Worksheet for signature
Worksheets("BOL").Activate
'Close UserForm.
Unload Me
End Sub
Display More
Help is greatly appreciated!