Consulting

Results 1 to 4 of 4

Thread: VBA Code will not update as target cell is updated using a formula

  1. #1

    VBA Code will not update as target cell is updated using a formula

    Hi All,

    I am new to VBA and know virtually nothing. I have the following code that hides rows based on the cell value in A99. It works fine, however, when the value in A99 changes, the vba code does not run unless i click on cell A99.

    Is there a way to adapt the code to update anytime the formula in cell A99 changes the value.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$99" Then
    Rows("102:119").Hidden = False
    If Target.Value > 0 And Target.Value < 19 Then
    Rows(Target.Value + 101 & ":119").Hidden = True
    End If
    End If
    End Sub
    Thanks in advance.

    NS
    Last edited by Paul_Hossler; 06-05-2020 at 05:45 PM. Reason: Added CODE tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Seems to work for me

    What specifically causes it to fail?

    Is there an Application.EnableEvents = False somewhere in the macros?

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$99" Then
            Rows("102:119").Hidden = False
            
            If Target.Value > 0 And Target.Value < 19 Then
                Rows(Target.Value + 101 & ":119").Hidden = True
            End If
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    Seems to work for me

    What specifically causes it to fail?

    Is there an Application.EnableEvents = False somewhere in the macros?

    Option Explicit
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$99" Then
            Rows("102:119").Hidden = False
            
            If Target.Value > 0 And Target.Value < 19 Then
                Rows(Target.Value + 101 & ":119").Hidden = True
            End If
        End If
    End Sub

    Sorry, I forgot to mention... the value in cell A99 is a formula. When the formula updates the cell value, the vba does not run.

    Do you know how to adjust the formula to update anytime A99 changes

    Thanks

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Try Worksheet_Calculate instead
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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