运行时错误9,下标超出范围-试图将信息复制到另一张表中

问题描述 投票:0回答:1

我正在整理一段旧代码。我设法摆动了一些错误,但是这个错误让我感到困惑。

尽管所有前半段代码都有效,并且允许我将其单元格中的信息复制到包含数据表的另一页上-使用相同的方法将信息从工作表8移到第三页上是行不通的。

如果我删除Sheets(8).Range("A1").Value并将其替换为A1,则该行会运行,因此我认为这是一个问题吗?

我认为在这一点以下,我还会问几个错误(因为错误9再次出现)。

这是下面的代码,错误的行是:

.Range("B" & RowToPasteTo).Value = Sheets("Config").Range("A1").Value
Sub PostToPipeline()
    With ThisWorkbook.Sheets(3)
        Dim RowToPasteTo As Long
        RowToPasteTo = .Cells(.Rows.Count, "B").End(xlUp).Row + 1

        'This defines the variable RowToPasteTo as the next empty line on the Pipeline

        .Range("D" & RowToPasteTo).Value = Sheets(2).Range("C4").Value 'This prints the clients name to pipeline.With the left of the '=' being the destination, and the right being the source of information.
        .Range("E" & RowToPasteTo).Value = Sheets(2).Range("C5").Value 'This prints TAS Principal
        .Range("F" & RowToPasteTo).Value = Sheets(2).Range("C6").Value 'This prints Pillar

        .Range("C" & RowToPasteTo).Value = Date 
        .Range("J" & RowToPasteTo).Value = Date 

        ThisWorkbook.Sheets(8).Range("A1").Value = ThisWorkbook.Sheets(8).Range("A1").Value + 1 'Sheet makes a permanent variable for reference and adds one with each use.

        .Range("B" & RowToPasteTo).Value = Sheets(8).Range("A1").Value

        Sheets(1).Range("C4:C11").Select.ClearContents 'Clears the information held in input table.
        Range("A1").Select

        Sheets(3).Range("A1").Select 'Deselects range
    End With
End Sub

我想要将工作表8中的单元格A1的值(“配置”)发布到B列中的下一个空单元格。

excel vba
1个回答
1
投票

您可以进行更改并尝试代码。如果您有任何问题,请让我知道

Option Explicit

Sub PostToPipeline()

    Dim RowToPasteTo As Long
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws8 As Worksheet

    With ThisWorkbook
        Set ws1 = .Sheets(1) 'Change the number in the bracket with the name of the worksheet ( e.g. .Sheets("Sheet1"))
        Set ws2 = .Sheets(2) 'Change the number in the bracket with the name of the worksheet ( e.g. .Sheets("Sheet1"))
        Set ws3 = .Sheets(3) 'Change the number in the bracket with the name of the worksheet ( e.g. .Sheets("Sheet1"))
        Set ws8 = .Sheets(8) 'Change the number in the bracket with the name of the worksheet ( e.g. .Sheets("Sheet1"))
    End With

    With ws3

        RowToPasteTo = .Cells(.Rows.Count, "B").End(xlUp).Row + 1

        'This defines the variable RowToPasteTo as the next empty line on the Pipeline

        .Range("D" & RowToPasteTo).Value = ws2.Range("C4").Value 'This prints the clients name to pipeline.With the left of the '=' being the destination, and the right being the source of information.
        .Range("E" & RowToPasteTo).Value = ws2.Range("C5").Value 'This prints TAS Principal
        .Range("F" & RowToPasteTo).Value = ws2.Range("C6").Value 'This prints Pillar

        .Range("C" & RowToPasteTo).Value = Date
        .Range("J" & RowToPasteTo).Value = Date

        .Range("B" & RowToPasteTo).Value = ws8.Range("A1").Value

        .Range("A1").Select 'Deselects range

    End With

    ws8.Range("A1").Value = ws8.Range("A1").Value + 1 'Sheet makes a permanent variable for reference and adds one with each use.
    ws1.Range("C4:C11").ClearContents 'Clears the information held in input table.

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.