Excel VBA剪切和粘贴功能,下标超出范围错误

时间:2016-11-22 04:22:03

标签: excel vba excel-vba

    Sub CutandPaste()
    With ActiveSheet
        Dim i As Long
        Dim Sheet1 As Worksheet
        Dim Sheet2 As Worksheet

        Set mainsheet = ActiveWorkbook.Sheets("Sheet1")
        Set subsheet = ActiveWorkbook.Sheets("Sheet2")

        endrow = mainsheet.Range("A" & mainsheet.Rows.Count).End(xlUp).Row

            For i = 25 To endrow
                If mainsheet.Cells(i, "J") <> "" Or mainsheet.Cells(i, "L") <> "" Or mainsheet.Cells(i, "N") <> "" Then
                    mainsheet.Range(Cells(i, "B"), Cells(i, "O")).Cut Destination:=subsheet.Range(Cells(i + 25, "B"), Cells(i + 25, "O")).Paste
                    mainsheet.Range(Cells(i, "B"), Cells(i, "O")).Delete '~~> if you want to delete
                    i = i + 1
                End If
            Next
    End With
End Sub

我想用以下函数编写一个程序

如果Sheet1中的Cell(&#34; J&#34; i)或Cell(&#34; L&#34; i)或Cell(&#34; N&#34; i)不为空,其中i等于到任何整数

然后将Cell 1(&#34; B&#34; i)切换到Sheet 1中的Cell(&#34; O&#34; i)到Cell(&#34; B&#34; i + 25)到Cell Sheet2中的(&#34; O&#34; i + 25)其中i等于任何整数

然而,据说我的代码超出了范围。

为什么会这样,我该如何解决?

非常感谢

    Sub CutandPaste()
    With ActiveSheet
        Dim i As Long
        Dim Sheet1  As Worksheet
        Dim Sheet2 As Worksheet

        Set mainsheet = ActiveWorkbook.Sheets("Sheet1")
        Set subsheet = ActiveWorkbook.Sheets("Sheet2")

        endrow = mainsheet.Range("A" & mainsheet.Rows.Count).End(xlUp).Row

        For i = endrow To 25 Step -1

                If mainsheet.Cells(i, "J") <> "" Or mainsheet.Cells(i, "L") <> "" Or mainsheet.Cells(i, "N") <> "" Then
                    mainsheet.Range(mainsheet.Cells(i, "B"), mainsheet.Cells(i, "O")).Cut Destination:=subsheet.Range(subsheet.Cells(i + 56, "B"), subsheet.Cells(i + 56, "O")).Paste
                    mainsheet.Range(mainsheet.Cells(i, "B"), mainsheet.Cells(i, "O")).Delete '~~> if you want to delete

                End If
            Next
    End With
End Sub

最新版本:

        Sub CutandPaste()
    With ActiveSheet
        Dim i As Long
        Dim Sheet1  As Worksheet
        Dim Sheet2 As Worksheet

        Set mainsheet = ActiveWorkbook.Sheets("Sheet1")
        Set subsheet = ActiveWorkbook.Sheets("Sheet2")

        endrow = mainsheet.Range("A" & mainsheet.Rows.Count).End(xlUp).Row
        Bendrow = subsheet.Range("B" & mainsheet.Rows.Count).End(xlUp).Row + 1

        For i = endrow To 25 Step -1
                If mainsheet.Cells(i, "J") <> "" Or mainsheet.Cells(i, "L") <> "" Or mainsheet.Cells(i, "N") <> "" Then
                    mainsheet.Range(mainsheet.Cells(i, "B"), mainsheet.Cells(i, "O")).Cut Destination:=subsheet.Cells(Bendrow, "B")
                    mainsheet.Range(mainsheet.Cells(i, "B"), mainsheet.Cells(i, "O")).Delete '~~> if you want to delete
                    Bendrow = Bendrow + 1
                End If
            Next
    End With
End Sub

MainSheet Data

SubSheet Data

1 个答案:

答案 0 :(得分:0)

问题是DestinationRange.Copy的{​​{1}}参数需要是一个范围。您已将其设置为范围的Range.Cut方法。

不正确

  

mainsheet.Range(mainsheet.Cells(i,“B”),mainsheet.Cells(i,“O”))。剪切目的地:= subsheet.Range(subsheet.Cells(i + 56,“B”)) ,subsheet.Cells(i + 56,“O”))。粘贴

正确

  

mainsheet.Range(mainsheet.Cells(i,“B”),mainsheet.Cells(i,“O”))。剪切目的地:= subsheet.Cells(i + 56,“B”)

我使用子范围重构了代码。我认为它给人一种更清洁的外观。

以下是定义相同范围的一些不同方法

  

范围(细胞(I, “A”),将细胞(我, “O”))   细胞(I, “A”)。调整(1,15)   行(i).Range(“A1:O1”)   行(i).Range(“A1:O1”)   行(I).Resize(1,15)   行(2).Columns( “A:O”)

Paste