VBA Code Add Condition

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,539
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I am using the this VBA Code to format dates which are greater than or equal to today.

I require to add another condition i.e. if the adjacent cell (right one) is empty then only the code should format dates otherwise nothing should happen.


Code:
Sub formatdates()

Dim rngCell As Range
Dim lngLstRow As Long
Dim strCol(1 To 9) As String


strCol(1) = "N"
strCol(2) = "P"
strCol(3) = "R"
strCol(4) = "T"
strCol(5) = "V"
strCol(6) = "X"
strCol(7) = "Z"
strCol(8) = "AB"
strCol(9) = "AD"


lngLstRow = ActiveSheet.UsedRange.Rows.Count


    For I = 1 To 9
        For Each rngCell In Range(strCol(I) & "4:" & strCol(I) & lngLstRow)
            If rngCell.Value >= Now() Then
                rngCell.Font.ColorIndex = 3
                
            Else
                rngCell.Font.ColorIndex = 1
                
            End If
        Next
    Next I


End Sub

Any help would be appreciated.

Regards,

Humayun
 
Let's stick to one problem at a time. You have not changed the if statements as I showed.
Also NEVER use "On Error Resume Next" in that manner, as it will simply hide problems.

Well I did change as you said so
but getting debug msg. Compile error ... End with without with
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Can you post that code?

Sure, why not

Code:
Sub formatdates()

Dim rngCell As Range
Dim lngLstRow As Long
Dim strCol(1 To 9) As String


strCol(1) = "N"
strCol(2) = "P"
strCol(3) = "R"
strCol(4) = "T"
strCol(5) = "V"
strCol(6) = "X"
strCol(7) = "Z"
strCol(8) = "AB"
strCol(9) = "AD"


lngLstRow = ActiveSheet.UsedRange.Rows.Count


   For I = 1 To 9
       For Each rngCell In Range(strCol(I) & "4:" & strCol(I) & lngLstRow)
         With rngCell
         
      If .Value <> "" Then
            
    If .Offset(, 1).Value = "" And .Value < Date And Date - .Value > 1 And .Value <> "" Then
     .Resize(, 2).Font.ColorIndex = 3
     .Offset(, 1).Value = Date - .Value & " Days"
            
    ElseIf .Offset(, 1).Value = "" And .Value < Date And Date - .Value = 1 And .Value <> "" Then
     .Resize(, 2).Font.ColorIndex = 3
     .Offset(, 1).Value = Date - .Value & " Day"
            
    ElseIf .Offset(, 1).Value = "" And .Value = Date And .Value <> "" Then
     .Resize(, 2).Font.ColorIndex = 3
     .Offset(, 1).Value = "Today"
            
    
     
            Else
               .Resize(, 2).Font.ColorIndex = 1
            End If
         
         If .Value = "Not Applicable" Then
         .Resize(, 2).Font.ColorIndex = 1
         
         End If
         End With
      Next rngCell
   Next I
End Sub
 
Upvote 0
How about
Code:
   For i = 1 To 9
      For Each rngCell In Range(strCol(i) & "4:" & strCol(i) & lngLstRow)
         With rngCell
            If .Value <> "" Then
               If .Offset(, 1).Value = "" And .Value < Date And Date - .Value > 1 Then
                  .Resize(, 2).Font.ColorIndex = 3
                  .Offset(, 1).Value = Date - .Value & " Days"
               ElseIf .Offset(, 1).Value = "" And .Value < Date And Date - .Value = 1 Then
                  .Resize(, 2).Font.ColorIndex = 3
                  .Offset(, 1).Value = Date - .Value & " Day"
               ElseIf .Offset(, 1).Value = "" And .Value = Date Then
                  .Resize(, 2).Font.ColorIndex = 3
                  .Offset(, 1).Value = "Today"
               Else
                  .Resize(, 2).Font.ColorIndex = 1
               End If
               If .Value = "Not Applicable" Then
                  .Resize(, 2).Font.ColorIndex = 1
               End If
            End If
         End With
      Next rngCell
   Next i
 
Upvote 0
Not working friend.

Run time error 13 type mismatch

This part of the code highlighted

Code:
If .Offset(, 1).Value = "" And .Value < Date And Date - .Value > 1 Then
 
Upvote 0
What is the value of rngCell & the cell to the right when you get the error?
 
Upvote 0
What is the value of rngCell & the cell to the right when you get the error?


Both rngCell & the cell to the right can have below possible values

1) Blank Cell ------------> But if you apply isblank formula to a cell having no values will result FALSE... It wont return true
2) A Date
3) text "Not Applicable"

Please note that all above possible values are copied from some other sheet & they are results of a formula and then pasted here as paste special values.
 
Upvote 0
What are the EXACT values when you get the error?
Also for the date is you use =ISNUMBER(C2) where C2 is one of the dates, what does it return.
 
Upvote 0
What are the EXACT values when you get the error?
Also for the date is you use =ISNUMBER(C2) where C2 is one of the dates, what does it return.

the error value I get is #N/A & to control this I use iferror function to not to get errors

For example this formula gives me an error #N/A
Code:
=IF(AN726="Not Applicable","Not Applicable",INDEX(fri_dpi_labtest_date,MATCH(1,($A726=fri_dpi_labtest_po)*("FRI 3"=fri_dpi_labtest_category),0)))

So to control this I applied iferror function
Code:
=IFERROR(IF(AN726="Not Applicable","Not Applicable",INDEX(fri_dpi_labtest_date,MATCH(1,($A726=fri_dpi_labtest_po)*("FRI 3"=fri_dpi_labtest_category),0))),"")

And where there is a date if I use =isnumber() I get TRUE as a result
 
Upvote 0
Please answer my first question
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top