+ Reply to Thread
Results 1 to 3 of 3

VBA Code Troubleshooting

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    139

    Lightbulb VBA Code Troubleshooting

    Hi All,

    I am trying to create VBA code that will automatically hide rows in my spreadsheet depending on whether other reference cells are marked with a Yes or a No.

    You select what items are included in the list (Flask, Toolkit, Cage, and Box). If you select Yes, this item is included in the list in cells B8:E11. If you select No, I would like the entire row for that item to be automatically hidden.

    Example:
    Flask: Yes
    Toolkit: No
    Cage: Yes
    Box: Yes

    In the above situation, row 9 on this spreadsheet should be automatically hidden, since that is the dedicated line for toolkits. With the help of GPT, I am currently working with the following code:

    SHEET1 (Code):
    Private Sub Worksheet_Change(ByVal Target As Range)
        ' Check if the change is in the C2:C5 range and if a single cell is edited
        If Not Intersect(Target, Me.Range("C2:C5")) Is Nothing Then
            ' If only one cell is edited (avoiding range changes like bulk edits)
            If Target.Cells.Count = 1 Then
                ' Call the AdjustRows macro when a change occurs in C2:C5
                Application.EnableEvents = False ' Disable events to avoid infinite loop
                Call AdjustRows
                Application.EnableEvents = True ' Re-enable events
            End If
        End If
    End Sub

    MODULE (Code):
    Sub AdjustRows()
        Dim ws As Worksheet
        Dim a As Variant
        Dim r As Integer
        Dim i As Integer
        
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Adjust the sheet name as necessary
        a = Array("Flask", "Toolkit", "Cage", "Box") ' Items to check
        
        ' Loop through each item
        For i = 0 To UBound(a)
            ' Check the corresponding "Yes" or "No" value in C2:C5
            If ws.Range("C2:C5").Cells(i + 1).Value = "Yes" Then
                ' If "Yes", unhide the row in the results table
                ws.Rows(8 + i).Hidden = False
            Else
                ' If "No", hide the row in the results table
                ws.Rows(8 + i).Hidden = True
            End If
        Next i
    End Sub
    So far, the macro works great, but I want this to happen automatically, which it currently does not. With the above code, selecting Yes or No changes nothing, unless I run the macro manually.

    Also, I have used the Worksheet_Change function in a separate spreadsheet (also for automatically hiding rows) for a separate purpose, and it worked fine there, so I don't think that is the problem.

    Anybody have any ideas as to how I can get this to work?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,830

    Re: VBA Code Troubleshooting

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

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,335

    Re: VBA Code Troubleshooting

    This is more then enough. In the Sheet module of Sheet1.
    I've also added Datavalidation in Range("C2:C5").

    Private Sub Worksheet_Change(ByVal Target As Range)
        ' Check if the change is in the C2:C5 range and if a single cell is edited
        If Not Intersect(Target, Range("C2:C5")) Is Nothing Then
            ' If only one cell is edited (avoiding range changes like bulk edits)
            If Target.Count = 1 Then
                ' Call the AdjustRows macro when a change occurs in C2:C5
                Target.Offset(6).EntireRow.Hidden = UCase(Target.Value) <> "YES"
            End If
        End If
    End Sub
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ 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. Troubleshooting VBA code using BarTender to print labels
    By liddy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-22-2024, 05:24 AM
  2. Replies: 9
    Last Post: 06-25-2024, 05:35 AM
  3. [SOLVED] VBA Code Troubleshooting
    By Keegan1116 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-17-2024, 08:18 AM
  4. [SOLVED] Troubleshooting - Prevent Duplicate Entries From VBA Code
    By mcodden in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2016, 11:40 AM
  5. [SOLVED] Deleting Duplicate Row code troubleshooting
    By ds16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2012, 04:31 PM
  6. Troubleshooting: Code works in some instances, but not all....
    By JP777 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-26-2012, 11:51 PM
  7. Troubleshooting code for export to text file.
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-03-2008, 07:44 PM

Tags for this Thread

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