VBA code to Hide and Unhide Columns based on a cell value

Mikey mike

New Member
Joined
Jul 8, 2013
Messages
6
I need to Hide Columns based on Cell value. I have tried using VB codes supplied on this forum and get some of it to work but not all. Please see below
Value in Cell D1 and the columns that need to be hidden based on this result:

IQC - F,H,I
Factory Flash -F,H,I
Build Test - F,H,I
Diagnostics - G:I
SMT - BGA- F,H,I
Assembly Tech- F,H,I
RF Calibration- F,H,I
Software- F,H,I
OQC-F,J
IMEI Clear- F,H,I
OOBA- F,H,I

<tbody>
</tbody>
Please help...loosing my mind here.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I need to Hide Columns based on Cell value. I have tried using VB codes supplied on this forum and get some of it to work but not all. Please see below
Value in Cell D1 and the columns that need to be hidden based on this result:

IQC - F,H,I
Factory Flash -F,H,I
Build Test - F,H,I
Diagnostics - G:I
SMT - BGA- F,H,I
Assembly Tech- F,H,I
RF Calibration- F,H,I
Software- F,H,I
OQC-F,J
IMEI Clear- F,H,I
OOBA- F,H,I

<tbody>
</tbody>
Please help...loosing my mind here.
Hi Mikey Mike,

How about something like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Address = "$D$1" Then
    Select Case Target.Value
        Case "IQC"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "Factory Flash"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "Build Test"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "Diagnostics"
            Cells.EntireColumn.Hidden = False
            Range("G1:I1").EntireColumn.Hidden = True
        Case "SMT BGA"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "Assembly Tech"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "RF Calibration"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "Software"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "OQC"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("J1").EntireColumn.Hidden = True
        Case "IMEI Clear"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case "OOBA"
            Cells.EntireColumn.Hidden = False
            Range("F1").EntireColumn.Hidden = True
            Range("H1").EntireColumn.Hidden = True
            Range("I1").EntireColumn.Hidden = True
        Case Else
            Cells.EntireColumn.Hidden = False
    End Select
End If


End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter your data in cell D1 and exit the cell.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D1")) Is Nothing Then Exit Sub
    ActiveSheet.Columns.EntireColumn.Hidden = False
    Select Case Target.Value
        Case "IQC", "Factory Flash", "Build test", "SMT - BGA", "Assembly Tech", "RF Combination", "Software", "IMEI Clear", "OOBA"
            Range("F:F,H:H,I:I").EntireColumn.Hidden = True
        Case "Diagnostics"
            Range("G:G,I:I").EntireColumn.Hidden = True
        Case "OQC"
            Range("F:F,J:J").EntireColumn.Hidden = True
    End Select
End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter your data in cell D1 and exit the cell.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D1")) Is Nothing Then Exit Sub
    ActiveSheet.Columns.EntireColumn.Hidden = False
    Select Case Target.Value
        Case "IQC", "Factory Flash", "Build test", "SMT - BGA", "Assembly Tech", "RF Combination", "Software", "IMEI Clear", "OOBA"
            Range("F:F,H:H,I:I").EntireColumn.Hidden = True
        Case "Diagnostics"
            Range("G:G,I:I").EntireColumn.Hidden = True
        Case "OQC"
            Range("F:F,J:J").EntireColumn.Hidden = True
    End Select
End Sub
Far more elegant than my crude hamfisted suggestion ;)
 
Upvote 0
@Fishboy & bugmonsta: Thank you so much for your comment and reps. :)
 
Upvote 0

Forum statistics

Threads
1,215,447
Messages
6,124,909
Members
449,195
Latest member
Stevenciu

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