+ Reply to Thread
Results 1 to 2 of 2

Populate Excel cells based on multiple IF conditions in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    08-30-2012
    Location
    PL
    MS-Off Ver
    Excel 2003; Excel 2007
    Posts
    3

    Post Populate Excel cells based on multiple IF conditions in VBA

    I have a terrible headache on the following conditions:

    The difference between a date given in cell (i, 9) and NOW() should undergo the IF procedures set presented below in the macro - in another cell (i, 13) it should display certain text with coloring (warning).
    However, if (i, 9) is empty, then cell (i, 10) should follow the same set of IFs.
    If there is nothing in both cells, the warning cell (i, 13) should remain empty.

    I was able to create only the first condition, which is working fine, however, I have no idea how to approach the second one. Does anyone have an idea how to create a loop where this would work? Unless I did not describe the issue clearly enough. Thank you in advance!


    Sub CheckDate()
    Dim a, select1, from1, where1, As String
    
        Worksheets("Temporary").Activate
        Range("A3:IV65000").ClearContents
            
        select1 = "SELECT " & Worksheets("Generate Report").Range("B4") & " "
        from1 = "FROM " & Worksheets("Generate Report").Range("C4") & " "
        where1 = "WHERE " & Worksheets("Generate Report").Range("D4") & " "
        a = select1 & from1 & where1 & orderedBy
            
        Worksheets("Temporary").Activate
        Cells(2, 1).Select
            
        getData (a)
          
    i = 3
    
        Do While (Cells(i, 1).Value <> "")
        
        difference = (DateDiff("d", Int(Cells(i, 9)), Int(Now())))
        
      '  difference2 = (DateDiff("d", Int(Cells(i, 10)), Int(Now())))
    
            If difference >= 30 And difference < 40 And Cells(i, 9) <> "" Then
                Cells(i, 13).Interior.Color = vbYellow
                Cells(i, 13).Font.Color = vbBlack
                Cells(i, 13) = "30 days warning"
            End If
            
            If difference >= 40 And difference < 45 And Cells(i, 9) <> "" Then
                Cells(i, 13).Interior.ColorIndex = 45
                Cells(i, 13).Font.Color = vbBlack
                Cells(i, 13) = "40 days warning"
            End If
            
           If difference > 45 And Cells(i, 9) <> "" Then
                Cells(i, 13).Interior.Color = vbRed
                Cells(i, 13).Font.Color = vbWhite
                Cells(i, 13) = "45 days warning"
            End If
              
            If difference < 30 Or Cells(i, 9) = "" Then
                Cells(i, 13).Interior.Color = vbWhite
            End If
        
        i = i + 1
        
        Loop    
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Populate Excel cells based on multiple IF conditions in VBA

    Hi

    Try this.

    Sub CheckDate()
      Dim a, select1, from1, where1 As String
    
        Worksheets("Temporary").Activate
        Range("A3:IV65000").ClearContents
            
        select1 = "SELECT " & Worksheets("Generate Report").Range("B4") & " "
        from1 = "FROM " & Worksheets("Generate Report").Range("C4") & " "
        where1 = "WHERE " & Worksheets("Generate Report").Range("D4") & " "
        a = select1 & from1 & where1 & orderedBy
            
        Worksheets("Temporary").Activate
        Cells(2, 1).Select
            
        GetData (a)
          
        i = 3
    
        'Do While (Cells(i, 1).Value <> "")
        For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
          If Len(Cells(i, 9)) = 0 And Len(Cells(i, 10)) = 0 Then
            Cells(i, 13).ClearContents
          Else
            docol = 9
            If Len(Cells(i, 9)) = 0 Then docol = 10
            difference = (DateDiff("d", Int(Cells(i, docol)), Int(Now())))
        
      '  difference2 = (DateDiff("d", Int(Cells(i, 10)), Int(Now())))
    
            If difference >= 30 And difference < 40 Then
              Cells(i, 13).Interior.Color = vbYellow
              Cells(i, 13).Font.Color = vbBlack
              Cells(i, 13) = "30 days warning"
            End If
            
            If difference >= 40 And difference < 45 Then
              Cells(i, 13).Interior.ColorIndex = 45
              Cells(i, 13).Font.Color = vbBlack
              Cells(i, 13) = "40 days warning"
            End If
            
            If difference > 45 Then
              Cells(i, 13).Interior.Color = vbRed
              Cells(i, 13).Font.Color = vbWhite
              Cells(i, 13) = "45 days warning"
            End If
              
            If difference < 30 Or Cells(i, 9) = "" Then
              Cells(i, 13).Interior.Color = vbWhite
            End If
          End If
        
          'i = i + 1
        
        'Loop
        Next i
        
    End Sub
    If that doesn't really do what you are chasing, then add an example file. I'm not sure about the last test you have that you want to really clear out column 13 if column 9 is blank...

    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1