Define and call a constant

powerdink

New Member
Joined
Feb 16, 2011
Messages
21
I'm trying to declare a constant that I can call in an if statement. The excerpt of code below is supposed to search all cells in column E and find where the value is equal to or less than 14. If it is, then the script will clear the contents of the entire row.

Code:
Dim u1 As Range
Const conAGEy As Integer = 14
 
Set u1 = ThisWorkbook.Worksheets("Sheet1").Range("B2").CurrentRegion
    With u1.Offset(1, 0).Resize(u1.Rows.Count, u1.Columns.Count)
        Last = Cells(Rows.Count, "A").End(xlUp).Row
        For i = Last To 2 Step -1
            If (Cells(i, "E").Value) <= conAGEy Then         'Constants are not working here
                Cells(i, "A").EntireRow.ClearContents
            End If
        Next i
    End With

Any help would be appreciated. Thanks all.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
That code looks fine to me. What is your problem with it?
 
Upvote 0
It doesn't actually go through and clear the rows where Column "E"'s value is <= the value of the constant. I don't get an error message either. It just runs like it completed, except the data is still there.
 
Upvote 0
Are you sure the values in your sheet are numbers and not text? Does it work if you simply use 14 directly in place of the constant?
 
Upvote 0
Do you have any gaps in your data, say row 3 is empty or anything below? Otherwise I can't see what's wrong either.. maybe change the clearcontents line to delete?
Code:
Cells(i, "A").EntireRow.Delete
Since you're looping backwards anyway...
 
Upvote 0
Thanks guys, I tried inserting 14 instead of the constant and it still doesn't update. So I added-
Code:
.numberformat = "0"
-earlier to format the column to numbers just in case it was text and that didn't work either.

Lastly, I tried adding delete instead of clear rows but none of it seems to work. Any ideas?
 
Upvote 0
Changing a number format will not in any way alter the contents of cells. Are your numbers left-aligned by default? If so they are text. Copying a blank cell then select the numbers and paste special - add
 
Upvote 0
Got this to work by making some modifications to the code. Turns out that without using the .cells to show the object in the with statement, the loop will execute through the entire sheet. I also took out the column names or "E" and replaced it with the column number.

Code:
    Set u4 = ThisWorkbook.Worksheets("Sheet1").Range("G2").CurrentRegion
    With u4.Offset(1, 0).Resize(u4.Rows.Count - 1, u4.Columns.Count)
        Last = .Cells(.Rows.Count, 5).Row
        For i = Last To 2 Step -1
            If (.Cells(i, 5).Value) <= conAGE1 Then
                .Rows(i).EntireRow.ClearContents
            End If
        Next i
    End With
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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