我有以下代码,VBA给了我一个" Next Without For"当我肯定两者都错了。我在网上查了一下,明白这是因为很多ifs,有人能告诉我我的问题在哪里吗?
我有3个workbbs,我希望根据第I列中的nimber将每行剪切并复制到正确的工作簿中。
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
答案 0 :(得分:5)
VBA打算通过支持缩进来帮助避免此类错误。你应该尝试实现的逻辑如下: -
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
我已根据上述原则重新安排了您的代码。这就是结果。
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