首页 > 解决方案 > 错误 13:在类似 vba 语句起作用的用户窗体上的文本框类型不匹配

问题描述

我们的电子表格之一需要用户表单。尝试将用户的值粘贴到包含数据的工作表时,我收到错误代码 13:类型不匹配。

所有字段都是文本框。除了我们发布信息的地址之外,一行代码相同。

这是我所拥有的:

Public Sub btnSubmit_Click()
Dim TableSht As Worksheet
Dim NextRow As Long

Set TableSht = ThisWorkbook.Sheets("Table")

TableSht.Visible = True

'https://www.mrexcel.com/forum/excel-questions/1017033-making-all-fields-userform-mandatory.html#post4880848
'determine if any fields were left blank
For Each Control In Me.Controls                                     '
Select Case TypeName(Control)
    Case "TextBox"
        If Control.Value = vbNullString Then
            MsgBox "empty field in " & Control.Name
            Exit For
        End If
     Case Else
End Select
Next Control

'data is housed in E3:J3, E5:J5, E7:J7, E9:J9. if statement determines what row information
'should be entered on.
If TableSht.Range("E3") = "" Then
NextRow = 3
    ElseIf TableSht.Range("E5") = "" Then
    NextRow = 5
        ElseIf TableSht.Range("E7") = "" Then
        NextRow = 7
            ElseIf TableSht.Range("E9") = "" Then
            NextRow = 9
                Else
                MsgBox ("There are no more available rows. Contact Craig for additional assistance.")
End If

'paste the user's data entry into the appropriate cells
With TableSht
.Cells(NextRow, 5) = Me.tbOwner
.Cells(NextRow, 6) = CDate(Me.tbDate)
.Cells(NextRow, 7) = Me.tbChange
'Me.tbChange.Value = CDec(Me.tbChange) 'no longer use this but one of my attempts

.Cells(NextRow, 8) = Me.tbAmount
.Cells(NextRow, 9) = Me.tbOriginal
.Cells(NextRow, 10) = Me.tbReason

.Cells(NextRow, 7).Value = Format(Range("G" & NextRow) / 100, "0.00%")
.Cells(NextRow, 8).Value = Format(Range("H" & NextRow), "$##.##")
.Cells(NextRow, 9).Value = Format(Range("I" & NextRow) / 100, "0.00%")
End With

Sheets("Rate Calculator v8").Select

TableSht.Visible = xlVeryHidden

Unload Me
End
End Sub

错误发生在

.Cells(NextRow, 7).Value = Format(Range("G" & NextRow) / 100, "0.00%")

如果我删除该行并在其后的两行循环,则没有错误,即使“结束于”之前的最后一行本质上是相同的语句。

我尝试交换相似的两行代码。"Cells(NextRow, 7)..." 和 ".Cells(NextRow, 9)..." 但错误仍然出现在 "Cells(NextRow, 7)..." 行。

我已经确认数据粘贴在 G 列中的单元格,并且我都被格式化为“百分比”。

标签: excelvba

解决方案


Range用一张纸来证明你的使用。如果工作表也是TableSht,则以下内容应该有效。如果Range来自不同的工作表,请限定该工作表

 = Format(.Range("G" & NextRow) / 100, "0.00%")
 = Format(.Range("H" & NextRow), "$##.##")
 = Format(.Range("I" & NextRow) / 100, "0.00%")

推荐阅读