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

时间:2019-05-21 09:04:11

标签: excel vba ms-error-9

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

尽管代码的前半部分全部正常工作,并且允许我将其单元格中的信息复制到包含数据表的另一页上-使用相同的方法将信息从工作表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列中的下一个空单元格中。

1 个答案:

答案 0 :(得分: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