Reputation: 740
I have the following code and VBA is giving me a "Next Without For" Error when I definitely have both. I checked on the internet and understand that it is because of many ifs, can someone tell me where is my problem?
i have 3 workbboks and I want that each row will be cut and copied into the right workbook acording to the nimber in column I.
Dim i As Integer
Dim RowTable As Integer
RowTable = Sheets(3).Range("A1", (Range("A1").End(xlDown))).Rows.count
'loop
For i = 2 To RowTable
'SORTING DATA TO WORKBOOKS
'Oren, Karin, Noam
'Oren
If Selection.Value = 1 Then
Selection.EntireRow.Cut
WB1.Activate
WB1.Sheets(1).Range("A1").Offset(1, 0).Select
If Selection.Value = "" Then
Selection.PasteSpecial (xlPasteValues)
Else
Range("A1").End(xlDown).Offset(1, 0).PasteSpecial (xlPasteValues)
End If
Else
'Karin
If Selection.Value = 2 Then
Selection.EntireRow.Cut
WB1.Activate
WB1.Sheets(1).Range("A1").Offset(1, 0).Select
If Selection.Value = "" Then
Selection.PasteSpecial (xlPasteValues)
Else
Range("A1").End(xlDown).Offset(1, 0).PasteSpecial (xlPasteValues)
End If
Else
'Noam
If Selection.Value = 3 Then
Selection.EntireRow.Cut
WB1.Activate
WB1.Sheets(1).Range("A1").Offset(1, 0).Select
If Selection.Value = "" Then
Selection.PasteSpecial (xlPasteValues)
Else
Range("A1").End(xlDown).Offset(1, 0).PasteSpecial (xlPasteValues)
End If
End If
'next row
mainWB.Activate
Sheets(3).Range("I2").End(xlDown).Select
'i do the same procedure in the indirect manager workbooks as i did in the first workbook
'i will copy the names to a differen range and then remove duplicates
'then i will have a list of direct managers in each wb
'and i will name ech direct manager as a number and open a worksheet based on his name
Next
Upvotes: 0
Views: 129
Reputation: 14373
VBA intends to help avoid such errors as you have by supporting indenting. The logic you should try to implement would look like this:-
For n = 1 to 10
If 1 <> 2 Then
'enter code here
Else
Do
' enter code here
Loop While 0 <> 0
End If
Next n
I have rearranged your code according to the above principle. This is the result.
Private Sub FormattedCode()
Dim i As Integer
Dim RowTable As Integer
RowTable = Sheets(3).Range("A1", (Range("A1").End(xlDown))).Rows.Count
'loop
For i = 2 To RowTable
'SORTING DATA TO WORKBOOKS
'Oren, Karin, Noam
If Selection.Value = 1 Then ' Oren
Selection.EntireRow.Cut
WB1.Activate
WB1.Sheets(1).Range("A1").Offset(1, 0).Select
If Selection.Value = "" Then
Selection.PasteSpecial (xlPasteValues)
End If ' this I inserted
Else
Range("A1").End(xlDown).Offset(1, 0).PasteSpecial (xlPasteValues)
End If
' Else ' this is illegal here
If Selection.Value = 2 Then 'Karin
Selection.EntireRow.Cut
WB1.Activate
WB1.Sheets(1).Range("A1").Offset(1, 0).Select
If Selection.Value = "" Then
Selection.PasteSpecial (xlPasteValues)
End If ' this I inserted
Else
Range("A1").End(xlDown).Offset(1, 0).PasteSpecial (xlPasteValues)
End If
' Else ' this is illegal here
If Selection.Value = 3 Then ' Noam
Selection.EntireRow.Cut
WB1.Activate
WB1.Sheets(1).Range("A1").Offset(1, 0).Select
If Selection.Value = "" Then
Selection.PasteSpecial (xlPasteValues)
End If ' this I inserted
Else
Range("A1").End(xlDown).Offset(1, 0).PasteSpecial (xlPasteValues)
End If
' End If ' superfluous
'next row
mainWB.Activate
Sheets(3).Range("I2").End(xlDown).Select
'i do the same procedure in the indirect manager workbooks as i did in the first workbook
'i will copy the names to a differen range and then remove duplicates
'then i will have a list of direct managers in each wb
'and i will name ech direct manager as a number and open a worksheet based on his name
Next
End Sub
Upvotes: 5