【问题标题】:VBA Macro: Error 13 Type Mismatch, error 2023 on cellVBA 宏:错误 13 类型不匹配,单元格上的错误 2023
【发布时间】:2018-07-06 20:03:58
【问题描述】:

我正在尝试创建一个宏,将行分组并汇总它们在列上的项目数量,我做到了它可以工作,但是在几行之后它只是崩溃并显示

错误 13:类型不匹配。

当我打开代码时,它会突出显示这一行

stringb = Cells(cont2, 7)

代码是这个:

Sub Juntarfilas()
    Dim Av1 As Double
    Dim cont As Long
    Dim cont2 As Long
    Dim numcol As Integer
    Dim comp1 As Integer
    Dim check As String
    Dim string1 As String
    Dim string2 As String
    Dim stringa As String
    Dim stringb As String
    Dim Rango As Variant

    Sheets("RESULTADO").Select
    numcol = Range("E2").Column
    cont = 2
    cont2 = 3

    If IsEmpty(Range("E2").Value) = True Then
        check = 0
    Else
        check = 1
    End If

    While (check = 1)
        string1 = Cells(cont, 5)
        string2 = Cells(cont2, 5)
        stringa = Cells(cont, 7)
        stringb = Cells(cont2, 7)
        comp1 = StrComp(string1, string2)
        If (comp1 = "0") Then
            Rango = Range(Cells(cont, 7), Cells(cont2, 7))
            Av1 = Application.WorksheetFunction.Sum(Rango)
            Cells(cont, 7) = Av1
            cont = cont
            cont2 = cont2
            Rows(cont2).EntireRow.Delete
        Else
            cont = cont + 1
            cont2 = cont2 + 1
        End If

        If IsEmpty(string1) = True Then
            check = 0
        Else
            check = 1
        End If
    Wend
End Sub

我无法提出解决方案,我来这里看看是否有人可以帮助我,提前谢谢。

如果需要,我也可以发送 excel 文件。

【问题讨论】:

  • 导致错误的值肯定是字符串吗?
  • 请去掉所有的空行,让你的代码更具可读性。
  • 单步执行或设置断点并检查单元格的值,可能不是字符串。
  • stringb 的值应该是多少?

标签: vba excel type-mismatch


【解决方案1】:

stringb = Cells(cont2, 7) 中的单元格包含返回错误的公式(例如#DIV/0!#N/A! 或任何其他)时,会发生类型不匹配错误。

使用stringb = Cells(cont2, 7).Text 来获取错误文本作为字符串。

【讨论】:

  • 好像是公式,没注意。我将该列更改为仅值并且它可以工作,现在它在找到一个空单元格时不会停止,但至少它可以求和,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
  • 2015-10-31
  • 2018-02-16
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多