where is the error in the second IF statement - excel

i have VBA code that check entered dates with the current date and fill the cell in the appropriate color and check if the colomn "F" is not empty it will color the D,E,F columns.
the problem is that i have until now 21 records but the system just color 19 record even so the 2 rows are not empty in the F column.
code:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Cells(i, 3).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(Range("F" & i).Value) <> "" Then Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End Sub

The ElseIf statements will throw Runtime Error 13 if the cells have a non-date value in them. This is caused by trying to convert a non-date value into a date VBA.CDate(Cells(i, 3))
Private Sub CommandButton1_Click()
Dim i As Long
With Worksheets("Sheet1")
For i = Range("C" & .Rows.Count).End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsDate(Cells(i, 3)) Then
Select Case VBA.CDate(.Cells(i, 3)) - VBA.Date()
Case Is < 0
.Cells(i, 3).Interior.Color = vbGreen
Case Is = 0
.Cells(i, 3).Interior.Color = vbYellow
Case Is <= 4
.Cells(i, 3).Interior.Color = vbRed
Case Is <= 10
.Cells(i, 3).Interior.Color = vbCyan
Case Else
.Cells(i, 3).Interior.ColorIndex = xlNone
End Select
Else
.Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(.Range("F" & i).Value) <> "" Then .Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End With
End Sub

Might be something with your data, it runs properly to me. What kind of data do you have in the F column?

Related

Modification of cells

I want to create a program in VBA, that will change color of cell depending on the type. And if the value is negative, font will be bold.
My code:
Sub Tables()
Dim N As Integer
Dim M As Integer
Dim i As Integer
Dim j As Integer
M = InputBox("Number of columns")
N = InputBox("Number of rows")
For i = 0 To N
For j = 0 To M
If IsEmpty(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 0
ElseIf Application.WorksheetFunction.IsText(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 1
ElseIf Application.WorksheetFunction.IsLogical(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 2
ElseIf Application.WorksheetFunction.IsErr(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 3
ElseIf IsDate(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 4
ElseIf Cells(i, j).HasFormula = True Then
Cells(i, j).Interior.ColorIndex = 5
ElseIf IsNumeric(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 6
Else
Cells(i, j).Interior.ColorIndex = 7
End If
If Cells(i, j).Vallue < 0 Then
Cells(i, j).Font.Bold = True
End If
Next j
Next i
End Sub
Unfortunately, I got:
Error 1004 "Application-defined or Object-defined error
There is no row or column 0.
i and j should start at 1.
Also as already pointed out, If Cells(i, j).Value < 0 Then will fail if the cell contains an error value, and will also fail if the cell contains text. Make sure to check if it's a number first.
If IsNumeric(Cells(i, j).Value) Then
Or just move the "negative" logic earlier:
ElseIf IsNumeric(Cells(i, j).Value) = True Then
Cells(i, j).Interior.ColorIndex = 6
If Cells(i, j).Value < 0 Then
Cells(i, j).Font.Bold = True
End If
Else

VBA - Subscript Out of Range Error on MOD command

I am still a novice VBA user. I can't seem to get my head around why this bit of script is not working. I get a
'Subscript Out of Range' error
on the second line in the If statement of the below:
Sub ScreenUpdate()
Dim LastRow As Long, LastColumn As Long, i As Long, j As Long
Application.ScreenUpdating = False
LastRow = Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 17).Value = Cells(i - 1, 17).Value Then
Cells(i, 1).NumberFormat = ";;;"
Cells(i, 2).NumberFormat = ";;;"
Cells(i, 15).NumberFormat = ";;;"
Cells(i, 16).NumberFormat = ";;;"
Else
Cells(i, 1).NumberFormat = "0"
Cells(i, 2).NumberFormat = "0"
Cells(i, 15).NumberFormat = "0"
Cells(i, 16).NumberFormat = "0"
End If
Next i
For j = 2 To LastRow
If Cells(j, 17).Value Mod 2 = 1 Then
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(221, 235, 247)
Else
Range(Cells(j, 1), Cells(j, 16)).Interior.ColorIndex = RGB(255, 255, 255)
End If
Next j
Application.ScreenUpdating = True
End Sub
Many thanks for your assistance!
You've mixed up Color, which you would use the RGB function with, and ColorIndex.
The RGB function returns a Long whole number corresponding to the color value. Specifically in this case,
RGB(221, 235, 247) returns 16,247,773
RGB(255, 255, 255) returns 16,777,215
Both of these are outside the very narrow range of possible values for a color index in the current color palette, hence the subscript out of range.
Change each instance of ColorIndex to Color.

VBA excel how to fix the code (exclude rows that are empty ) do not fill the background in a color

i have a code written in VBA that will check the date and base on the it will fill the background with the appropriate color.
i have cells (A to G ).
i want to check if the column C is empty i want to keep the row transparent
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
the problem is that the code step into the if statment to check if empty ... then it step into the last if statement and fill all the empty rows with the specified color.
CODE:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "G" is not empty
If Trim(Range("G" & i).Value) <> "" Then
Range("A" & i & ":G" & i).Interior.ColorIndex = 15
ElseIf Trim(Range("G" & i).Value) = "" Then
Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
End If
Next
End Sub
Try this, just an added an extra If clause to your second statement
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "G" is not empty
If Not IsEmpty(Cells(i, 3)) Then
If Trim(Range("G" & i).Value) <> "" Then
Range("A" & i & ":G" & i).Interior.ColorIndex = 15
ElseIf Trim(Range("G" & i).Value) = "" Then
Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
End If
End If
Next i
End Sub

VBA excel how to select specify cells to change the background color

i have a code written in VBA that will check the date and base on the it will fill the background with the appropriate color.
i have cells (A to G ).
i want to check if G is empty if so i want to change the background color of the cells (A)(B)(D)(E)(F)(G) except the CELL (C.
CODE:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(Range("G" & i).Value) <> "" Then
Range("A" & i & ":G" & i).Interior.ColorIndex = 15
ElseIf Trim(Range("G" & i).Value) = "" Then
Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
End If
Next
End Sub
I suppose that you should write:
Trim(Range("G" & i).Value) = ""
In stead of Trim(Range("G" & i).Value) == ""
The == operator does not exist in VBA, there is only =.
The rest should work ok. To avoid syntax errors like this, start writing Option Explicit on the top of the module. It will tell you immediately after you run the program where the error is.
Following your coding style, this is how you exclude column C:
Range("A" & i & ":B" & i).Interior.ColorIndex = 7
Range("D" & i & ":F" & i).Interior.ColorIndex = 7

Delete lines VBA macro takes a significant amount of time

I have a macro that deletes lines based on a certain value in a column and then sorts them. It works fine. However, the worksheet starts with about 4000 rows and the macro ends up deleting about 2000 of them and it takes 1 minute 25 seconds to do it. I'm wondering if there's something I can do that will make it take a lot less time. Here's the code:
'remove numbers that are not allowed based on values in "LimitedElements" worksheet
For i = imax To 1 Step -1
a = Sheets("FatigueResults").Cells(i, 1).Value
Set b = Sheets("LimitedElements").Range("A:A")
Set c = b.Find(What:=a, LookIn:=xlValues)
If Not c Is Nothing Then
Sheets("FatigueResults").Rows(i).EntireRow.Delete
End If
Next i
'delete unecessary or redundant rows and columns
Rows(3).EntireRow.Delete
Rows(1).EntireRow.Delete
Columns(23).EntireColumn.Delete
Columns(22).EntireColumn.Delete
Columns(21).EntireColumn.Delete
Columns(20).EntireColumn.Delete
Columns(14).EntireColumn.Delete
Columns(13).EntireColumn.Delete
Columns(12).EntireColumn.Delete
Columns(11).EntireColumn.Delete
Columns(4).EntireColumn.Delete
Columns(3).EntireColumn.Delete
Columns(2).EntireColumn.Delete
'sort data
Dim strDataRange As Range
Dim keyRange As Range
Set strDataRange = Range("A:Q")
Set keyRange1 = Range("B1")
Set keyRange2 = Range("G1")
strDataRange.sort Key1:=keyRange1, Order1:=xlDescending, Key2:=keyRange2, Order2:=xlDescending, Header:=xlYes
'delete rows that are not in the included values For i = imax To 2 Step -1
If (Cells(i, 2).Value <> 0.04 And Cells(i, 2).Value <> 0.045 And Cells(i, 2).Value <> 0.05 And Cells(i, 2).Value <> 0.056 And Cells(i, 2).Value <> 0.063 And Cells(i, 2).Value <> 0.071 And Cells(i, 2).Value <> 0.08 And Cells(i, 2).Value <> 0.09 Or Cells(i, 3).Value <= 0) Then
ActiveSheet.Rows(i).EntireRow.Delete
End If
Next i
Add this at the beginning:
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Add this at the end:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Also, instead of
If (Cells(i, 2).Value <> 0.04 And Cells(i, 2).Value <> 0.045 And Cells(i, 2).Value <> 0.05 And Cells(i, 2).Value <> 0.056 And Cells(i, 2).Value <> 0.063 And Cells(i, 2).Value <> 0.071 And Cells(i, 2).Value <> 0.08 And Cells(i, 2).Value <> 0.09 Or Cells(i, 3).Value <= 0) Then
ActiveSheet.Rows(i).EntireRow.Delete
End If
Use
Select Case Cells(i, 2)
Case 0.4, 0.045, 0.05, 0.056, 0.063, 0.071, 0.08, 0.09, Is < 0
'Do nothing
Case Else
ActiveSheet.Rows(i).EntireRow.Delete
End Select
I much prefer to build a string of rows to be deleted then do ONE delete. Here is a sample I put together for another post on here yesterday:
Sub DeleteRows()
Dim i As Long, DelRange As String
For i = 1 To Cells(Rows.Count, 6).End(xlUp).Row 'Doesn't matter which way you go when you delete in one go
If Left(Cells(i, 6), 3) = "314" Then DelRange = DelRange & "," & i & ":" & i 'Change the "314" as you see fit
Next i
Range(Right(DelRange, Len(DelRange) - 1)).Delete
End Sub
Also no need to worry about turning calculation or screen updating etc off when you only perform one deletion

Resources