finaljustice
Board Regular
- Joined
- Oct 6, 2010
- Messages
- 175
Hi there, I've come up with this code, but it doesn't work after I added more "IFs".
This code works perfectly, this was me initially developing the idea:
Now when I try and enhance to do the same thing but now with more IFs the code says "DO without LOOP" Error.
This code does'nt work:
Could anyone shine a light for me why does it work with a single IF and not the others? How should this be structured so that I go down the list once only.
Thank you for your attention.
Final
This code works perfectly, this was me initially developing the idea:
Code:
Sub Estrutura()
Application.ScreenUpdating = False
Sheets("Base_1").Select
Range("B4").Select
Do While ActiveCell <> Empty
'2011
If ActiveCell.Offset(0, 166).Value = 2011 Then
If ActiveCell.Offset(0, 155).Value > 0 Then 'vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1,FP1,FQ1").Copy
Sheets("2011").Select
Range("B35").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B35").End(xlUp).Offset(1).EntireRow.Insert
Else ' ñ vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1").Copy
Sheets("2011").Select
Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B1048576").End(xlUp).Offset(1).EntireRow.Insert
End If
End If
Sheets("Base_1").Select
ActiveCell.Offset(1, 0).Select
Loop
Application.ScreenUpdating = True
End Sub
Now when I try and enhance to do the same thing but now with more IFs the code says "DO without LOOP" Error.
This code does'nt work:
Code:
Sub Estrutura()
Application.ScreenUpdating = False
Sheets("Base_1").Select
Range("B4").Select
Do While ActiveCell <> Empty
'2011
If ActiveCell.Offset(0, 166).Value = 2011 Then
If ActiveCell.Offset(0, 155).Value > 0 Then 'vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1,FP1,FQ1").Copy
Sheets("2011").Select
Range("B35").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B35").End(xlUp).Offset(1).EntireRow.Insert
Else ' ñ vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1").Copy
Sheets("2011").Select
Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B1048576").End(xlUp).Offset(1).EntireRow.Insert
End If
Else
'2012
If ActiveCell.Offset(0, 166).Value = 2012 Then
If ActiveCell.Offset(0, 155).Value > 0 Then 'vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1,FP1,FQ1").Copy
Sheets("2012").Select
Range("B35").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B35").End(xlUp).Offset(1).EntireRow.Insert
Else ' ñ vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1").Copy
Sheets("2012").Select
Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B1048576").End(xlUp).Offset(1).EntireRow.Insert
End If
Else
'2013
If ActiveCell.Offset(0, 166).Value = 2013 Then
If ActiveCell.Offset(0, 155).Value > 0 Then 'vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1,FP1,FQ1").Copy
Sheets("2013").Select
Range("B35").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B35").End(xlUp).Offset(1).EntireRow.Insert
Else ' ñ vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1").Copy
Sheets("2013").Select
Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B1048576").End(xlUp).Offset(1).EntireRow.Insert
End If
Else
'2014
If ActiveCell.Offset(0, 166).Value = 2014 Then
If ActiveCell.Offset(0, 155).Value > 0 Then 'vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1,FP1,FQ1").Copy
Sheets("2014").Select
Range("B35").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B35").End(xlUp).Offset(1).EntireRow.Insert
Else ' ñ vantajoso
Rows(ActiveCell.Row).Range("B1,V1,Z1,AC1,FN1,FK1").Copy
Sheets("2014").Select
Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValuesAndNumberFormats)
Range("B1048576").End(xlUp).Offset(1).EntireRow.Insert
End If
End If
Sheets("Base_1").Select
ActiveCell.Offset(1, 0).Select
Loop
Application.ScreenUpdating = True
End Sub
Could anyone shine a light for me why does it work with a single IF and not the others? How should this be structured so that I go down the list once only.
Thank you for your attention.
Final