如何写这个VBA代码更短/整洁

我一直在写这个很简短的方式很难,我知道它可能很简单,但我不能让我的头周围有嵌套的ifs,而范围的变化,有什么build议吗?

数据是在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 

尝试使用Offset函数的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