PC Review


Reply
Thread Tools Rate Thread

Data Validation List Hide Unhide Rows

 
 
Eric
Guest
Posts: n/a
 
      23rd Jul 2008
Hello,

Let me start by saying, I have years of excel experience, but very little
VBA experience.

I currently have a data validation list with 8 options. My goal is to have
the user select an option, then have a section expand just below that, based
on what they select.

I have attempted to write a macro to do this, but failed miserably. (I
decided to start with the first 2 options until I could get those to work) I
don't know if this is even close, but this is what I have started:

Private Sub Worksheet_Change(ByVal Target As Range)

Select Case Range("vUtility_Company").Value

Case ""
PS_MinimizeALL
Case "PGE Residential"
PGE_Res
Case "PGE Business"
PGE_Bus
Case Else
Exit Sub

End Select

End Sub


Sub PS_MinimizeALL()

If [vUtility_Company] = "" Then
Rows("31:63").Select
Selection.EntireRow.Hidden = True
End If

End Sub


Sub PGE_Res()

If [vUtility_Company] = "PGE Residential" Then
Rows("31:63").Select
Selection.EntireRow.Hidden = False

ElseIf [vUtility_Company] <> "PGE Residential" Then
Rows("30:64").Select
Selection.EntireRow.Hidden = True
End If

End Sub

Sub PGE_Bus()

If [vUtility_Company] = "PGE Business" Then
Rows("31:63").Select
Selection.EntireRow.Hidden = True

ElseIf [vUtility_Company] <> "PGE Business" Then
Rows("30:64").Select
Selection.EntireRow.Hidden = False
End If

End Sub
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      23rd Jul 2008
One problem is that in those subroutines (like PGE_Res) is that the way you got
there was that "PGE Residential" was typed into that vUtility_Company cell.

That means that the "if" portion of this statement has to be true (or you
wouldn't be in that routine. And the Else portion will never be true. (I am
assuming that those subroutines are not called by any other routine.)

> Sub PGE_Res()
>
> If [vUtility_Company] = "PGE Residential" Then
> Rows("31:63").Select
> Selection.EntireRow.Hidden = False
>
> ElseIf [vUtility_Company] <> "PGE Residential" Then
> Rows("30:64").Select
> Selection.EntireRow.Hidden = True
> End If


Maybe something like:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Me.Range("vUtility_Company")) Is Nothing Then
Exit Sub
End If

Select Case LCase(Me.Range("vUtility_Company").Value)
Case ""
Call PS_MinimizeALL
Case LCase("PGE Residential")
Call PGE_Res
Case LCase("PGE Business")
Call PGE_Bus
Case Else
'do nothing, just continue to the end sub
End Select

End Sub
Sub PS_MinimizeALL()
Me.Rows("31:63").EntireRow.Hidden = True
End Sub
Sub PGE_Res()
Me.Rows("31:63").EntireRow.Hidden = False
End Sub
Sub PGE_Bus()
Me.Rows("31:63").EntireRow.Hidden = True
End Sub

===============
I gotta feeling that you really want to unhide/hide some other rows at the same
time.

You may want to show everything and then hide just the ones you want:

Sub PS_MinimizeALL()
Me.Rows.Hidden = False
Me.Rows("31:63").Hidden = True
End Sub




Eric wrote:
>
> Hello,
>
> Let me start by saying, I have years of excel experience, but very little
> VBA experience.
>
> I currently have a data validation list with 8 options. My goal is to have
> the user select an option, then have a section expand just below that, based
> on what they select.
>
> I have attempted to write a macro to do this, but failed miserably. (I
> decided to start with the first 2 options until I could get those to work) I
> don't know if this is even close, but this is what I have started:
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> Select Case Range("vUtility_Company").Value
>
> Case ""
> PS_MinimizeALL
> Case "PGE Residential"
> PGE_Res
> Case "PGE Business"
> PGE_Bus
> Case Else
> Exit Sub
>
> End Select
>
> End Sub
>
> Sub PS_MinimizeALL()
>
> If [vUtility_Company] = "" Then
> Rows("31:63").Select
> Selection.EntireRow.Hidden = True
> End If
>
> End Sub
>
> Sub PGE_Res()
>
> If [vUtility_Company] = "PGE Residential" Then
> Rows("31:63").Select
> Selection.EntireRow.Hidden = False
>
> ElseIf [vUtility_Company] <> "PGE Residential" Then
> Rows("30:64").Select
> Selection.EntireRow.Hidden = True
> End If
>
> End Sub
>
> Sub PGE_Bus()
>
> If [vUtility_Company] = "PGE Business" Then
> Rows("31:63").Select
> Selection.EntireRow.Hidden = True
>
> ElseIf [vUtility_Company] <> "PGE Business" Then
> Rows("30:64").Select
> Selection.EntireRow.Hidden = False
> End If
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      23rd Jul 2008
Thank you for your help David. Your assumption is correct. I want to hide all
sections except the one that applies to the value selected in my list.

I pasted the code into the macro and I could not get anything to happen when
I changed the value of "vUtility_Company". Next I tried to run the macro (I
thought maybe I had to initiate it?) but I only got an error. When I tried to
run it manually I am getting an "Invalid use of Me keyword." error.

Any ideas?

"Dave Peterson" wrote:

> One problem is that in those subroutines (like PGE_Res) is that the way you got
> there was that "PGE Residential" was typed into that vUtility_Company cell.
>
> That means that the "if" portion of this statement has to be true (or you
> wouldn't be in that routine. And the Else portion will never be true. (I am
> assuming that those subroutines are not called by any other routine.)
>
> > Sub PGE_Res()
> >
> > If [vUtility_Company] = "PGE Residential" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = False
> >
> > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > Rows("30:64").Select
> > Selection.EntireRow.Hidden = True
> > End If

>
> Maybe something like:
>
> Option Explicit
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Intersect(Target, Me.Range("vUtility_Company")) Is Nothing Then
> Exit Sub
> End If
>
> Select Case LCase(Me.Range("vUtility_Company").Value)
> Case ""
> Call PS_MinimizeALL
> Case LCase("PGE Residential")
> Call PGE_Res
> Case LCase("PGE Business")
> Call PGE_Bus
> Case Else
> 'do nothing, just continue to the end sub
> End Select
>
> End Sub
> Sub PS_MinimizeALL()
> Me.Rows("31:63").EntireRow.Hidden = True
> End Sub
> Sub PGE_Res()
> Me.Rows("31:63").EntireRow.Hidden = False
> End Sub
> Sub PGE_Bus()
> Me.Rows("31:63").EntireRow.Hidden = True
> End Sub
>
> ===============
> I gotta feeling that you really want to unhide/hide some other rows at the same
> time.
>
> You may want to show everything and then hide just the ones you want:
>
> Sub PS_MinimizeALL()
> Me.Rows.Hidden = False
> Me.Rows("31:63").Hidden = True
> End Sub
>
>
>
>
> Eric wrote:
> >
> > Hello,
> >
> > Let me start by saying, I have years of excel experience, but very little
> > VBA experience.
> >
> > I currently have a data validation list with 8 options. My goal is to have
> > the user select an option, then have a section expand just below that, based
> > on what they select.
> >
> > I have attempted to write a macro to do this, but failed miserably. (I
> > decided to start with the first 2 options until I could get those to work) I
> > don't know if this is even close, but this is what I have started:
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> >
> > Select Case Range("vUtility_Company").Value
> >
> > Case ""
> > PS_MinimizeALL
> > Case "PGE Residential"
> > PGE_Res
> > Case "PGE Business"
> > PGE_Bus
> > Case Else
> > Exit Sub
> >
> > End Select
> >
> > End Sub
> >
> > Sub PS_MinimizeALL()
> >
> > If [vUtility_Company] = "" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = True
> > End If
> >
> > End Sub
> >
> > Sub PGE_Res()
> >
> > If [vUtility_Company] = "PGE Residential" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = False
> >
> > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > Rows("30:64").Select
> > Selection.EntireRow.Hidden = True
> > End If
> >
> > End Sub
> >
> > Sub PGE_Bus()
> >
> > If [vUtility_Company] = "PGE Business" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = True
> >
> > ElseIf [vUtility_Company] <> "PGE Business" Then
> > Rows("30:64").Select
> > Selection.EntireRow.Hidden = False
> > End If
> >
> > End Sub

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      23rd Jul 2008
Hello David,

Sorry about the message earlier, I didn't realize I needed to paste the code
directly into the code for that sheet. Once I did that everything you wrote
worked perfectly. Thanks again for the help!

"Eric" wrote:

> Thank you for your help David. Your assumption is correct. I want to hide all
> sections except the one that applies to the value selected in my list.
>
> I pasted the code into the macro and I could not get anything to happen when
> I changed the value of "vUtility_Company". Next I tried to run the macro (I
> thought maybe I had to initiate it?) but I only got an error. When I tried to
> run it manually I am getting an "Invalid use of Me keyword." error.
>
> Any ideas?
>
> "Dave Peterson" wrote:
>
> > One problem is that in those subroutines (like PGE_Res) is that the way you got
> > there was that "PGE Residential" was typed into that vUtility_Company cell.
> >
> > That means that the "if" portion of this statement has to be true (or you
> > wouldn't be in that routine. And the Else portion will never be true. (I am
> > assuming that those subroutines are not called by any other routine.)
> >
> > > Sub PGE_Res()
> > >
> > > If [vUtility_Company] = "PGE Residential" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = False
> > >
> > > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > > Rows("30:64").Select
> > > Selection.EntireRow.Hidden = True
> > > End If

> >
> > Maybe something like:
> >
> > Option Explicit
> > Private Sub Worksheet_Change(ByVal Target As Range)
> >
> > If Intersect(Target, Me.Range("vUtility_Company")) Is Nothing Then
> > Exit Sub
> > End If
> >
> > Select Case LCase(Me.Range("vUtility_Company").Value)
> > Case ""
> > Call PS_MinimizeALL
> > Case LCase("PGE Residential")
> > Call PGE_Res
> > Case LCase("PGE Business")
> > Call PGE_Bus
> > Case Else
> > 'do nothing, just continue to the end sub
> > End Select
> >
> > End Sub
> > Sub PS_MinimizeALL()
> > Me.Rows("31:63").EntireRow.Hidden = True
> > End Sub
> > Sub PGE_Res()
> > Me.Rows("31:63").EntireRow.Hidden = False
> > End Sub
> > Sub PGE_Bus()
> > Me.Rows("31:63").EntireRow.Hidden = True
> > End Sub
> >
> > ===============
> > I gotta feeling that you really want to unhide/hide some other rows at the same
> > time.
> >
> > You may want to show everything and then hide just the ones you want:
> >
> > Sub PS_MinimizeALL()
> > Me.Rows.Hidden = False
> > Me.Rows("31:63").Hidden = True
> > End Sub
> >
> >
> >
> >
> > Eric wrote:
> > >
> > > Hello,
> > >
> > > Let me start by saying, I have years of excel experience, but very little
> > > VBA experience.
> > >
> > > I currently have a data validation list with 8 options. My goal is to have
> > > the user select an option, then have a section expand just below that, based
> > > on what they select.
> > >
> > > I have attempted to write a macro to do this, but failed miserably. (I
> > > decided to start with the first 2 options until I could get those to work) I
> > > don't know if this is even close, but this is what I have started:
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > >
> > > Select Case Range("vUtility_Company").Value
> > >
> > > Case ""
> > > PS_MinimizeALL
> > > Case "PGE Residential"
> > > PGE_Res
> > > Case "PGE Business"
> > > PGE_Bus
> > > Case Else
> > > Exit Sub
> > >
> > > End Select
> > >
> > > End Sub
> > >
> > > Sub PS_MinimizeALL()
> > >
> > > If [vUtility_Company] = "" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = True
> > > End If
> > >
> > > End Sub
> > >
> > > Sub PGE_Res()
> > >
> > > If [vUtility_Company] = "PGE Residential" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = False
> > >
> > > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > > Rows("30:64").Select
> > > Selection.EntireRow.Hidden = True
> > > End If
> > >
> > > End Sub
> > >
> > > Sub PGE_Bus()
> > >
> > > If [vUtility_Company] = "PGE Business" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = True
> > >
> > > ElseIf [vUtility_Company] <> "PGE Business" Then
> > > Rows("30:64").Select
> > > Selection.EntireRow.Hidden = False
> > > End If
> > >
> > > End Sub

> >
> > --
> >
> > Dave Peterson
> >

 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      29th Jul 2008
Hello Dave,

How would I set up another Worksheet_Change on the same sheet for a
different cell value? This one is called "vOwnRent"

Thanks again

"Dave Peterson" wrote:

> One problem is that in those subroutines (like PGE_Res) is that the way you got
> there was that "PGE Residential" was typed into that vUtility_Company cell.
>
> That means that the "if" portion of this statement has to be true (or you
> wouldn't be in that routine. And the Else portion will never be true. (I am
> assuming that those subroutines are not called by any other routine.)
>
> > Sub PGE_Res()
> >
> > If [vUtility_Company] = "PGE Residential" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = False
> >
> > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > Rows("30:64").Select
> > Selection.EntireRow.Hidden = True
> > End If

>
> Maybe something like:
>
> Option Explicit
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Intersect(Target, Me.Range("vUtility_Company")) Is Nothing Then
> Exit Sub
> End If
>
> Select Case LCase(Me.Range("vUtility_Company").Value)
> Case ""
> Call PS_MinimizeALL
> Case LCase("PGE Residential")
> Call PGE_Res
> Case LCase("PGE Business")
> Call PGE_Bus
> Case Else
> 'do nothing, just continue to the end sub
> End Select
>
> End Sub
> Sub PS_MinimizeALL()
> Me.Rows("31:63").EntireRow.Hidden = True
> End Sub
> Sub PGE_Res()
> Me.Rows("31:63").EntireRow.Hidden = False
> End Sub
> Sub PGE_Bus()
> Me.Rows("31:63").EntireRow.Hidden = True
> End Sub
>
> ===============
> I gotta feeling that you really want to unhide/hide some other rows at the same
> time.
>
> You may want to show everything and then hide just the ones you want:
>
> Sub PS_MinimizeALL()
> Me.Rows.Hidden = False
> Me.Rows("31:63").Hidden = True
> End Sub
>
>
>
>
> Eric wrote:
> >
> > Hello,
> >
> > Let me start by saying, I have years of excel experience, but very little
> > VBA experience.
> >
> > I currently have a data validation list with 8 options. My goal is to have
> > the user select an option, then have a section expand just below that, based
> > on what they select.
> >
> > I have attempted to write a macro to do this, but failed miserably. (I
> > decided to start with the first 2 options until I could get those to work) I
> > don't know if this is even close, but this is what I have started:
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> >
> > Select Case Range("vUtility_Company").Value
> >
> > Case ""
> > PS_MinimizeALL
> > Case "PGE Residential"
> > PGE_Res
> > Case "PGE Business"
> > PGE_Bus
> > Case Else
> > Exit Sub
> >
> > End Select
> >
> > End Sub
> >
> > Sub PS_MinimizeALL()
> >
> > If [vUtility_Company] = "" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = True
> > End If
> >
> > End Sub
> >
> > Sub PGE_Res()
> >
> > If [vUtility_Company] = "PGE Residential" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = False
> >
> > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > Rows("30:64").Select
> > Selection.EntireRow.Hidden = True
> > End If
> >
> > End Sub
> >
> > Sub PGE_Bus()
> >
> > If [vUtility_Company] = "PGE Business" Then
> > Rows("31:63").Select
> > Selection.EntireRow.Hidden = True
> >
> > ElseIf [vUtility_Company] <> "PGE Business" Then
> > Rows("30:64").Select
> > Selection.EntireRow.Hidden = False
> > End If
> >
> > End Sub

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      29th Jul 2008
You only get one worksheet event per worksheet. But that procedure can have as
many branches as you need.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If not (Intersect(Target, Me.Range("vUtility_Company")) Is Nothing) Then
Select Case LCase(Me.Range("vUtility_Company").Value)
Case ""
Call PS_MinimizeALL
Case LCase("PGE Residential")
Call PGE_Res
Case LCase("PGE Business")
Call PGE_Bus
Case Else
'do nothing, just continue to the end sub
End Select
else
if not (intersect(target, me.range("vOwnRent")) is nothing) then
'do what you want for vOwnRent
end if
end if

End Sub

Eric wrote:
>
> Hello Dave,
>
> How would I set up another Worksheet_Change on the same sheet for a
> different cell value? This one is called "vOwnRent"
>
> Thanks again
>
> "Dave Peterson" wrote:
>
> > One problem is that in those subroutines (like PGE_Res) is that the way you got
> > there was that "PGE Residential" was typed into that vUtility_Company cell.
> >
> > That means that the "if" portion of this statement has to be true (or you
> > wouldn't be in that routine. And the Else portion will never be true. (I am
> > assuming that those subroutines are not called by any other routine.)
> >
> > > Sub PGE_Res()
> > >
> > > If [vUtility_Company] = "PGE Residential" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = False
> > >
> > > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > > Rows("30:64").Select
> > > Selection.EntireRow.Hidden = True
> > > End If

> >
> > Maybe something like:
> >
> > Option Explicit
> > Private Sub Worksheet_Change(ByVal Target As Range)
> >
> > If Intersect(Target, Me.Range("vUtility_Company")) Is Nothing Then
> > Exit Sub
> > End If
> >
> > Select Case LCase(Me.Range("vUtility_Company").Value)
> > Case ""
> > Call PS_MinimizeALL
> > Case LCase("PGE Residential")
> > Call PGE_Res
> > Case LCase("PGE Business")
> > Call PGE_Bus
> > Case Else
> > 'do nothing, just continue to the end sub
> > End Select
> >
> > End Sub
> > Sub PS_MinimizeALL()
> > Me.Rows("31:63").EntireRow.Hidden = True
> > End Sub
> > Sub PGE_Res()
> > Me.Rows("31:63").EntireRow.Hidden = False
> > End Sub
> > Sub PGE_Bus()
> > Me.Rows("31:63").EntireRow.Hidden = True
> > End Sub
> >
> > ===============
> > I gotta feeling that you really want to unhide/hide some other rows at the same
> > time.
> >
> > You may want to show everything and then hide just the ones you want:
> >
> > Sub PS_MinimizeALL()
> > Me.Rows.Hidden = False
> > Me.Rows("31:63").Hidden = True
> > End Sub
> >
> >
> >
> >
> > Eric wrote:
> > >
> > > Hello,
> > >
> > > Let me start by saying, I have years of excel experience, but very little
> > > VBA experience.
> > >
> > > I currently have a data validation list with 8 options. My goal is to have
> > > the user select an option, then have a section expand just below that, based
> > > on what they select.
> > >
> > > I have attempted to write a macro to do this, but failed miserably. (I
> > > decided to start with the first 2 options until I could get those to work) I
> > > don't know if this is even close, but this is what I have started:
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > >
> > > Select Case Range("vUtility_Company").Value
> > >
> > > Case ""
> > > PS_MinimizeALL
> > > Case "PGE Residential"
> > > PGE_Res
> > > Case "PGE Business"
> > > PGE_Bus
> > > Case Else
> > > Exit Sub
> > >
> > > End Select
> > >
> > > End Sub
> > >
> > > Sub PS_MinimizeALL()
> > >
> > > If [vUtility_Company] = "" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = True
> > > End If
> > >
> > > End Sub
> > >
> > > Sub PGE_Res()
> > >
> > > If [vUtility_Company] = "PGE Residential" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = False
> > >
> > > ElseIf [vUtility_Company] <> "PGE Residential" Then
> > > Rows("30:64").Select
> > > Selection.EntireRow.Hidden = True
> > > End If
> > >
> > > End Sub
> > >
> > > Sub PGE_Bus()
> > >
> > > If [vUtility_Company] = "PGE Business" Then
> > > Rows("31:63").Select
> > > Selection.EntireRow.Hidden = True
> > >
> > > ElseIf [vUtility_Company] <> "PGE Business" Then
> > > Rows("30:64").Select
> > > Selection.EntireRow.Hidden = False
> > > End If
> > >
> > > End Sub

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enabling option „Format rows“ to hide/unhide rows using VBA-code? ran58 Microsoft Excel Misc 0 28th Jul 2009 03:46 PM
unhide rows based on data validation cathy Microsoft Excel Programming 7 5th Jun 2008 08:41 PM
Hide/Show Rows based on Cell Value with Data Validation Shelly Microsoft Excel Programming 3 4th Jan 2008 11:01 PM
Hide Rows (Current Date) / Unhide Rows =?Utf-8?B?Sm9lIEsu?= Microsoft Excel Programming 1 10th Oct 2007 05:37 PM
Hide Columns based on a Data Validation List Ricky Pang Microsoft Excel Programming 4 15th Sep 2004 02:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.