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:
SubSheet:
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>
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:
SubSheet:
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>