+ Reply to Thread
Results 1 to 9 of 9

Excel VBA Hide - Show Rides

  1. #1
    Registered User
    Join Date
    09-04-2021
    Location
    Australia
    MS-Off Ver
    Microsoft 365
    Posts
    16

    Excel VBA Hide - Show Rides

    I have a VBA code that shows rows if a cell is "Yes" and hides rows if "No".

    If I type "Yes" or "No" in a cell, it will show / hide the rows. If I i use an IF formula to generate "Yes" or "No" in the cell, it does not work.

  2. #2
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: Excel VBA Hide - Show Rides

    To capture changes by a formula you have to use the Worksheet_Calculate() event.

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,206

    Re: Excel VBA Hide - Show Rides

    You can also use the Worksheet_Change event, but on the cells that are the source for the Yes/No formula.
    You program the _Change event for the source cells, check what the Yes/No formula returns and take the appropriate action depending on the read formula result.

    Artik

  4. #4
    Registered User
    Join Date
    09-04-2021
    Location
    Australia
    MS-Off Ver
    Microsoft 365
    Posts
    16

    Re: Excel VBA Hide - Show Rides

    Thanks for your help, but I cannot get it to work. I have the Worksheet Change in the code but to no avail. Below is the code that I have.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim answer As Integer




    'to hide Family POS

    Set KeyCells = Range("B52")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then

    ActiveSheet.Unprotect

    If Target.Value = "Yes" Then
    Rows("54:61").Select
    Selection.EntireRow.Hidden = False
    Range("e52").Select
    ActiveCell.Offset(0, 1).Select

    ActiveSheet.Protect

    ElseIf Target.Value <> "Yes" Then

    ActiveSheet.Unprotect

    Rows("54:61").Select
    Selection.EntireRow.Hidden = True
    Range("e51").Select
    ActiveCell.Offset(0, 1).Select

    ActiveSheet.Protect

    End If
    End If


    End Sub

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,206

    Re: Excel VBA Hide - Show Rides

    What's in cell B52? The code shown will work when you manually (or code) change its content. If the cell contains a formula, your code won't work. In this case, show what formula it contains. Ideally, you would include a sample workbook. See the yellow banner at the top of the page.

    By the way, edit your post and put the presented code in the [code] tags (select the entire code and press the [#] button).

    Artik

  6. #6
    Registered User
    Join Date
    09-04-2021
    Location
    Australia
    MS-Off Ver
    Microsoft 365
    Posts
    16

    Re: Excel VBA Hide - Show Rides

    Thanks for replying. Cell b52 contains a formula. It was a if formula with the answer as “Yes”or “no”

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,206

    Re: Excel VBA Hide - Show Rides

    The Worksheet_Change event is dispatched when you manually or with code change the contents of cells in a given sheet, i.e. edit cells. Thanks to the Target parameter, you can specify that we are only interested in a certain range of cells, and if there is a change in the Target range, we will take a specific action. In other ranges the action will not be taken. The Worksheet_Calculation event is dispatched when there are formulas on the sheet. It cannot be directly indicated that we are only interested in calculating cell B52 (no Target parameter). The event affects all formulas on the sheet. So, if you use the _Calculation event, your action will always be executed, even if the formula in B52 does not change at the moment. So redundant work is performed (regardless of whether the result of the formula has changed or not, the action will be performed). If you use the Worksheet_Change event, your action can only be performed when cell B52 changes the value from Yes to No or vice versa. But in order to program this event, you must investigate whether the change occurs in the cells that are the source for the formula in B52.
    Quote Originally Posted by PaulHenderson View Post
    Cell b52 contains a formula. It was a if formula with the answer as “Yes”or “no”
    We know what the formula returns from the very beginning. However, we still don't know what the source scope is. The problem may be more complex if B52 refers to the FORMULA in a different scope. Then we need to get to the source range of this formula. We need to get to the original range, the change of which ultimately results in the recalculation of cell B52. That's why I asked you to show in the attachment how it actually looks like.
    If you don't want to dig that deep, you can use the _Calculate event. But be warned, this is not correct.
    Please Login or Register  to view this content.
    The _Calculate event of this sheet can be dispatched when it is not active. It could even be another workbook that is open. In this case, Range(XX).Select will cause an error, because you can only select a cell in the active sheet.

    For the next time, please edit your previous post where you showed the code. Edit the post, select the code and use the [#] icon to add [code] tags.

    Artik

  8. #8
    Registered User
    Join Date
    09-04-2021
    Location
    Australia
    MS-Off Ver
    Microsoft 365
    Posts
    16

    Re: Excel VBA Hide - Show Rides

    Hi Artik, Amazing response, many thanks for taking the time. The users were happy to always show the rows in the end. I have another query about refeshing a pivot table via VBA. I will post separately. Thanks

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Excel VBA Hide - Show Rides

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Best Regards,

    Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Excel VBA How to hide or show columns
    By David Brimacomb in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-22-2021, 10:06 AM
  2. How to hide Excel but show the Userform only
    By samipm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-10-2015, 12:06 AM
  3. Check if a taxi was used or not from the list of rides
    By chrisvercser in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-30-2015, 08:25 AM
  4. [SOLVED] Excel VBA hide / Show Shape
    By wambaugh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2015, 11:00 AM
  5. excel sheet show/hide
    By arnab0711 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2010, 01:26 AM
  6. Testing two conditions, one over rides the other?
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-18-2007, 02:26 PM
  7. Essbase over-rides basic Functionality?
    By bflicker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2006, 03:45 AM

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