我收到编译错误Next without For
,我不确定原因。这也是我第一次尝试这个特殊的代码片段,我需要知道我的**/01/**
搜索实际上只会搜索'月份'在日期字段内?
下面是我的代码。
Private Sub LeaveDateCheck()
Dim wstSource As Worksheet, _
wstOutput As Worksheet
Dim rngCell As Range, _
rngMyData As Range
Dim lngMyRow As Long
Dim Usdrws As Long, LastRow As Long
'Call AddLeaveDateTable
Usdrws = Worksheets("EeeDetails").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("EeeDetails").Range("AB2:AB" & Usdrws).FormulaR1C1 = "=iferror(vlookup(RC[-2],LeaveDate!C[-27]:C[-26],2,False),RC[-2])"
Worksheets("EeeDetails").Range("AB:AB").NumberFormat = "dd/mm/yyyy"
Set wstSource = Worksheets("EeeDetails") 'Tab containing source data. Change to suit.
Set wstOutput = Worksheets("Errors") 'Tab for output (duplicate) data. Change to suit.
Set rngMyData = wstSource.Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) 'Column range to check for duplicates. Change to suit.
Application.ScreenUpdating = False
For Each rngCell In rngMyData
rownumber = Split(rngCell.Address, "$")(2)
LeaveDate = Worksheets("EeeDetails").Range("Y" & rownumber).Value
LastPayDate = Worksheets("EeeDetails").Range("AB" & rownumber).Value
lngMyRow = wstOutput.Cells(Rows.Count, "A").End(xlUp).Row + 1
If LeaveDate <> "" Then
If LastPayDate <> "" Then
If LastPayDate <> "**/**/**" Then
If LastPayDate = "Wk 5" Then
If LeaveDate = "**/04/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/05/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 9" Then
If LeaveDate = "**/05/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/06/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 13" Then
If LeaveDate = "**/06/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/07/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 14" Then
If LeaveDate = "**/06/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/07/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 18" Then
If LeaveDate = "**/07/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/08/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 22" Then
If LeaveDate = "**/08/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/09/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 26" Then
If LeaveDate = "**/09/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/10/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 27" Then
If LeaveDate = "**/09/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/10/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 31" Then
If LeaveDate = "**/10/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/11/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 35" Then
If LeaveDate = "**/11/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/12/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 39" Then
If LeaveDate = "**/12/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/01/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 40" Then
If LeaveDate = "**/12/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/01/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 44" Then
If LeaveDate = "**/01/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/02/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LastPayDate = "Wk 48" Then
If LeaveDate = "**/02/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/03/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
End If
If CDate(LeaveDate) > CDate(LastPayDate) Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
End If
End If
Next rngCell
End Sub
此外,我确信这段代码可以缩小,稍后我会考虑这样做,只是为了让它在当下工作!
答案 0 :(得分:3)
在评论中写这篇文章有点长而且很难,但基本上你错过了End If
的{{1}}个Ifs
。
例如,如果您正确缩进代码:
If LastPayDate = "Wk 5" Then
If LeaveDate = "**/04/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/05/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
' <--- MISSING End If HERE
If LastPayDate = "Wk 9" Then
If LeaveDate = "**/05/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
If LeaveDate = "**/06/**" Then
Worksheets("EeeDetails").Range(LeaveDate, LastPayDate).Interior.ColorIndex = 3
Worksheets("EeeDetails").Range("AA" & rownumber).Value = rownumber
wstSource.Range("A" & rngCell.Row & ":AA" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":AA" & lngMyRow)
End If
' <--- MISSING End If HERE
' and so on until the end of your code
建议:尝试使用Select Case LastPayDate
,它会帮助您“清理”一些代码。