• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

If ... Then

Kinghart

Member
Hi,
This Is very Simple But Not For Me..
I Want A VBA Code For:
If Cell A3="T" Then Cell C3 To Write A Formula (eg: sum(a+b))
 
You'll probably have to use the macro recorder to figure out the exact syntax for the formula you want to enter, but this code should work for the purposes of evaluating cell A3.

Code:
If Range("A3") = "T" Then Range("C3").Formula = "=Insert Your Formula Here"
 
Hi Kinghart ,

I am not sure what your exact requirement is ; if it is that C3 should have in it the sum of A3 and B3 , then a simple way to write it is :

If (UCase([A3]) = "T") Then [C3] = Val([A3]) + [B3]

This illustrates a few problems in the definition of your requirements !

If A3 is a text item ( the letter T ) , how can you add A3 and B3 ? You can provided you convert the text in A3 to a numeric value ; however , the numeric value of a text item is always 0.

The UCase function is to convert the text item in A3 to upper case , so that we can compare it to the upper case letter T , since the lower case "t" when compared with the upper case "T" will return FALSE.

Narayan
 
Hmmm. I haven't seen that one before, but a quick Google search leads me to suspect that maybe you're trying to use that code in a Function rather than a Sub procedure? If that's the case, the error doesn't surprise me because (at least as far as I understand it) functions can only produce a result, they can't actually make changes to workbooks or worksheets.
 
Thanks Guys And Sorry For The Troubles... What I Actaullay Wants Is When I Type The Letter "T" In (A3), Cell (C3) Should Show The Formula "=Sum(D3+E3)" Using VBA And Do The Calculation. That's My Primary Goal. My secondary Goal Is When (A3) Is Other Than "T" or blank, (C3) Should become blank.
 
This code should work if you place it in the worksheet for which you want the custom behavior:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
If Not Target.Address = "$A$3" Then Exit Sub
 
If Target = "T" Then
    Range("C3").Formula = "=SUM($D$3+$E$3)"
ElseIf Target = "" Then
    Range("C3") = ""
End If
 
End Sub
 
I think this should do the trick. Please let me know if you find otherwise:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim rngWatch As Range
Set rngWatch = [A3:A15]
 
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, rngWatch) Is Nothing Then Exit Sub
 
If Target = "T" Then
    Range("C" & Target.Row).FormulaR1C1 = "=SUM(RC4+RC5)"
Else
    Range("C" & Target.Row) = ""
End If
 
End Sub
 
Back
Top