1

I want to set a value for a cell [S12] in a sheet called "AlphaPackage_2017" based on the other cell's value [S3] .

checking should follow the value in cell [S3] for any changing value and update immediately the value in cell [S12].

I wrote the below code:

Do
    If S3.Value > 6 Then
        S12 = 20
    Else
        S12 = 7
    End If
Loop

the code does not work correctly, any advise is appreciated ... and also where the code should be write: in Module or in Page?

1
  • Note: S3 cell will be change by Form Control or Active X Control, not manually. Commented Apr 21, 2017 at 4:27

2 Answers 2

2

In the code module of the AlphaPackage_2017 worksheet, write this routine:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "S3" Then
        Me.Range("S12").Value = IIf(Target.Value > 6, 20, 7)
    End If
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

@ShaiRado although you typed much more lines. +1 for yours uses Intersect which is more accurate a solution :)
@A.S.H Thank you for your advise, but after changing the S3 value by manually (with keyboard) , Excel is stopped and VB shows the second line: If Target.Adress(0, 0) = "S3" Then in yellow color. And also I want to use a form control or active X control for changing S3 value. what is solution? Appreciate, Kasra
@Kasra I'm so sorry, there was a typo in there (Adress should be Address). Seize the occasion and start using Option Explicit, the compiler will then spot for you such mistakes immediately :)
@A.S.H Thank You My Friend ... OK, I want to use a Form Control or Active X control for changing S3 value. what is the solution with before conditions? Appreciate, Kasra
@Kasra that's a different and broad subject. I suggest you try setting the Cell Link property of the control and observe what happens. When facing a specific problem it should be worth a dedicated question :)
2

Add the code below in the Worksheet_Change event of "AlphaPackage_2017" sheet.

Private Sub Worksheet_Change(ByVal Target As Range)

' run the code below only if a value in cell "S3" is changed
If Not Intersect(Target, Range("S3")) Is Nothing Then

    Select Case Target.Value ' <-- check the value of Range("S3")
        Case Is > 6
            Range("S12").Value = 20
        Case Else
            Range("S12").Value = 7
    End Select
End If

End Sub

Follow screen-shot below where to add this code (just in case you haven't used a Worksheet event before):

enter image description here

4 Comments

Dear Shai, Thank you for your advise. but the code does not work for me. After changing the S3 value, the S12 has not any change ...
@Kasra where did you put the code ? what value did you enter in "S3" ?
Hi, I putted the code under "Microsoft Excel Object" in »AlphaPackage_2017« ... What is surprising is that after changing S3 (S3 is Numeric type) then S12 has not been change. but when any Macro runs then S12 has been change! what happen my friend?
Hi my friend. thank you for your advise. I'm Sorry, your code working but just if I change S3 value manually by keyboard! The code not works by form control or active X control. what is the solution? Appreciate, Kasra

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.