IF Formula

Mahsun23

New Member
Joined
Oct 19, 2006
Messages
17
Hi there,

Is it possible to make an IF formula where the value of the cell is linked to another cell if TRUE, but the value doesn't change anymore if FALSE. In other words the cell should keep the latest value if FALSE.

Regards

Mahsun 8-)
 
Mahsun23

I am not certain this will do exactly what you want but give it a try. First I suggest you make a backup of your file.

1. Right click the sheet tab and choose 'View Code'. This will display the VBA window.
2. Copy the code below and paste it in the large white pane at the right of the VBA window.
3. Close the VBA window, return to your sheet and try changing some values.

If it does not do what you expect, report back with as much detail as possible.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    Columns("IV").EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Range("C8").Value <> Range("U8").Value <SPAN style="color:#00007F">Then</SPAN>
        Range("W8").Value = Range("IV8").Value
    <SPAN style="color:#00007F">Else</SPAN>
        Range("W8").Formula = "=C8/D8 - 1"
        Range("IV8").Value = Range("W8").Value
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Thank you Pete,

I guess there is a genius somewhere downunder.

Is it also possible to disable/enable the Event only for cell W8?

Regards

Mahsun
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Is it also possible to disable/enable the Event only for cell W8?
What do you mean? If the suggested code did not do what you wanted, can you explain in what way or under what circumstances it failed? For example, what did it do and what should it have done?
 
Upvote 0
Hi Pete,
Your code works. but there is an other issue.
What I try to say is that when I use the same technique in another cell it doesn't work. I modified your code a little bit as you can see here under for cell Y8.


Private Sub Worksheet_Change2(ByVal Target As Range)
Columns("IU").EntireColumn.Hidden = True
Application.EnableEvents = False
If Range("U8").Value <> Range("V8").Value Then
Range("Y8").Value = Range("IU8").Value
Else
Range("Y8").Formula = "=IF(R8=,"".(U8/D8-1))"
Range("IU8").Value = Range("Y8").Value
End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Mahsun23

Can you clarify what formula you want in Y8? There are two problems with the formula as posted:
1. IF(R8=, What should be after the = and before the , ?
2. Confirm that the . in the formula should be a , ?
Range("Y8").Formula = "=IF(R8=,"".(U8/D8-1))"
 
Upvote 0
Dear Pete,

Sorry, I am a little bit negligent.

The formula should be =IF(R8=0,"",(U8/D8-1))

It still doesn't work.

Cheers

Mahsun23
 
Upvote 0
Mahsun23

You need to have both lots of code within the (only) Worksheet_Change event. See if this is what you want. Note a slight change in the order in the code and also note the """" in the Y8 formula.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
    
    Columns("IV").EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Range("C8").Value <> Range("U8").Value <SPAN style="color:#00007F">Then</SPAN>
        Range("W8").Value = Range("IV8").Value
    <SPAN style="color:#00007F">Else</SPAN>
        Range("W8").Formula = "=C8/D8 - 1"
        Range("IV8").Value = Range("W8").Value
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    Columns("IU").EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Range("U8").Value <> Range("V8").Value <SPAN style="color:#00007F">Then</SPAN>
    Range("Y8").Value = Range("IU8").Value
    <SPAN style="color:#00007F">Else</SPAN>
    Range("Y8").Formula = "=IF(R8=0,"""",(U8/D8-1))"
    Range("IU8").Value = Range("Y8").Value
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Dear Pete, or shall I call you Mr Excel?

Thank you again,

Finally I made the following code:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False

Columns("IV").EntireColumn.Hidden = True
If Range("C8").Value <> Range("U8").Value Then
Range("X8").Value = Range("IV8").Value
Else
Range("X8").Formula = "=C8/D8 - 1"
Range("IV8").Value = Range("X8").Value
End If

Columns("IU").EntireColumn.Hidden = True
If Range("U8").Value <> Range("V8").Value Then
Range("Y8").Value = Range("IU8").Value
Else
Range("Y8").Formula = "=IF(R8=0,"""",(U8/D8-1))"
Range("IU8").Value = Range("Y8").Value
End If

Columns("IT").EntireColumn.Hidden = True
If Range("V8").Value <> Range("W8").Value Then
Range("Z8").Value = Range("IT8").Value
Else
Range("Z8").Formula = "=IF(S8=0,"""",(V8/D8-1))"
Range("IT8").Value = Range("Z8").Value
End If

Application.EnableEvents = True
End Sub

It works!

I got one more question.

I have to use this code for more 5 more rows, e.q. in stead of row 8, I have to use this code also for 9, 10, 11 etc.

And also for two more sheets (in the same excel file)

Do I have to put them all in the same worksheet_Change event? I can see in VBA that every sheet has an own code pane.

Regards

Mahsun
 
Upvote 0
..or shall I call you Mr Excel?
Definitely not!
I have to use this code for more 5 more rows, e.q. in stead of row 8, I have to use this code also for 9, 10, 11 etc.
Try this structure:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    <SPAN style="color:#00007F">Dim</SPAN> StartRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> EndRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    
    StartRow = 8
    EndRow = 13
    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
    Columns("IT:IV").EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">For</SPAN> r = StartRow <SPAN style="color:#00007F">To</SPAN> EndRow
        <SPAN style="color:#00007F">If</SPAN> Cells(r, 3).Value <> Cells(r, 21).Value <SPAN style="color:#00007F">Then</SPAN>
            Cells(r, 23).Value = Cells(r, 256).Value
        <SPAN style="color:#00007F">Else</SPAN>
            Cells(r, 23).Formula = "=C" & r & "/D" & r & " - 1"
            Cells(r, 256).Value = Cells(r, 23).Value
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(r, 21).Value <> Cells(r, 22).Value <SPAN style="color:#00007F">Then</SPAN>
            Cells(r, 25).Value = Cells(r, 255).Value
        <SPAN style="color:#00007F">Else</SPAN>
            Cells(r, 25).Formula = "=IF(R" & r & "=0,"""",(U" & r & "/D" & r & "-1))"
            Cells(r, 255).Value = Cells(r, 25).Value
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> Cells(r, 22).Value <> Cells(r, 23).Value <SPAN style="color:#00007F">Then</SPAN>
            Cells(r, 26).Value = Cells(r, 254).Value
        <SPAN style="color:#00007F">Else</SPAN>
            Cells(r, 26).Formula = "=IF(S" & r & "=0,"""",(V" & r & "/D" & r & "-1))"
            Cells(r, 254).Value = Cells(r, 26).Value
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> r
    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

And also for two more sheets (in the same excel file)

Do I have to put them all in the same worksheet_Change event? I can see in VBA that every sheet has an own code pane.
Each sheet will need its own Worksheet_Change event code.
 
Upvote 0
Dear Pete,

It's an exciting code that you provided.

I changed the number 23 to 24, (because W needed to be X)
See the changed code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim StartRow As Integer
Dim EndRow As Integer
Dim r As Integer

StartRow = 8
EndRow = 11
Application.EnableEvents = False
Columns("IT:IV").EntireColumn.Hidden = True
For r = StartRow To EndRow
If Cells(r, 3).Value <> Cells(r, 21).Value Then
Cells(r, 24).Value = Cells(r, 256).Value
Else
Cells(r, 24).Formula = "=C" & r & "/D" & r & " - 1"
Cells(r, 256).Value = Cells(r, 24).Value
End If
If Cells(r, 21).Value <> Cells(r, 22).Value Then
Cells(r, 25).Value = Cells(r, 255).Value
Else
Cells(r, 25).Formula = "=IF(R" & r & "=0,"""",(U" & r & "/D" & r & "-1))"
Cells(r, 255).Value = Cells(r, 25).Value
End If
If Cells(r, 22).Value <> Cells(r, 23).Value Then
Cells(r, 26).Value = Cells(r, 254).Value
Else
Cells(r, 26).Formula = "=IF(S" & r & "=0,"""",(V" & r & "/D" & r & "-1))"
Cells(r, 254).Value = Cells(r, 26).Value
End If
Next r
Application.EnableEvents = True
End Sub


I thought I was there, but when I was working on another sheet I had to change the start row to 8 and the end row to 35. And because there are some empty rows I got an error message. This is understandable because the code doesn't recognize empty rows.

Is there a way to by pass these empty rows?

I have a good feeling that this is my last question.

Best Regards

Mahsun
 
Upvote 0
Mahsun

Assuming the rows that are not empty all have something in column C (column 3 in code) then try this.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    <SPAN style="color:#00007F">Dim</SPAN> StartRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> EndRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    
    StartRow = 8
    EndRow = 13
    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
    Columns("IT:IV").EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">For</SPAN> r = StartRow <SPAN style="color:#00007F">To</SPAN> EndRow
        <SPAN style="color:#00007F">If</SPAN> Cells(r, 3).Value <> "" <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'Check to see if there is somrthing in col C</SPAN>
            <SPAN style="color:#00007F">If</SPAN> Cells(r, 3).Value <> Cells(r, 21).Value <SPAN style="color:#00007F">Then</SPAN>
                Cells(r, 23).Value = Cells(r, 256).Value
            <SPAN style="color:#00007F">Else</SPAN>
                Cells(r, 23).Formula = "=C" & r & "/D" & r & " - 1"
                Cells(r, 256).Value = Cells(r, 23).Value
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">If</SPAN> Cells(r, 21).Value <> Cells(r, 22).Value <SPAN style="color:#00007F">Then</SPAN>
                Cells(r, 25).Value = Cells(r, 255).Value
            <SPAN style="color:#00007F">Else</SPAN>
                Cells(r, 25).Formula = "=IF(R" & r & "=0,"""",(U" & r & "/D" & r & "-1))"
                Cells(r, 255).Value = Cells(r, 25).Value
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">If</SPAN> Cells(r, 22).Value <> Cells(r, 23).Value <SPAN style="color:#00007F">Then</SPAN>
                Cells(r, 26).Value = Cells(r, 254).Value
            <SPAN style="color:#00007F">Else</SPAN>
                Cells(r, 26).Formula = "=IF(S" & r & "=0,"""",(V" & r & "/D" & r & "-1))"
                Cells(r, 254).Value = Cells(r, 26).Value
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#007F00">' If nothing in col C the code jumps to here</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> r
    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,100
Members
452,301
Latest member
QualityAssurance

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