用户表单 – types不匹配错误

我是新来的编码(2个月),我主要是从networking上的代码。 对于上下文,我从这里的一些说明如何使用用户userform更新我的工作表中的值。

第一位是好的,我能够从我的工作表拉回数据到我能够编辑的用户表单,但试图然后更新工作表中的数据是给我一个'types不匹配'的错误。 代码如下,当我点击“更新”button

 Private Sub cmdupdate_Click() If Me.cmbslno.Value = "" Then MsgBox "SL No Can Not be Blank!!!", vbExclamation, "SL No" Exit Sub End If Sheets("Sheet 1").Select Dim rowselect As String rowselect = Me.cmbslno.Value Cells(rowselect, 2) = Me.TextBoxdate.Value Cells(rowselect, 3) = Me.TextBoxraisedby.Value Cells(rowselect, 5) = Me.ComboBoxsite.Value Cells(rowselect, 6) = Me.ComboBoxfacility.Value Cells(rowselect, 7) = Me.ComboBoxpdriver.Value Cells(rowselect, 8) = Me.TextBoxissue.Value Cells(rowselect, 9) = Me.TextBoxconsequence.Value Cells(rowselect, 10) = Me.TextBoxmitigation.Value Cells(rowselect, 11) = Me.TextBoximpact.Value Cells(rowselect, 12) = Me.TextBoxlikely.Value Cells(rowselect, 13) = Me.TextBoximpact.Value End Sub 

我得到的Cells(rowselect, 2) = Me.TextBoxdate.Value stagetypes不匹配。 Me.cmbslno.Value是一个简短的数字unique ID

我已经完全复制了上面的指南 – 除了重新命名相应的东西 – 所以不知道问题是什么。

帮助将不胜感激。

2个可能的问题:

  1. rowselect小于1
  2. select的一些问题。

试试像这样:

 Option Explicit Private Sub cmdupdate_Click() If Me.cmbslno.Value = "" Then MsgBox "SL No Can Not be Blank!!!", vbExclamation, "SL No" Exit Sub End If Dim rowselect As Long rowselect = Me.cmbslno.Value If rowselect < 1 Then MsgBox "WRONG VALUE" With Worksheets("Sheet 1") .Cells(rowselect, 2) = Me.TextBoxdate.Value .Cells(rowselect, 3) = Me.TextBoxraisedby.Value .Cells(rowselect, 5) = Me.ComboBoxsite.Value .Cells(rowselect, 6) = Me.ComboBoxfacility.Value .Cells(rowselect, 7) = Me.ComboBoxpdriver.Value .Cells(rowselect, 8) = Me.TextBoxissue.Value .Cells(rowselect, 9) = Me.TextBoxconsequence.Value .Cells(rowselect, 10) = Me.TextBoxmitigation.Value .Cells(rowselect, 11) = Me.TextBoximpact.Value .Cells(rowselect, 12) = Me.TextBoxlikely.Value .Cells(rowselect, 13) = Me.TextBoximpact.Value End With End Sub 

这是我在上面的代码中修复它们的方法:

  1. 我对rowselect <1进行检查。如果rowselect有一个string值,它会给出一个错误。
  2. 我使用With Worksheets("Sheet 1")

Cells属性接受2个参数,这两个参数都应该是数字的:

您需要确保Me.cmbslno.Value的值是数字(或转换为数字),并且您当前使用的variablesrowselect被声明为Long

 Dim rowselect As Long If IsNumeric(Me.cmbslno.Value) Then rowselect = Me.cmbslno.Value Cells(rowselect, 2) = Me.TextBoxdate.Value '... End If