【问题标题】:Break from for loop back into if statement vba从 for 循环中断回到 if 语句 vba
【发布时间】:2016-05-25 00:52:31
【问题描述】:

我正在尝试执行一个操作来查看范围内的日期 (dateRng) 是否小于今天的日期,如果是,则执行 for 循环以隐藏列中包含值的行为零。 (我正在还清贷款,并且每个月我都希望它隐藏所有已还清的贷款。)月份是跨列的,贷款是在行中的。贷款余额为 (i, j)。

问题在于它永远不会退出 for 循环以返回并检查每个新的“j”(列)之后的日期。它只是停留在 for 循环中。我尝试过中断、退出、继续等。似乎没有一个工作,至少在我放置它们的地方。如何让它检查日期,与“今天”进行比较,然后运行 ​​for 循环检查列中的每个单元格,然后再转到第 2 列,检查日期并执行相同的 for 循环。

最好让它是动态的,但这不是必需的,因为每个月我都可以更改代码中的范围。这仅限于我个人使用。任何帮助表示赞赏。谢谢你。

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

【问题讨论】:

    标签: vba loops if-statement for-loop exit


    【解决方案1】:

    我在代码中添加了 cmets 以显示我的更改。

    你很接近,但有一对多的循环,就像你说的,需要找到合适的出口位置。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多