我一直很难用更简短的方式写这个,我知道它可能很简单,但是当范围改变时,我无法理解嵌套ifs,任何建议?
数据分为7列,周日至周六 基本上我需要弄清楚报告中有哪些日期数据来自M3:Sx,所以目前我假设整整一周,然后向后工作以检查没有数据。
有些日子可能是空白,所以周日可能有数据,然后没有数据,直到星期四,所以我需要iDayNumber在这种情况下为5
iDayNumber = 7
If WorksheetFunction.CountA(.Range("S" & iFirstRow & ":S" & iLastRow)) = 0 Then
iDayNumber = iDayNumber - 1
If WorksheetFunction.CountA(.Range("R" & iFirstRow & ":R" & iLastRow)) = 0 Then
iDayNumber = iDayNumber - 1
If WorksheetFunction.CountA(.Range("Q" & iFirstRow & ":Q" & iLastRow)) = 0 Then
iDayNumber = iDayNumber - 1
If WorksheetFunction.CountA(.Range("P" & iFirstRow & ":P" & iLastRow)) = 0 Then
iDayNumber = iDayNumber - 1
If WorksheetFunction.CountA(.Range("O" & iFirstRow & ":O" & iLastRow)) = 0 Then
iDayNumber = iDayNumber - 1
If WorksheetFunction.CountA(.Range("N" & iFirstRow & ":N" & iLastRow)) = 0 Then
iDayNumber = iDayNumber - 1
If WorksheetFunction.CountA(.Range("M" & iFirstRow & ":M" & iLastRow)) = 0 Then
Application.ScreenUpdating = True
If MsgBox("There is no data in the current week." & vbCrLf & "Cannot Continue.", _
vbOKOnly + vbCritical, "Aborting") = vbOK Then
Exit Sub
End If
End If
End If
End If
End If
End If
End If
End If
End If
答案 0 :(得分:2)
尝试使用具有偏移功能的for循环
For i = 6 To 0 Step -1
If WorksheetFunction.CountA(.Range("M" & iFirstRow & ":M" & iLastRow).Offset(0, i)) = 0 Then
iDayNumber = iDayNumber - 1
Else
Exit For
End If
Next i