Changing values in a column to zero, then back again

graemeal

Active Member
Joined
May 17, 2007
Messages
316
Platform
  1. Windows
I have 2 columns shown below, column C and column D. Both columns have formulas in them already. I now sort column D from largest to smallest by a macro.

I would like create a condition in another macro that changes the value in column D to 0 if the value opposite in C column is less than 0, then sort from largest to smallest.

C D
%%% Gain
2 -1.34 2.30
3 3.61 2.15
4 -3.72 1.67
5 -4.05 1.50
6 0.76 1.20
7 5.60 1.10

So the macro would change D2, D4, and D5 to zero then Sort D from largest to smallest would produce the bottom example.

C D
%%% Gain
2 3.61 2.15
3 0.76 1.20
4 5.60 1.10

Ideally I would like to reverse that via another Macro.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Rather than changing formulae to zero values, and having to reinstate the formulae, why not simply filter?

VBA Code:
With ActiveSheet
    If .AutoFilterMode Then
        .AutoFilterMode = False
    Else
        .Rows("1:1").AutoFilter Field:=3, Criteria1:=">=0"
    End If
End With
 
Upvote 0
Solution
Rather than changing formulae to zero values, and having to reinstate the formulae, why not simply filter?

VBA Code:
With ActiveSheet
    If .AutoFilterMode Then
        .AutoFilterMode = False
    Else
        .Rows("1:1").AutoFilter Field:=3, Criteria1:=">=0"
    End If
End With

Where do I insert that code?

I am not very experience in Excel.
 
Upvote 0
You can put the code in its own Sub, and call the Sub any way you like (click a button, click a shape, keyboard shortcut - what are you doing with your current macros?)

VBA Code:
Sub ShowHide()

    With ActiveSheet
        If .AutoFilterMode Then
            .AutoFilterMode = False
        Else
            .Rows("1:1").AutoFilter Field:=3, Criteria1:=">=0"
        End If
    End With
       
End Sub

My code doesn't sort column D - you said you do this via another macro, and filtering out the negative column C values won't change the sort order.
 
Upvote 0

Forum statistics

Threads
1,215,428
Messages
6,124,832
Members
449,190
Latest member
rscraig11

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