[英]I am trying to copy and rename a sheet but I get subscript out of range error
[英]Runtime Error 9, Subscript Out of Range - Trying to copy information to another sheet
我正在整理一段旧代码。 我已经设法解决了一些错误,但这个错误让我感到困惑。
尽管代码的前半部分都在工作并允许我将其单元格中的信息复制到另一个由数据表组成的页面 - 使用相同的方法将信息从工作表 8 移动到第 3 页是行不通的。
如果我删除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 列中的下一个空单元格。
您可以进行更改并尝试代码。 如果您有任何问题,请告诉我
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.