Code to hide and unhide columns, with certain condition?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code to enable a hidden column(C) to be unhidden, should
a certain category in the previous column(B) be chosen. I took that from an
online example. However what I trying to achieve without success is the
following: I would like column(C) automatically to be hidden again, once the
next cell in the next column has been moved to. Any help would be greatly
appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 And Target.Value = "Locators" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Detectors" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Survey/Navigation Equip." Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Machinery" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Medical Equip. Cap. Assets"
Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Weapons" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Desktops" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Laptops" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Printers" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Scanners" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Digital Cameras" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Radios" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Sat Phones" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Mobile Phones" Then
Columns("G").Hidden = False
ElseIf Target.Column = 5 And Target.Value = "Vehicles" Then
Columns("G").Hidden = False

End If
End Sub
 
I don't see anything in that code that references columns B or C?

When you say the next cell in the next column has been moved to, what
exactly do you mean? Do you have some tab order, or some control over where
they go next?
 
Frederic,

Try
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 5 And (Target.Value = "Locators" Or Target.Value =
"Detectors" Or Target.Value = "Survey/Navigation Equip." Or Target.Value =
"Machinery" Or Target.Value = "Medical Equip. Cap. Assets" Or Target.Value =
"Weapons" Or Target.Value = "Desktops" Or Target.Value = "Laptops" Or
Target.Value = "Printers" Or Target.Value = "Scanners" Or Target.Value =
"Digital Cameras" Or Target.Value = "Radios" Or Target.Value = "Sat Phones"
Or Target.Value = "Mobile Phones" Or Target.Value = "Vehicles") Then
Columns("G").Hidden = False
Else Columns("G").Hidden = True
End If
End Sub

Henry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top