Excel VBA cut and paste function with subscript out of range error

rickyyy2006

New Member
Joined
Nov 21, 2016
Messages
8
I would like to write a program with the below function
If Cell ("J" i) or Cell("L" i) or Cell("N" i) in Sheet1 is not empty where i equals to any integer
Then cut Cell("B" i) to Cell("O" i) in Sheet 1 to Cell("B" i+25) to Cell("O" i+25) in Sheet2 where i equals to any integer
However,my code was said to be out of range.
I am wondering if the issue exists because the " i " in my code cannot reach the data in my excel. Thus, i have attached the below 2 links which involve my excel data

MainSheet:
XlCKg.jpg

SubSheet:
yn5b9.jpg


how can i fix it? Many thanks

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;"> Sub CutandPaste()
With ActiveSheet
Dim i As Long
Dim Sheet1 As Worksheet
Dim Sheet2 As Worksheet

Set mainsheet = ActiveWorkbook.Sheets("Sheet1")
Set subsheet = ActiveWorkbook.Sheets("Sheet2")

endrow
= mainsheet.Range("A" & mainsheet.Rows.Count).End(xlUp).Row

For i = endrow To 25 Step -1

If mainsheet.Cells(i, "J") <> "" Or mainsheet.Cells(i, "L") <> "" Or mainsheet.Cells(i, "N") <> "" Then
mainsheet
.Range(mainsheet.Cells(i, "B"), mainsheet.Cells(i, "O")).Cut Destination:=subsheet.Range(subsheet.Cells(i + 56, "B"), subsheet.Cells(i + 56, "O")).Paste
mainsheet
.Range(mainsheet.Cells(i, "B"), mainsheet.Cells(i, "O")).Delete '~~> if you want to delete

End If
Next
End With
End Sub</code>
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
just a guess...

1. Check that the lack of spacing in your post is not what is reflected in your actual code... I'm looking at Dim i AsLong and For i = endrow To25Step-1 for example... those would probably result in errors if they are not spaced out properly. But it might just be the formatting on this message board...

2. I do not think you need .Paste after a .Cut Destination:= I think the paste part is implied. Just end it with the closing ))

3. You do not use your With statement... You can remove With ActiveSheet and End With without consequence since you are clear in defining your sheet names with variables and use them consistently throughout the code...
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top