【问题标题】:Excel VBA: type mismatch error 13Excel VBA:类型不匹配错误 13
【发布时间】:2015-01-26 03:19:34
【问题描述】:

我有 2 张纸。当 ws2 中特定列中的条目包含单词“UPDATE”时,它将使用 ws2 中找到的更新数据更新 ws1

Private Sub CommandButton1_Click()

Dim LastRow As Long, CurRow As Long, DestRow As Long, DestLast As Long



Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = Sheets("Dashboard")
Set ws2 = Sheets("TempHRI")

LastRow = ws2.Range("B" & Rows.Count).End(xlUp).Row
DestLast = ws1.Range("E" & Rows.Count).End(xlUp).Row

For CurRow = 2 To LastRow 'Assumes first row has headers
    If ws2.Range("X" & CurRow) = "UPDATE" Then 'Column that looks up the word "Update" in ws2
        If Not ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
            DestRow = ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole).Row
        End If
        ws1.Range("Q" & DestRow).Value = ws2.Range("N" & CurRow).Value 'assumes supervisor is in column C in both sheets
        ws1.Range("R" & DestRow).Value = ws2.Range("O" & CurRow).Value 'assumes director is in column D in both sheets
    End If
Next CurRow

End Sub

但我在网上遇到类型不匹配:

If ws2.Range("X" & CurRow) = "UPDATE" Then

提前致谢。

【问题讨论】:

    标签: vba excel type-mismatch


    【解决方案1】:

    尝试:

    If ws2.Range("X" & CurRow).Text = "UPDATE" Then
    

    【讨论】:

    • 可能要补充一点,当您正在比较的单元格之一中存在错误值时会导致此错误,例如“#N/A”。 VBA 不允许您将错误类型的变体与文字字符串进行比较。
    • 是的,我设法修复它。 Dim checkstatus As String 并添加 checkstatus = CStr(ws2.Range("AB" & CurRow).Value) 以将值设为字符串。现在可以使用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多