Userform - 类型不匹配错误

时间:2017-07-21 10:18:26

标签: excel vba types userform mismatch

我是编码新手(2个月),我主要从网上翻录代码。对于上下文,我正在遵循here的一些说明,了解如何使用userform更新工作表中的值。

第一位没问题,我可以将数据从我的工作表中拉回到我能编辑的userform,但是尝试更新工作表中的数据给了我一个&# 39;类型不匹配'错误。我点击“更新”时的代码如下:按钮

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 stage上遇到类型不匹配的问题。 Me.cmbslno.Value是一个简短的数字unique ID

我完全复制了上面的指南 - 除了相应地重命名之外 - 所以不知道问题是什么。

非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

2个可能的问题:

  1. rowselect小于1
  2. 选择的一些问题。
  3. 试试这样:

    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&lt; 1.如果rowselect有字符串值,则会出错。
    2. 我使用With Worksheets("Sheet 1")

答案 1 :(得分:1)

Cells属性接受2个参数,两个参数都应为数字:

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

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