Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

Can someone help me with inserting a row using my userform vba code?

$
0
0

I have created a userform and set all fields for data to populate on 'submit' command button. This 'populate' is using a separate sheet command that finds the last row with data in a field and tells my macro population to move down one row. However, I would like, instead, to insert the row so I can remove extra rows from my document to start with.

Here is my code thus far, including error messages, etc.. Everything is working just as desired for entering in the row below the last with data so I assume I just need to add code to 'InsertEntireRow' someplace but I'm not completely sure where. Here is my code:

`Public Sub cmdSubmit_Click()
`enter code here`'When submit button is clicked

Dim TargetRow As Integer

TargetRow = Sheets("Codes").Range("D37").value + 1
Lodging = format(Lodging, "$#,##0.00")
txtTravelDate = format(txtTravelDate, "mm/dd/yyyy")
txtDepartTime = format(txtDepartTime, "hh:mm am/pm")
txtArrivalTime = format(txtArrivalTime, "hh:mm am/pm")


'Error messages presented when submit is selected
If txtTravelDate.value = "" Then
    MsgBox "You must enter Date of Travel", vbCritical
    Exit Sub
End If
If txtDepartTime.value = "" Then
    MsgBox "You must enter Actual Departure Time.  If this was overnight travel, and you traveled the previous day to your destination, enter 12:01 AM", vbCritical
    Exit Sub
End If
If txtArrivalTime.value = "" Then
    MsgBox "You must enter Actual Arrival Time.  If this is an overnight trip and you will not return home today, please enter 12:00 PM", vbCritical
    Exit Sub
End If
If cmbOVNRTN.value = "" Then
    MsgBox "You must select if these expense reimbursements are for Overnight travel or same day Return", vbCritical
    Exit Sub
End If
If Sheets("Travel Expense Voucher").Range("f5").value = 1 And txtProjectNumber.value = "" Then
    MsgBox "You must provide a valid Project Number", vbCritical
    Exit Sub
End If
If cmbInOutState.value = "" Then
    MsgBox "You must select if this travel was 'In-State' or 'Out-of-State'", vbCritical
    Exit Sub
End If
If txtTrvlDetails.value = "" Then
    MsgBox "You must provide Travel Details/Description and Business Purpose for this trip", vbCritical
    Exit Sub
End If

'''BEGIN DATA MOVE INTO DATABASE'''
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 0).value = txtTravelDate 'travel date
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 1).value = txtDepartTime 'departure time
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 3).value = txtArrivalTime 'arrival time
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 5).value = cmbOVNRTN 'overnight or return
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 12).value = txtProjectNumber 'project number
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 11).value = cmbInOutState 'in-state or out-of-state
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 10).value = cmbTravelMode 'travel mode
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 14).value = txtMilesTraveled 'mileage
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 13).value = cmbMileageRates 'mileage rate
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 16).value = Lodging 'no-receipt lodging
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 17).value = chkMorning 'morning meal
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 18).value = chkMidday 'midday meal
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 19).value = chkEvening 'evening meal
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow, 6).value = txtTrvlDetails 'travel details/business purpose
'''END DATA MOVE INTO DATABASE'''

  MsgBox "Travel entry for "& txtTravelDate & " is complete.  Select the 'Add' button to add another day of travel.", 0, "Complete"

Unload frmUserTravel
End Sub`

Viewing all articles
Browse latest Browse all 88868

Trending Articles