将Focus设置为formField1

时间:2015-07-23 23:26:35

标签: excel vba excel-vba

我需要一些帮助。我创建了一个UserForm,并试图获取表单(在一个工作表上)写入另一个工作表。我觉得我越来越接近,但我不断收到以下错误:

我一直在excel中获得运行时2110错误。

除此之外,我正在尝试将用户表单中的数据发布到名为“每小时CI数据”的工作表中。

以下代码来自我的UserForm对象:

Private Sub cmdbtnCancel_Click()
' Clear data fields and reset the form
Me.formField1.Value = ""
Me.formField2.Value = ""
Me.formField3.Value = ""
Me.formField4.Value = ""
Me.formField5.Value = ""
Me.formField6.Value = ""
Me.formField7.Value = ""
Me.formField8.Value = ""
Me.formField9.Value = ""
Me.formField10.Value = ""
Me.formField11.Value = ""
Me.formField1.SetFocus
Unload Me
End Sub

Sub cmdbtnSave_Click()
Dim iRow As Long
Dim ws As Worksheet
Unload Me
Set ws = Worksheets("Hourly Slot CI - Data")
' Find the next empty row
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
' Input the data in the Data Table
ws.Cells(iRow, 1).Value = Me.formField1.Value
ws.Cells(iRow, 2).Value = Me.formField2.Value
ws.Cells(iRow, 3).Value = Me.formField3.Value
ws.Cells(iRow, 4).Value = Me.formField4.Value
ws.Cells(iRow, 5).Value = Me.formField5.Value
ws.Cells(iRow, 6).Value = Me.formField6.Value
ws.Cells(iRow, 7).Value = Me.formField7.Value
ws.Cells(iRow, 8).Value = Me.formField8.Value
ws.Cells(iRow, 9).Value = Me.formField9.Value
ws.Cells(iRow, 10).Value = Me.formField10.Value
ws.Cells(iRow, 11).Value = Me.formField11.Value
ws.Cells(iRow, 12).Value = Me.formField12.Value
ws.Cells(iRow, 13).Value = Me.formField13.Value
ws.Cells(iRow, 14).Value = Me.formField14.Value
ws.Cells(iRow, 15).Value = Me.formField15.Value
ws.Cells(iRow, 16).Value = Me.formField16.Value
ws.Cells(iRow, 17).Value = Me.formField17.Value
ws.Cells(iRow, 18).Value = Me.formField18.Value
ws.Cells(iRow, 19).Value = Me.formField19.Value
ws.Cells(iRow, 20).Value = Me.formField20.Value
ws.Cells(iRow, 21).Value = Me.formField21.Value
ws.Cells(iRow, 22).Value = Me.formField22.Value
ws.Cells(iRow, 23).Value = Me.formField23.Value
ws.Cells(iRow, 24).Value = Me.formField24.Value
ws.Cells(iRow, 25).Value = Me.formField25.Value
ws.Cells(iRow, 26).Value = Me.formField26.Value
ws.Cells(iRow, 27).Value = Me.formField27.Value
ws.Cells(iRow, 28).Value = Me.formField28.Value
ws.Cells(iRow, 29).Value = Me.formField29.Value
ws.Cells(iRow, 30).Value = Me.formField30.Value
ws.Cells(iRow, 31).Value = Me.formField31.Value
ws.Cells(iRow, 1).Activate
' Clear all fields and reset the form
Me.formField1.Value = ""
Me.formField2.Value = ""
Me.formField3.Value = ""
Me.formField4.Value = ""
Me.formField5.Value = ""
Me.formField6.Value = ""
Me.formField7.Value = ""
Me.formField8.Value = ""
Me.formField9.Value = ""
Me.formField10.Value = ""
Me.formField11.Value = ""
Me.formField12.Value = ""
Me.formField13.Value = ""
Me.formField14.Value = ""
Me.formField15.Value = ""
Me.formField16.Value = ""
Me.formField17.Value = ""
Me.formField18.Value = ""
Me.formField19.Value = ""
Me.formField20.Value = ""
Me.formField21.Value = ""
Me.formField22.Value = ""
Me.formField23.Value = ""
Me.formField24.Value = ""
Me.formField25.Value = ""
Me.formField26.Value = ""
Me.formField27.Value = ""
Me.formField28.Value = ""
Me.formField29.Value = ""
Me.formField30.Value = ""
Me.formField31.Value = ""
Me.formField1.SetFocus
End Sub

我不确定如何正确设置此焦点,因此任何输入都有帮助。提前谢谢!

1 个答案:

答案 0 :(得分:1)

在实际尝试设置值之前,您正在卸载表单。在完成最后完成后,您需要卸载表单。在您的情况下,在CmdbtnSave_Click子广告中,您希望在End Sub之前将其正确放置,并确保将其从头开始删除。

编辑:此外,如果您要卸载表单,则不需要清除不同的表单域。卸载将从计算机的内存中删除它(和表单字段值)。