Consulting

Results 1 to 15 of 15

Thread: auto update with cell value change

  1. #1

    auto update with cell value change

    hello,

    How to write a macro to update the values without running the macro. i mean, if i change a cell, it will effects after i run the macro, but i will to change the values without running the macro.

    Any help?

  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi,

    I think yoe want a Worksheet Change macro. Post an example of what you have/want and I'll show you how
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Here is an example

    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "H1:H10" '<== change to suit

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    ' do your stuff
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]

    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Thank you very much XLD, I will check it out.

  5. #5
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    Learned something new today!

    Quick Question xld.....if there are, say 5, non-continuous cells that you want to run the macro when they are updated, how would that be coded ?

    Or would you need to copy the code above 5 times, one for each cell ?

    thanks!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sure, something like

    [vba]

    Const WS_RANGE As String = "H1,M3,T9,L5,A9" '<== change to suit
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    great, thanks !

  8. #8
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    OK, 'nuther question....if I want to run one macro if cell B5 is updated and ANOTHER macro if cell B19 is updated, what would that look like ?

    i tried copying the above code and using a different name and assigning it as a Change but did not run. ??

  9. #9
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    anyone ?

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range("B5")) Is Nothing Then
    With Target
    Call Macro1
    End With
    ElseIf Not Intersect(Target, Me.Range("B19")) Is Nothing Then
    With Target
    Call Macro2
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Contributor
    Joined
    Jul 2009
    Posts
    157
    Location
    Thanks xld, you are always a lot of help !! :-)

  12. #12
    What to modify in above code if the cell which has to trigger the macro is linked to another cell.

    Details of the Problem.
    Cell A2 is linked to C2 i.e. in Cell A2 the formula is "=C2"
    Whenever I change C2, A2 is updated and that should trigger the macro.

    I cannot make it simple by placing the trigger on C2 itself.

    I am using above examples but isnt working in my particular case. Help appreciated.

  13. #13
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi Ted

    Sorry but this doesn't quite logic out. If you change C2, A2 changes.


    "I cannot make it simple by placing the trigger on C2 itself." Huh?

    How does C2 get changed?
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  14. #14
    When I used the Event Sub "Worksheet_Change", if A2 was changed through C2, the macro did not trigger. The above examples only work if A2 is changed itself but not through any other cell.

    I used the following code to get my work done, offcourse with the help of experts here.

    Private Sub Worksheet_Calculate()
    If Range("A2").Value <> 0 Then
    ' do your stuff
    End If
    End Sub

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try something along the lines o

    [vba]
    If Not Intersect(Target, Me.Range("A:C")) Is Nothing Then

    For Each cell In Target

    'do stuff with cell not Target
    Next cell
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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