Vba to return zero based on another cell text

jaxs2009

Board Regular
Joined
Nov 28, 2010
Messages
200
:cool: Excel 2007 VBA

Column AX range AX6:AX1000 when the text "CANCELLED" is present in any one row then enter the number "zero"in that row in column AZ

(The cells in column AZ are "accounting" formatted)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Why VBA?
Why not just put formula:
Code:
=IF(AX6="CANCELLED",0,"")
in cell AZ6, and then copy that formula all the way down to row 1000?
If you want values and not the formula, you can do a Copy -> Paste Special -> Values afterwards.

Recording doing the steps above manually using the Macro Recorder would also give you the VBA code to do that.
 
Upvote 0
Why VBA?
Why not just put formula:
Code:
=IF(AX6="CANCELLED",0,"")
in cell AZ6, and then copy that formula all the way down to row 1000?
If you want values and not the formula, you can do a Copy -> Paste Special -> Values afterwards.

Recording doing the steps above manually using the Macro Recorder would also give you the VBA code to do that.

I am sorry I did not mention that in column AZ rows 6 through 1000 there is a number value in it, by entering the text "CANCELLED" in column AX rows 6 trough 1000 I want the current $ value to return to zero.

The =IF formula as is will work.

Column AX rows 6 through 1000 has a data validation list with the word "CANCELLED" in the list. That is why I thought of A VBA
 
Upvote 0
I am sorry I did not mention that in column AZ rows 6 through 1000 there is a number value in it
The devil is often in the details not mentioned! ;)

Try this:
Code:
Sub MyMacro()

    Dim rng As Range
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
'   Designate range to check
    Set rng = Range("AX6:AX1000")
    
'   Loop through range
    For Each cell In rng
'       Check to see if cell equals "CANCELLED"
        If cell.Value = "CANCELLED" Then
'           Zero out column AZ
            cell.Offset(0, 2).Value = 0
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
This works if you go to developer tab click on Macros and then manually run the macro, I want the marco or VBA to run automatically when the "CANCELLED" is selected in any cell AX6:AX1000 to zero out offset, 2 wwhich is column AZ same row selected, Thank you for your help.
 
Upvote 0
Put this code in the appropriate Sheet module:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Count > 1 Then Exit Sub
    
    If Not Intersect(Target, Range("AX6:AX1000")) Is Nothing Then
        If Target = "CANCELLED" Then
            Application.EnableEvents = False
            Target.Offset(0, 2) = 0
            Application.EnableEvents = True
        End If
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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