错误13:在类似vba语句的用户窗体上,文本框的类型不匹配

时间:2018-08-17 19:39:21

标签: excel 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%")

即使删除“ end with”之前的最后一行基本上是相同的语句,如果我删除该行并在其后的两个循环之间也没有错误。

我尝试交换相似的两行代码。 “ Cells(NextRow,7)...”和“ .Cells(NextRow,9)...”,但错误仍显示在“ Cells(NextRow,7)...”行。

我已经确认将数据粘贴到G列中的单元格,并且我都将其格式设置为“百分比”。

1 个答案:

答案 0 :(得分:0)

用一张纸确认您的Range用法。如果工作表也是TableSht,则下面的方法应该起作用。如果Range来自其他工作表,请限定该工作表

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