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

第一点没问题,我可以将数据从工作表中拉回到我可以编辑的userform中,但是尝试随后更新工作表中的数据则出现“类型不匹配”错误。单击“更新”按钮时的以下代码

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个可能的问题:


rowselect小于1
选择的一些问题。


像这样尝试:

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


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


我检查rowselect rowselect具有字符串值,它将给出一个错误。
我使用With Worksheets("Sheet 1")

08-08 00:21