+ Reply to Thread
Results 1 to 6 of 6

Thread: Cell Reference from Programming

  1. #1
    Registered User
    Join Date
    05-01-2010
    Location
    Vijayawada, India
    MS-Off Ver
    Excel 2007
    Posts
    19

    Cell Reference from Programming

    Hi,
    Hope you can help in resolving the below issue,
    I have a code that works for Retaining the Upper (in B1) and Lower values (in C1) as they reach changed to next number in Cell A1 ------ while a mean Value is maintained in D1
    --
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim nMin As Long, nMax As Long
    
    'Intersect(Target.Cells, Range("1:50"), Union(Columns("A"), Columns("E")))
    If Intersect(Target.Cells, Range("1:50"), Union(Columns("A"), Columns("D"))) Is Nothing Then Exit Sub
    'Replace line above with line below to work on any column
    'If Target.Row <> 1 And Target.Row <> 4 Then Exit Sub
    
    With WorksheetFunction
        nMax = Cells(Target.Row, "B").Value
        nMin = Cells(Target.Row, "C").Value
        If Target.Column = 4 Then
            nMax = .Max(Target.Value, Cells(Target.Row, "A").Value)
            nMin = .Min(Target.Value, Cells(Target.Row, "A").Value)
        End If
        Cells(Target.Row, "B").Value = .Max(Target, nMax)
        Cells(Target.Row, "C").Value = .Min(Target, nMin)
    End With
    
    End Sub
    OK After the above was developed with help of StephenR form this forum.
    ...
    I need a new help as below.
    ..
    I want to insert a new value in A1 as refrence from G1.
    ..
    When I do this refrence in the Cell A1 directly the above code stopped working further .
    ..
    Hope I was able to expalin the isse and reqeust you to help me in resolving the issue.
    Moeny Baba,
    You can not get everything with Moeny.
    ~~ ~~ ~~ ~~ ~~

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Cell Refrence from Programming

    Do you mean you have a formula in A1 "=G1"? I don't think that triggers the worksheet change event so you will need a different approach.

  3. #3
    Registered User
    Join Date
    05-01-2010
    Location
    Vijayawada, India
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Cell Refrence from Programming

    Hi Stepheen,
    THanks for your update, yes you are correct I want same as you said.
    Please help how I can proceed ..
    ...
    And also I want to have refrence to D1 also in same way meaning d1=h1
    ..
    Hope you can help me on this.
    Moeny Baba,
    You can not get everything with Moeny.
    ~~ ~~ ~~ ~~ ~~

  4. #4
    Registered User
    Join Date
    05-01-2010
    Location
    Vijayawada, India
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Cell Refrence from Programming

    Hi,
    Hope I Could get help today?
    Thanks.
    Baba
    Moeny Baba,
    You can not get everything with Moeny.
    ~~ ~~ ~~ ~~ ~~

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Cell Refrence from Programming

    I think you need a different approach. To be honest I'm not sure what is the best way. Can you attach a workbook which shows the new situation?

  6. #6
    Forum Guru
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,110

    Re: Cell Refrence from Programming

    excelbaba;

    Worksheet_Change() is only triggered when you manually change a cell or if a macro changes a cell. If a formula changes the value Worksheet_Change() is not triggered.
    So you can do something like this:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim nMin As Long, nMax As Long
    
        'Intersect(Target.Cells, Range("1:50"), Union(Columns("A"), Columns("E")))
        If Intersect(Target.Cells, Range("1:50"), Union(Columns("A"), Columns("D"))) Is Nothing Then Exit Sub
        'Replace line above with line below to work on any column
        'If Target.Row <> 1 And Target.Row <> 4 Then Exit Sub
    
        With WorksheetFunction
            nMax = Cells(Target.Row, "B").Value
            nMin = Cells(Target.Row, "C").Value
            If Target.Column = 4 Then
                nMax = .Max(Target.Value, Cells(Target.Row, "A").Value)
                nMin = .Min(Target.Value, Cells(Target.Row, "A").Value)
            End If
    
            'Note these will trigger Worksheet_Change() again
            '    but since they are not in A or D the Intersects above will filter them out.
            Cells(Target.Row, "B").Value = .Max(Target, nMax)
            Cells(Target.Row, "C").Value = .Min(Target, nMin)
    
            'Add These
            'These will also trigger Worksheet_Change()
            '    so put a test to eliminate an endless loop.
            If Cells(Target.Row, "A").Value <> Cells(Target.Row, "G").Value Then
                Cells(Target.Row, "A").Value = Cells(Target.Row, "G").Value
                Cells(Target.Row, "D").Value = Cells(Target.Row, "H").Value
            End If
        End With
        
    End Sub
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

+ 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.2.0