将单元格粘贴到 VBA 中的另一个工作表
Paste cells to another worksheet in VBA
我想在 VBA 中跨工作表粘贴单元格。在下面的代码中,我首先 select 单元格区域,然后粘贴到另一个工作表。但它运行错误“9”:下标超出范围。我认为问题出在复制和粘贴的最后一行。这是我的代码:
Sub MatchFRB()
' find last row and column
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set StartCell = Range("A1")
LastRow = Sheet22.Cells(Sheet22.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = Sheet22.Cells(StartCell.Row, Sheet22.Columns.Count).End(xlToLeft).Column
' Select cells until meets Threshold=5000000000
Dim i As Integer
Dim Bal As Double
Threshold = 0
For i = 2 To LastRow
Bal = Threshold + Range("AV" & i)
If Threshold > 5000000000# Then
Exit For
End If
Next i
' copy cells from Sheet22 and paste to Sheet21
Sheet22.Range(StartCell, Sheet22.Cells(i, LastColumn)).Copy Worksheets("Sheet21").Range(StartCell, Sheet21.Cells(i, LastColumn))
End Sub
非常感谢!
您必须正确调用您的sheet。 VBA 不只接受 sheet 的名称作为对象。您必须使用 Worksheets("Sheet22")
引用 sheet,另一种选择是将对象设置为:
Dim ws as object
set ws = Thisworkbook.Worksheets("Sheet22")
这样 VBA 就知道您想要书中包含宏的 Sheet22;否则您可以使用 Workbooks("YourWorkBookName").WorkSheets("SheetName")
指定工作簿。
从那里您可以像使用 Sheet22
一样使用 ws.Range
。同样,StartCell
可能是一个范围,但它只作用于活动的 sheet,所以将它引用到某个 sheet and/or 也未尝不可。书也一样。但在这种情况下,我将其省略,因为它始终是 A1
,而且输入起来很简单。
稍后在您的代码中尝试计算余额时,您还必须在调用范围后使用 .Value
,以便您实际访问存储在单元格中的数字。但是,如果您要检查的是阈值,则应该将 threshold
添加回自身。但是,在这种情况下我选择只使用 Bal
,因为它对我来说更有意义。
Sub MatchFRB()
' find last row and column
Dim LastRow As Long
Dim LastColumn As Long
Dim ws as object
Dim i As Integer
Dim Bal As Double
set ws = Thisworkbook.Worksheets("Sheet22")
LastRow = ws.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
LastColumn = ws.Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
' Select cells until meets Threshold=5000000000
Bal = 0
For i = 2 To LastRow
Bal = Bal + ws.Range("AV" & i).Value
If Bal >= 5000000000 Then
Exit For
End If
Next i
' copy cells from Sheet22 and paste to Sheet21
ws.Range("A1:" & Cells(i, LastColumn).Address).Copy Worksheets("Sheet21").Range("A1:", Cells(i, LastColumn).address)
End Sub
我想在 VBA 中跨工作表粘贴单元格。在下面的代码中,我首先 select 单元格区域,然后粘贴到另一个工作表。但它运行错误“9”:下标超出范围。我认为问题出在复制和粘贴的最后一行。这是我的代码:
Sub MatchFRB()
' find last row and column
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set StartCell = Range("A1")
LastRow = Sheet22.Cells(Sheet22.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = Sheet22.Cells(StartCell.Row, Sheet22.Columns.Count).End(xlToLeft).Column
' Select cells until meets Threshold=5000000000
Dim i As Integer
Dim Bal As Double
Threshold = 0
For i = 2 To LastRow
Bal = Threshold + Range("AV" & i)
If Threshold > 5000000000# Then
Exit For
End If
Next i
' copy cells from Sheet22 and paste to Sheet21
Sheet22.Range(StartCell, Sheet22.Cells(i, LastColumn)).Copy Worksheets("Sheet21").Range(StartCell, Sheet21.Cells(i, LastColumn))
End Sub
非常感谢!
您必须正确调用您的sheet。 VBA 不只接受 sheet 的名称作为对象。您必须使用 Worksheets("Sheet22")
引用 sheet,另一种选择是将对象设置为:
Dim ws as object
set ws = Thisworkbook.Worksheets("Sheet22")
这样 VBA 就知道您想要书中包含宏的 Sheet22;否则您可以使用 Workbooks("YourWorkBookName").WorkSheets("SheetName")
指定工作簿。
从那里您可以像使用 Sheet22
一样使用 ws.Range
。同样,StartCell
可能是一个范围,但它只作用于活动的 sheet,所以将它引用到某个 sheet and/or 也未尝不可。书也一样。但在这种情况下,我将其省略,因为它始终是 A1
,而且输入起来很简单。
稍后在您的代码中尝试计算余额时,您还必须在调用范围后使用 .Value
,以便您实际访问存储在单元格中的数字。但是,如果您要检查的是阈值,则应该将 threshold
添加回自身。但是,在这种情况下我选择只使用 Bal
,因为它对我来说更有意义。
Sub MatchFRB()
' find last row and column
Dim LastRow As Long
Dim LastColumn As Long
Dim ws as object
Dim i As Integer
Dim Bal As Double
set ws = Thisworkbook.Worksheets("Sheet22")
LastRow = ws.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
LastColumn = ws.Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
' Select cells until meets Threshold=5000000000
Bal = 0
For i = 2 To LastRow
Bal = Bal + ws.Range("AV" & i).Value
If Bal >= 5000000000 Then
Exit For
End If
Next i
' copy cells from Sheet22 and paste to Sheet21
ws.Range("A1:" & Cells(i, LastColumn).Address).Copy Worksheets("Sheet21").Range("A1:", Cells(i, LastColumn).address)
End Sub