code to hide hides too much

M

Marilyn

Hello I want to hide columns "e" and "f" if cells C7 says anything else
than "Web System ". I created the code below and it works except that it also
hides columns "e" and "f" if I enter anything in in row 7 thanks

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And (Target.Value = "Web System") Then
Columns("E:F").Hidden = False
Else: Columns("E:F").Hidden = True
End If
End Sub
 
T

Tyro

You're checking only for a change in Column C. There is no reference to row
7. Your code should look something like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And Target.Row = 7 Then
If Target.Value = "WS" Then
Columns("E:F").Hidden = False
Else
Columns("E:F").Hidden = True
End If
End If
End Sub

Tyro
 
J

Jim Cone

The following does what you requested.
However, I doubt it is what you want?...
'--
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$7" Then
If (Target.Value = "Web System") Then
Columns("E:F").Hidden = False
Else
Columns("E:F").Hidden = True
End If
End If
End Sub
'--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)


"Marilyn"
wrote in message
Hello I want to hide columns "e" and "f" if cells C7 says anything else
than "Web System ". I created the code below and it works except that it also
hides columns "e" and "f" if I enter anything in in row 7 thanks

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And (Target.Value = "Web System") Then
Columns("E:F").Hidden = False
Else: Columns("E:F").Hidden = True
End If
End Sub
 
D

Dave Peterson

I'd use something like:

Private Sub Worksheet_Change(ByVal Target As Range)
if target.cells.count > 1 then exit sub 'one cell at a time
if intersect(target, me.range("C7")) is nothing then exit sub

if lcase(target.value) = lcase("web system") then
me.columns("E:F").hidden = true
else
me.columns("E:F").hidden = false
end if

End Sub

You could actually replace that if/then/else with this:

me.columns("E:F").hidden = cbool(lcase(target.value) = lcase("web system"))
 

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

Top