2016-05-25 16 views
0

範囲(dateRng)の日付が今日の日付よりも小さいかどうかを確認するアクションを実行しようとしています。列の値はゼロです。 (私はローンを払っていますが、毎月、払い出されたローンを隠すことを望んでいます。)月間は列を横切っており、ローンは列に並んでいます。貸出残高は(i、j)です。forループからif文vbaへのブレーク

問題は、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 

答えて

0

私は変更を示すためにコードにコメントを追加しました。

あなたは近くにいましたが、ループが1対多であり、出口のために適切な場所を見つける必要がありました。

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 
+0

うまくいった!どうもありがとうございます! – aoman49