从 for 循环中断回到 if 语句 vba
Break from for loop back into if statement vba
我正在尝试执行一项操作,查看范围内的日期 (dateRng) 是否小于今天的日期,如果是,则执行 for 循环以隐藏行 w.here a列中的值为零。 (我正在还清贷款,每个月我都希望它隐藏所有已还清的贷款。)月份是跨栏的,贷款是行的。贷款余额为 (i, j).
问题是它永远不会退出 for 循环以在每个新 'j'(列)之后返回并检查日期。它只是停留在 for 循环中。我试过 break、exit、continue 等。None 似乎有效,至少在我放置它们的地方是这样。我如何让它检查日期,与 'today' 比较,然后 运行 for 循环检查列中的每个单元格,然后再转到第 2 列,检查日期并执行相同的操作循环。
最好是动态的,但这不是必需的,因为每个月我都可以更改代码中的范围。这仅供我个人使用。任何帮助表示赞赏。谢谢你。
Sub hidePaid()
Dim day As Range, loanRng As Range, loanSum As Worksheet, dateRng As Range, cel2 As Range, i As Long, j As Long, col As Range
Set loanSum = ThisWorkbook.Worksheets("Loan Sum")
loanSum.Activate
Set dateRng = ActiveSheet.Range("D2:R2")
Set loanRng = ActiveSheet.Range("D4:R16")
For Each day In dateRng
If day.Value < Date Then
For j = 1 To loanRng.Columns.Count
For i = 1 To loanRng.Rows.Count
If loanRng.Cells(i, j).Value < 1 Then
loanRng.Cells(i, j).EntireRow.Hidden = True
End If
Next i
Next j
End If
Next
End sub
我在代码中添加了注释以显示我的更改。
你很接近,但是有一对多的循环,就像你说的,需要找到正确的出口位置。
Sub hidePaid()
Dim day As Range
Dim loanRng As Range
Dim loanSum As Worksheet
Dim dateRng As Range
Dim i As Long
Set loanSum = ThisWorkbook.Worksheets("Loan Sum")
loanSum.Activate
Set dateRng = ActiveSheet.Range("D2:R2")
Set loanRng = ActiveSheet.Range("D4:R16")
'This loop processes by column
For Each day In dateRng
'Once the date in the column is greater than today, it will stop processing
'It assumes the values in dateRng are valid dates
'(I.e. '01/01/2016' not just 'Jan', you can use number format in Excel to
'get a date to show as 'Jan' if that is better for you)
If DateDiff("d", Now(), day.Value) > 0 Then Exit For
'The line of code you had should have worked in sense,
'it would have touched every column but only procesed those before today
'It also assumes that value in the cell to be an actual date
'If day.Value < Date Then
'You do not need a column loop here as you are already in one in the
'previous loop
'For j = 1 To loanRng.Columns.Count
'This loop processes all the rows that are not already hidden and if
'the value is equal to 0 then it hides the row
'Note: you had the check to be less than 1, .50 is less than 1 and you don't
'want to get caught out on a loan!
For i = 1 To loanRng.Rows.Count
If (loanRng.Cells(i, day.Column - 3).Value = 0) And (loanRng.Cells(i, day.Column - 3).EntireRow.Hidden = False) Then
loanRng.Cells(i, day.Column - 3).EntireRow.Hidden = True
End If
Next i
Next
'Its good practice to clear out resources when finishing
Set dateRng = Nothing
Set loanRng = Nothing
Set loanSum = Nothing
End Sub
我正在尝试执行一项操作,查看范围内的日期 (dateRng) 是否小于今天的日期,如果是,则执行 for 循环以隐藏行 w.here a列中的值为零。 (我正在还清贷款,每个月我都希望它隐藏所有已还清的贷款。)月份是跨栏的,贷款是行的。贷款余额为 (i, j).
问题是它永远不会退出 for 循环以在每个新 'j'(列)之后返回并检查日期。它只是停留在 for 循环中。我试过 break、exit、continue 等。None 似乎有效,至少在我放置它们的地方是这样。我如何让它检查日期,与 'today' 比较,然后 运行 for 循环检查列中的每个单元格,然后再转到第 2 列,检查日期并执行相同的操作循环。
最好是动态的,但这不是必需的,因为每个月我都可以更改代码中的范围。这仅供我个人使用。任何帮助表示赞赏。谢谢你。
Sub hidePaid()
Dim day As Range, loanRng As Range, loanSum As Worksheet, dateRng As Range, cel2 As Range, i As Long, j As Long, col As Range
Set loanSum = ThisWorkbook.Worksheets("Loan Sum")
loanSum.Activate
Set dateRng = ActiveSheet.Range("D2:R2")
Set loanRng = ActiveSheet.Range("D4:R16")
For Each day In dateRng
If day.Value < Date Then
For j = 1 To loanRng.Columns.Count
For i = 1 To loanRng.Rows.Count
If loanRng.Cells(i, j).Value < 1 Then
loanRng.Cells(i, j).EntireRow.Hidden = True
End If
Next i
Next j
End If
Next
End sub
我在代码中添加了注释以显示我的更改。
你很接近,但是有一对多的循环,就像你说的,需要找到正确的出口位置。
Sub hidePaid()
Dim day As Range
Dim loanRng As Range
Dim loanSum As Worksheet
Dim dateRng As Range
Dim i As Long
Set loanSum = ThisWorkbook.Worksheets("Loan Sum")
loanSum.Activate
Set dateRng = ActiveSheet.Range("D2:R2")
Set loanRng = ActiveSheet.Range("D4:R16")
'This loop processes by column
For Each day In dateRng
'Once the date in the column is greater than today, it will stop processing
'It assumes the values in dateRng are valid dates
'(I.e. '01/01/2016' not just 'Jan', you can use number format in Excel to
'get a date to show as 'Jan' if that is better for you)
If DateDiff("d", Now(), day.Value) > 0 Then Exit For
'The line of code you had should have worked in sense,
'it would have touched every column but only procesed those before today
'It also assumes that value in the cell to be an actual date
'If day.Value < Date Then
'You do not need a column loop here as you are already in one in the
'previous loop
'For j = 1 To loanRng.Columns.Count
'This loop processes all the rows that are not already hidden and if
'the value is equal to 0 then it hides the row
'Note: you had the check to be less than 1, .50 is less than 1 and you don't
'want to get caught out on a loan!
For i = 1 To loanRng.Rows.Count
If (loanRng.Cells(i, day.Column - 3).Value = 0) And (loanRng.Cells(i, day.Column - 3).EntireRow.Hidden = False) Then
loanRng.Cells(i, day.Column - 3).EntireRow.Hidden = True
End If
Next i
Next
'Its good practice to clear out resources when finishing
Set dateRng = Nothing
Set loanRng = Nothing
Set loanSum = Nothing
End Sub