I’m trying to loop through a specified number of cells (defined by width and height), but I’m running into problems here. It keeps stalling on me, and then gets upset about:
If .Cells(11 + row, col).Value > maxVal Then
It’s giving me an “Application defined or object defined error”
Can anyone tell me where I’m going wrong with my code:
Sub ApplyFilter()
Dim maxVal As Double
Dim minVal As Double
maxVal = ActiveSheet.Range("D10").Value
minVal = ActiveSheet.Range("D11").Value
Dim width As Integer
Dim height As Integer
width = ActiveSheet.Range("L3").Value
height = ActiveSheet.Range("L4").Value
Dim row As Integer
Dim col As Integer
ActiveSheet.Select
With Selection
row = 1
Do
col = 1
Do
If .Cells(11 + row, col).Value > maxVal Then
.Cells(11 + row, col).Value = 0
End If
If .Cells(11 + row, col).Value < minVal Then
.Cells(11 + row, col).Value = 0
End If
col = col + 1
width = width - 1
Loop Until width = 1
row = row + 1
height = height - 1
Loop Until height = 1
End With
End Sub
Sometimes Excel would give “Application defined or object defined error” for no reason, because it’s just the way Excel is, but that’s probably not the case here.
Don’t use
SelectorSelectionto interact with objects. Name the objects directly.