Help with basic IF statement

alexbat

New Member
Joined
Dec 12, 2013
Messages
32
Hi,

trying to write a script to be performed in Sheet 1 if calculated value in Sheet 3 is changed.
I put this piece of code in sheet 3 just a test to see if I understand how to make an if statement:

Code:
Private Sub Worksheet_Calculate()
   
    Sheet1.Select
    
    If Range("O1").Value = "11" Then
       
    Range("O2").Select
    ActiveCell.FormulaR1C1 = "Code works"
        
    End If
  
    
End Sub

Unfortunately, it does not work. Any suggestions on what I´m doing wrong. Tried both with and without the "" for the value 11.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
alexbat,

Not sure if you are really wanting to select sheet1 or if you have used select because you think that it is necessary in order to get "code works" into sheet1?

Try this.....

Code:
Private Sub Worksheet_Calculate()    
If Sheets("Sheet1").Range("O1") = 11 Then Sheets("Sheet1").Range("O2") = "Code Works"
Sheets("Sheet1").Select
End Sub

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,837
Members
449,471
Latest member
lachbee

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