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