Excel用户窗体以更新现有数据

时间:2019-04-24 12:42:10

标签: excel userform

我想使用Userform覆盖数据,我可以基于Combobox(数据表中A列的唯一引用)将数据调用到表单。我无法将更新后的数据发送回去,并停留在运行时错误'13。

我看了很多帖子,但无法挑选出成功的线索!任何帮助表示赞赏。我只剩下简单的代码了,以更新该行的第4列。最终,我将从第二列开始进行扩展。

Private Sub cmbtrade_Change() - this part works as expected
Dim trade_name As String

If Me.cmbtrade.Value = "" Then

MsgBox "Trade Can Not be Blank!!!", vbExclamation, "Trade"

Exit Sub

End If

trade_name = cmbtrade.Value

On Error Resume Next

Dim trade As Double
trade_name = cmbtrade.Value
TextBox16.Text = Application.WorksheetFunction.VLookup(trade_name, 
Sheets("Sheet2").Range("A2:D43"), 4, False)

End Sub

问题部分。...

Private Sub cmdupdate_Click()
If Me.cmbtrade.Value = "" Then

MsgBox "Trade Name Can Not be Blank", vbExclamation, "Trade"
Exit Sub
End If
trade_name = cmbtrade.Value
Sheets("sheet2").Select
Dim rowselect As Double

rowselect = Me.cmbtrade.Value (this is where my mismatch error occurs)
rowselect = rowselect + 1
Rows(rowselect).Select

Cells(rowselect, 4) = Me.TextBox16.Value
End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试一下。您实际上不需要将组合框转换为Long,但是我认为这是一个好习惯。

Private Sub cmdupdate_Click()

If Me.cmbtrade.Value = "" Then
    MsgBox "Trade Name Can Not be Blank", vbExclamation, "Trade"
    Exit Sub
End If

Dim rowselect As Long

rowselect = CLng(Me.cmbtrade.Value) + 1
Sheets("sheet2").Cells(rowselect, 4) = Me.TextBox16.Value

End Sub