Results 1 to 3 of 3

VBA Code Troubleshooting

Threaded 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

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