I want to paste cells across worksheets in VBA. In the code below, I first select the range of cells, and then paste to another worksheet. But it runs error '9": Subscript out of range. I think the problem is in the last line for copy & paste. Here's my code:
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
Many thanks!
Sheet22.Range()
vsWorksheets("Sheet21")
. I'd pick one way and stick with that. That being said, which line throws the error? (Click "Debug" when the error pops up). Also, are the origin and destination ranges the same size? – BruceWayneSet StartCell = Range("A1")
I'd fully qualify this line then try again -Set StartCell = Sheet22.Range("A1")
– dwirony