Hello. I used chatgpt to get the following code which is posted in the Forms section under "TruckLoanForm". The form opens, i put in values, i hit the enter button, but it does not post the values. The values should be posted to the following cells:
FE5, FF5, FG5, FH5, FJ5, FL5, FM5 on the "Tables" sheet.
I'm also trying to make it so that every time the button that activates the macro is pressed, it will add a new entry series to the next row (in this case 6, 7, 8, and so on).
Option Explicit ' This ensures that variable declaration is required
Private TextBoxTruckType As MSForms.textBox
Private TextBoxDownPayment As MSForms.textBox
Private TextBoxUpfitCost As MSForms.textBox
Private TextBoxInterestRate As MSForms.textBox
Private TextBoxTermLength As MSForms.textBox
Private TextBoxPaymentsPerYear As MSForms.textBox
Private Sub UserForm_Initialize()
' Initialize the user form
Me.Caption = "Truck Data Form"
Me.BackColor = RGB(51, 51, 51) ' Dark Gray
Me.ForeColor = RGB(255, 255, 255) ' White
' Increase form size by 1.5 times
Me.Width = Me.Width * 1.5
Me.Height = Me.Height * 1.5
' Add components to the user form
AddLabelAndTextBox "Truck Type:", "TextBoxTruckType", 10, 10
AddLabelAndTextBox "Down Payment:", "TextBoxDownPayment", 10, 40
AddLabelAndTextBox "Upfit Cost:", "TextBoxUpfitCost", 10, 70
AddLabelAndTextBox "Interest Rate:", "TextBoxInterestRate", 10, 100
AddLabelAndTextBox "Term Length (yrs):", "TextBoxTermLength", 10, 130
AddLabelAndTextBox "Payments Per Year:", "TextBoxPaymentsPerYear", 10, 160
' Add submit button to the bottom right
Dim btnSubmit As MSForms.CommandButton
Set btnSubmit = Controls.Add("Forms.CommandButton.1", , True)
With btnSubmit
.Name = "btnSubmit"
.Caption = "Submit"
.Left = Me.Width - .Width - 25 ' 10 pixels from the right edge
.Top = Me.Height - .Height - 40 ' 40 pixels from the bottom edge (moved upward by 1 row)
.Width = 80
.Height = 30
End With
End Sub
Private Sub btnSubmit_Click()
' Handle the submit button click event
' Get data from the form
Dim ws As Worksheet
Dim lastRow As Long
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Tables")
' Find the last row in column F
lastRow = ws.Cells(ws.Rows.Count, "FF").End(xlUp).Row
' Add data to the respective columns
ws.Cells(lastRow + 1, 161).Value = TextBoxTruckType.Value
ws.Cells(lastRow + 1, 162).Value = TextBoxDownPayment.Value
ws.Cells(lastRow + 1, 163).Value = TextBoxUpfitCost.Value
ws.Cells(lastRow + 1, 166).Value = TextBoxInterestRate.Value
ws.Cells(lastRow + 1, 169).Value = TextBoxTermLength.Value
ws.Cells(lastRow + 1, 168).Value = TextBoxPaymentsPerYear.Value
' Close the user form
Unload Me
End Sub
Private Sub AddLabelAndTextBox(labelText As String, textBoxName As String, leftPos As Single, topPos As Single)
' Add a label and text box to the user form
Dim lbl As MSForms.Label
Dim textBox As MSForms.textBox
' Add label
Set lbl = Controls.Add("Forms.Label.1", , True)
With lbl
.Caption = labelText
.Left = leftPos
.Top = topPos
End With
' Add text box
Set textBox = Controls.Add("Forms.TextBox.1", , True)
With textBox
.Name = textBoxName
.Left = leftPos + 120
.Top = topPos
End With
' Assign the text box to the appropriate variable
Select Case textBoxName
Case "TextBoxTruckType"
Set TextBoxTruckType = textBox
Case "TextBoxDownPayment"
Set TextBoxDownPayment = textBox
Case "TextBoxUpfitCost"
Set TextBoxUpfitCost = textBox
Case "TextBoxInterestRate"
Set TextBoxInterestRate = textBox
Case "TextBoxTermLength"
Set TextBoxTermLength = textBox
Case "TextBoxPaymentsPerYear"
Set TextBoxPaymentsPerYear = textBox
End Select
End Sub
Bookmarks