+ Reply to Thread
Results 1 to 3 of 3

an interesting Excel question

  1. #1
    Registered User
    Join Date
    04-03-2006
    Posts
    3

    an interesting Excel question

    I have a question about the using of Excel and VBA.

    Let’s say in cell A1 and A2, I input 1 and 2. Then I input formula “=SUM(A1+A2)” into cell A3.

    I would like to write a VBA code to have the following functionality:

    Once I change the formula in A3, let’s say I input 4 to cell A3, the color of the cell A3 should be changed.
    Once I double click the changed cell A3, it should recover to the formula “=SUM(A1+A2)”.

    Anybody could help me out to solve the problem?

    Thank you

  2. #2
    Forum Contributor
    Join Date
    08-08-2005
    Location
    Kansas, USA
    MS-Off Ver
    2016
    Posts
    293
    Take a look at

    http://www.cpearson.com/excel/events.htm

    For a discussion on worksheet events.

    ---GJC

  3. #3
    Bob Phillips
    Guest

    Re: an interesting Excel question

    This might work for you

    Dim prevVal
    Const WS_RANGE As String = "A3"

    '-----------------------------------------------------------------
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
    Boolean)
    '-----------------------------------------------------------------
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    If .Interior.ColorIndex = 3 Then
    .Formula = prevVal
    .Interior.ColorIndex = xlColorIndexNone
    End If
    End With
    End If

    End Sub

    '-----------------------------------------------------------------
    Private Sub Worksheet_Change(ByVal Target As Range)
    '-----------------------------------------------------------------

    On Error GoTo ws_exit:
    Application.EnableEvents = False
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    .Interior.ColorIndex = 3 'red
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub

    '-----------------------------------------------------------------
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    '-----------------------------------------------------------------
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    If .HasFormula Then
    prevVal = .Formula
    End If
    End With
    End If
    End Sub

    'This is worksheet event code, which means that it needs to be
    'placed in the appropriate worksheet code module, not a standard
    'code module. To do this, right-click on the sheet tab, select
    'the View Code option from the menu, and paste the code in.





    --
    HTH

    Bob Phillips

    (remove nothere from email address if mailing direct)

    "jaccker" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I have a question about the using of Excel and VBA.
    >
    > Let's say in cell A1 and A2, I input 1 and 2. Then I input formula
    > "=SUM(A1+A2)" into cell A3.
    >
    > I would like to write a VBA code to have the following functionality:
    >
    > Once I change the formula in A3, let's say I input 4 to cell A3, the
    > color of the cell A3 should be changed.
    > Once I double click the changed cell A3, it should recover to the
    > formula "=SUM(A1+A2)".
    >
    > Anybody could help me out to solve the problem?
    >
    > Thank you
    >
    >
    > --
    > jaccker
    > ------------------------------------------------------------------------
    > jaccker's Profile:

    http://www.excelforum.com/member.php...o&userid=33116
    > View this thread: http://www.excelforum.com/showthread...hreadid=529223
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1