簡體   English   中英

運行時錯誤13類型不匹配

[英]run-time error 13 type mismatch

我正在嘗試運行一個腳本,該腳本根據特定列的單元格值在現有行之間插入空白行。

到目前為止,我有這個:

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類型不匹配。 我已經搜索了該站點,但沒有找到修復程序,但是我是新手,所以任何幫助/指導都將非常感謝!

我猜想這段代碼來自其他地方,您嘗試編輯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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM