【问题标题】:run-time error 13 type mismatch运行时错误 13 类型不匹配
【发布时间】:2015-02-05 17:04:20
【问题描述】:

我正在尝试运行一个脚本,根据某列的单元格值在现有行之间插入空白行。

到目前为止,我有这个:

Set currentcell = Sheets("L1f").Range("I2:I131")

Do While Not IsEmpty(currentcell)

    Set nextcell = ActiveCell.Offset(1, 0)

   ** If currentcell.Value = 1 Then
        nextcell.EntireRow.Insert

    ElseIf currentcell.Value = 2 Then
        nextcell.EntireRow.Insert
        nextcell.EntireRow.Insert

    ElseIf currentcell.Value = 3 Then
        nextcell.EntireRow.Insert
        nextcell.EntireRow.Insert
        nextcell.EntireRow.Insert

    End If
    Set currentcell = nextcell
Loop

我在突出显示的行上遇到运行时错误 13 类型不匹配。我已经搜索了这个网站,但没有找到解决办法,但我是新手,所以任何帮助/指导都会非常感谢!

【问题讨论】:

    标签: excel runtime-error type-mismatch vba


    【解决方案1】:

    我猜这段代码来自其他地方,并且您尝试编辑 Set currentcell 行,以便将其限制为 L1f!I2:I131 而不仅仅是选择所在的位置?

    问题在于,当您将currentcell 设置为多个单元格时,它的.Value 变为一个数组而不是单个值。因此,您需要将currentcell 设置为L1f!I2:I131 中的开始 单元格,并引入另一个测试以查看它何时超出该范围:

    Dim currentcell as Range
    Dim endcell as Range
    Set currentcell = [L1f!I2]
    Set endcell = [L1f!I131]
    
    Do While currentcell.Row <= endcell.Row
    
        Set nextcell = currentcell.Offset(1, 0)
    
        If IsNumeric(currentcell) Then
    
            If currentcell.Value = 1 Then
                nextcell.EntireRow.Insert
    
            ElseIf currentcell.Value = 2 Then
                nextcell.EntireRow.Insert
                nextcell.EntireRow.Insert
    
            ElseIf currentcell.Value = 3 Then
                nextcell.EntireRow.Insert
                nextcell.EntireRow.Insert
                nextcell.EntireRow.Insert
    
            End If
    
        End If
    
        Set currentcell = nextcell
    Loop
    

    【讨论】:

    • 另一个问题(您似乎已经解决但未提及)是Set nextcell = ActiveCell.Offset(1, 0),其中ActiveCell 与程序的其余部分无关。
    • 感谢您的快速回复!试过了,效果很好!
    最近更新 更多