This macro is driving me crazy - Please help

tmpollard

Board Regular
Joined
Jun 12, 2008
Messages
111
I've got a macro that delete values in a dataset that are higher than a value in a specific cell. This works fine

I then copied the macro, pasted it into a new macro template to reverse the sign so that it delete all values lower than a value in a specific cell and it just will not work. This is driving me crazy, can someone please help. Thanks

Sub MaxValue()
'
' MaxValue Macro
' This macro deletes all values greater than the value in Cell M1.
'

'
Dim cell As Range
For Each cell In Range("D11:I1000")
If cell.Value > ActiveSheet.Range("M1") Then
cell.ClearContents
End If
Next cell
End Sub




Sub MinValue()
'
' MinValue Macro
'This macro deletes all values lower than the value in Cell M2.
'

'
Dim cell As Range
For Each cell In Range("D11:I1000")
If cell.Value < ActiveSheet.Range("M2") Then
cell.ClearContents
End If
Next cell
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi, I don't see a particular reason why the code wouldn't work except for "ActiveSheet"

Change ActiveSheet to something like Worksheets("Sheet1") where Sheet1 would be the name of the worksheet you want to look into.
And for more explicit expression of comparing to a value, use Range.Value instead of just Range
Like this
Code:
If cell.Value < Worksheets("Sheet1").Range("M2").Value Then
 
Upvote 0
Hi, I don't see a particular reason why the code wouldn't work except for "ActiveSheet"

Change ActiveSheet to something like Worksheets("Sheet1") where Sheet1 would be the name of the worksheet you want to look into.
And for more explicit expression of comparing to a value, use Range.Value instead of just Range
Like this
Code:
If cell.Value < Worksheets("Sheet1").Range("M2").Value Then



Thank you. I saved the macros in my Personal.xls so that they can be used on various workbooks. The tab names at the bottom vary depending on which spreadsheet I open so, I can't really put the worksheet part in there.
 
Upvote 0
Went ahead and edited the macro to do as kpark91 suggested in his initial comments with putting the sheet tab name in the macro, it still didn't work.

Also, verified that the cell M2 is formatted for a number, still no luck.

This is puzzling, last thing I tried was using the exact same cell (M1) which is the one that works, then changed the sign in the macro, still didn't work.
 
Upvote 0
How about your data in D11:I1000? If that data is being read as text rather than a number it would be deleted by the greater than comparison but ignored by the less than.
 
Upvote 0

Forum statistics

Threads
1,202,959
Messages
6,052,797
Members
444,602
Latest member
Cookaa

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