Help! VBA Code to hide specific columns containing selected text from drop down list

dvdlev

New Member
Joined
May 24, 2020
Messages
12
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
  2. MacOS
Hi,

I am trying to write a VBA code that would allow me to hide all columns containing whatever text/option has been selected from a drop down list in excel.

For example, all the options are within columns D:Z. The drop down list contains "Cat", "Dog", "Horse", "Fish". If I am selected on the Cat option in the drop down list then I want to hide all columns in "D:Z" containing Dog/Horse/Fish. On the other hand, if I am selected on "Dog", I want all columns containing "Cat", Horse", "Fish" to be hidden in columns "D:Z" and so on.

I am fairly new to writing VBA code and any help would be MUCH appreciated.

Thank you !
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Is the drop down list associated with a cell on the worksheet, or is it a combobox and if it is a combobox then where is it located (worksheet or Userform)?
 
Upvote 0
You haven't specified where your dropdown list is, SO I have assumed the result is in Cell A1:
VBA Code:
Sub test2()
animal = Cells(1, 1)
If animal <> "" Then
lastrow = ActiveSheet.UsedRange.Rows.Count
inarr = Range(Cells(1, 1), Cells(lastrow, 26))
Columns("D:Z").Hidden = True
 For i = 4 To 26
  For j = 1 To lastrow
     If animal = inarr(j, i) Then
      Columns(i).Hidden = False
      Exit For
     End If
  Next j
Next i
End If
 
Upvote 0
Is the drop down list associated with a cell on the worksheet, or is it a combobox and if it is a combobox then where is it located (worksheet or Userform)?

its a drop down list, the names are on another sheet in the background
 
Upvote 0
You haven't specified where your dropdown list is, SO I have assumed the result is in Cell A1:
VBA Code:
Sub test2()
animal = Cells(1, 1)
If animal <> "" Then
lastrow = ActiveSheet.UsedRange.Rows.Count
inarr = Range(Cells(1, 1), Cells(lastrow, 26))
Columns("D:Z").Hidden = True
For i = 4 To 26
  For j = 1 To lastrow
     If animal = inarr(j, i) Then
      Columns(i).Hidden = False
      Exit For
     End If
  Next j
Next i
End If


I ran it but that didn't work do anything. was I supposed to change anything in your code? I just pasted it in and ran. also, it was a drop down list, the names are on another sheet in the background. thanks for helping out!
 
Upvote 0
The question was: "Is the drop down in cell A1?" If it is not in cell A1 then the code will not produce any results because it is referencing the wrong place to look. The code below also uses A1 as the reference cell, if it is not that cell where you have the drop down, then change the code to reflect the cell where the dropdown list is. Please respond to the questions asked, they are asked for a reason.

VBA Code:
Sub t()
Dim fn As Range, adr As String, i As Long
Sheet1.Range("D:Z").EntireColumn.Hidden = True
    For i = 4 To 26
        If Application.CountIf(Sheet1.Columns(i), Sheet1.Range("A1").Value) > 0 Then
            Columns(i).Hidden = False
        End If
    Next
End Sub

Where the llist for the dropdown is derived from is irrelevant for this purpose. We need to know where on the sheet the drop down box is located.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top