如何使用我的userform VBA代码插入行?

时间:2019-11-25 00:42:02

标签: excel vba userform

我已经创建了一个用户窗体,并设置了所有字段,以便在“提交”命令按钮上填充数据。此“填充”使用单独的工作表命令,该命令在字段中查找包含数据的最后一行,并告诉宏宏向下移动一行。但是,我想改为插入行,以便可以从文档中删除多余的行开始。

到目前为止,这是我的代码,包括错误消息等。一切都按需要进行,以便在最后一行的下面输入数据,所以我假设我只需要在某个地方向“ InsertEntireRow”添加代码,但我我不能完全确定在哪里。

这是我的代码:

`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")

Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow).EntireRow.Copy
Sheets("Travel Expense Voucher").Range("Data_Start").Offset(TargetRow + 1).EntireRow.Insert
Application.CutCopyMode = False



'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`

0 个答案:

没有答案