使用VBA在excel中创建表单
我正试图用VBA在excel中创建一个表单,但我被代码卡住了。我需要找到使用VBA表单将数据输入工作表的代码。这是我正在使用的代码,但不起作用使用VBA在excel中创建表单,vba,excel,Vba,Excel,我正试图用VBA在excel中创建一个表单,但我被代码卡住了。我需要找到使用VBA表单将数据输入工作表的代码。这是我正在使用的代码,但不起作用 Private Sub cmdAdd_Click() Dim LastRow As Range Dim DPDIAdhocRequestTable As ListObject With LastRow Cells(1, 2) = RequesterName.Value Cells(1, 3) = RequesterPhoneNum
Private Sub cmdAdd_Click()
Dim LastRow As Range
Dim DPDIAdhocRequestTable As ListObject
With LastRow
Cells(1, 2) = RequesterName.Value
Cells(1, 3) = RequesterPhoneNumber.Value
Cells(1, 4) = RequesterBureau.Value
Cells(1, 5) = DateRequestMade.Value
Cells(1, 6) = DateRequestDue.Value
Cells(1, 7) = PurposeofRequest.Value
Cells(1, 8) = ExpectedDataSaurce.Value
Cells(1, 9) = Timeperiodofdatarequested.Value
Cells(1, 10) = ReoccuringRequest.Value
Cells(1, 11) = RequestNumber.Value
Cells(1, 12) = AnalystAssigned.Value
Cells(1, 13) = AnalystEstimatedDueDate.Value
Cells(1, 14) = AnalystCompletedDate.Value
Cells(1, 15) = SupervisiorName.Value
End With
End Sub
你能帮我找出enter命令的正确代码吗
非常感谢您的帮助。第一个问题是您创建了一个名为LastRow的区域,但尚未为其分配任何内容
'Declaration
Dim LastRow As Range
'Assigns the last row based on the last item in Column A
Set LastRow = Range("A" & Rows.Count).End(xlUp).Row
With LastRow
...
End With
第二个问题是With LastRow块中的一个小语法错误
应该是
With LastRow
.Cells(x,y).Value = "Foo"
End With
这与LastRow.Cellsx,y.Value=Foo基本相同。没有这个。在单元格前面,VBA不会将With应用于该对象,并假设您是指ActiveSheet.Cells,正如@Adam所说的-您创建了LastRow,但未将其分配给任何对象。 我猜这是您要将数据粘贴到的下一行,因此它应该是一个长的行号,而不是对单元格的实际引用 在下面的代码中,您可以通过添加Me来限定表单控件,例如Me.RequesterName.Value
但是不起作用具体来说,什么不起作用?Cellsx,y.Value=ObjectName.Value您尝试过这种语法吗?您可能想更正SupervisorName的拼写-虽然不是一个错误,但它会让我非常恼火他们的With语句也不会对其当前的编写方式产生任何影响
With LastRow
.Cells(x,y).Value = "Foo"
End With
Private Sub cmdAdd_Click()
Dim wrkSht As Worksheet
Dim LastRow As Long
'The sheet you want the data to go into.
Set wrkSht = ThisWorkbook.Worksheets("Sheet1")
'You're after the last row number, rather than referencing the range so LastRow is a Long.
'This looks for the last cell containing data in column A (the 1 in 'Cells(...)').
LastRow = wrkSht.Cells(Rows.Count, 1).End(xlUp).Row + 1
'With... End With block. Cells is preceded by a '.' notation - indicating it's referencing the 'With wkrSht'#
'https://msdn.microsoft.com/en-us/library/wc500chb.aspx
With wrkSht
'Using LastRow as row number in cell reference.
.Cells(LastRow, 2) = RequesterName.Value
'Before adding dates to the sheet you might want to check that the
'user entered a date and not rubbish.
.Cells(LastRow, 5) = DateRequestMade.Value
'You could use CDATE to force it to a date - will try and coerce the entered text into a date.
'Note - 1 will be changed to 01/01/1900 (so will need to add extra code to check it really is a date).
.Cells(LastRow, 5) = CDate(DateRequestMade.Value)
End With
End Sub