Change the value on a variable in the loop "For...next" depending on the condition if

Gwhaou

Board Regular
Joined
May 10, 2022
Messages
78
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello

I have a code, with a loop (for...next to check lines) to check value on cell (line 1) and the next cell (line 2) to assemble them or not :

VBA Code:
Sub loop_check()

Dim lastline As Long
Dim i As Long


lastline = Sheets("Data_Sheet").Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastline

    If (Range("J" & i).Value = Range("B" & i + 1).Value) Then
    
'Condition to check if value on the actual line and the next line are the same 
           Sheets("Data_Sheet").Range("A" & i).Resize(, 6).Copy Destination:=Sheets("Sheet_SameV").Range("A" & Rows.Count).End(xlUp).Offset(1)
           Sheets("Data_Sheet").Range("A" & i + 1).Resize(, 6).Copy Destination:=Sheets("Sheet_SameV").Range("G" & Rows.Count).End(xlUp).Offset(1)
'If they are equal just copy those rows and export on another sheet on the same rows called Sheet_SameV
    
    
    ElseIf (Range("J" & i).Value <> Range("B" & i + 1).Value) Then

'Condition to check if value on the actual line and the next line are not the same
            Sheets("Data_Sheet").Range("A" & i).EntireRow.Copy Destination:=Sheets("Sheet_Solo").Range("A" & Rows.Count).End(xlUp).Offset(1)
'If the values are different copy just the actual row i on another sheet called Sheet_Solo
    
    Else
   
           MsgBox ("No data")
    
    Exit Sub
    
    End If
    
Next

End Sub


I warn you, I'm totally new to vba, i don't know if I did the correct thing.
The actual code is not totally completed.
Because the actual loop check line by line the conditions.

But i want to jump lines depending the case :

Case 1 : Value on cell (F) line i and value on cell (B) line i+1 are equal
In that case the code will export those two lines i and i+1 on another sheet

Case 2 : Value on cell (F) line i and value on cell (B) line i+1 are not equal
In that case, the code will just export the first line i

Because if the code detect a case 1 so the data on row i and i +1 are already copied to another sheet, we dont have to check the next line, so for the next loop have to positioned i +2.
And if the code detect a case 2 so the code will just copy the row i and go to the next row to check if it's a case 1 or to, in that case the loop has to go to the next line because i just copied one line.

Hi need your help to configure the loop depending on the case to modify the variable i.🙏

If it's case 1 jump those 2 lines
If it's case 2 go to the next line

I don't know if my explanation was clear, i'm not that good in English sorry 😅
 

Attachments

  • Sample2.PNG
    Sample2.PNG
    145 KB · Views: 20

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
After copying the 2 lines that are equal and before the ElseIf just add the line.
VBA Code:
i = i + 1
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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