Solved

Automate Collapse/Expand code for two cells

Posted on 2014-12-22
11
88 Views
Last Modified: 2014-12-22
I'm trying to have a range of two cells (C31-32) that when both equal 0, a certain amount of cells collapse. I also have it so that certain cells inside it collapse or expand if they are greater or equal 0 as well. Below is the code I'm using, in bold is the code i'm trying to work with

Also, I keep getting an error message of "Run-time error '13':Type Mismatch", what does this mean and how do I deal with it?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("$A$12:$A$1500")) Then

    If Range("C24").Value = 0 Then
        Rows("24:27").EntireRow.Hidden = True
    Else
        Rows("24:27").EntireRow.Hidden = False
    End If
    
    If Range("C23").Value = 0 Then
        Rows("23").EntireRow.Hidden = True
    Else
        Rows("23").EntireRow.Hidden = False
    End If
    
    If Range("C32").Value = 0 Then
        Rows("32:35").EntireRow.Hidden = True
    Else
        Rows("32:35").EntireRow.Hidden = False
    End If
    
    If Range("C31").Value = 0 Then
        Rows("31").EntireRow.Hidden = True
    Else
        Rows("31").EntireRow.Hidden = False
    End If
    
  [b]  If Range("C31, C32").Value = 0 Then
        Rows("30:35").EntireRow.Hidden = True
    Else
        Rows("30:35").EntireRow.Hidden = False
    End If[/b]
        
End If

End Sub

Open in new window

0
Comment
Question by:Boston617
  • 5
  • 3
  • 3
11 Comments
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40513550
try this...replace the section you have with what's below.
   If Range("C31").Value = 0 AND Range("C32").Value = 0Then
        Rows("30:35").EntireRow.Hidden = True
    Else
        Rows("30:35").EntireRow.Hidden = False
    End If
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40513553
turn
If Range("C31, C32").Value = 0 Then
        Rows("30:35").EntireRow.Hidden = True
Else
        Rows("30:35").EntireRow.Hidden = False
End If

Open in new window


into this
If Range("C31").Value = 0 and Range("C32").Value = 0  Then
        Rows("30:35").EntireRow.Hidden = True
Else
        Rows("30:35").EntireRow.Hidden = False
End If

Open in new window

0
 

Author Comment

by:Boston617
ID: 40513560
Thank you

But now the prior two Ifs

If Range("C32").Value = 0 Then
        Rows("32:35").EntireRow.Hidden = True
    Else
        Rows("32:35").EntireRow.Hidden = False
    End If

Open in new window

   
and

 
  If Range("C31").Value = 0 Then
        Rows("31").EntireRow.Hidden = True
    Else
        Rows("31").EntireRow.Hidden = False
    End If

Open in new window


no longer seem to function
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40513579
Please try this.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("$A$12:$A$1500")) Then
        Rows("24:27").EntireRow.Hidden = Range("C24").Value = 0
        Rows("23").EntireRow.Hidden = Range("C23").Value = 0
        Rows("32:35").EntireRow.Hidden = Range("C32").Value = 0
        Rows("31").EntireRow.Hidden = Range("C31").Value = 0
        Rows("30:35").EntireRow.Hidden = Range("C31").Value2 = 0 And Range("C32").Value2 = 0
    End If
End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40513580
You mean the if statements don't work anymore? Or do you mean that your code doesn't do what you intend it to do?

Follow the logic in your code and you should be able to figure it out. Perhaps you don't need those prior if statements? It's hard for me to know without knowing what it is you're trying to do.  See my comments in your code below.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

	If Not Intersect(Target, Range("$A$12:$A$1500")) Then

		If Range("C24").Value = 0 Then
			Rows("24:27").EntireRow.Hidden = True
		Else
			Rows("24:27").EntireRow.Hidden = False
		End If
		
		If Range("C23").Value = 0 Then
			Rows("23").EntireRow.Hidden = True
		Else
			Rows("23").EntireRow.Hidden = False
		End If
		
		'checking for c32 = 0 here
		If Range("C32").Value = 0 Then
			Rows("32:35").EntireRow.Hidden = True
		Else
			Rows("32:35").EntireRow.Hidden = False
		End If
		
		'checking for c31 = 0 here
		If Range("C31").Value = 0 Then
			Rows("31").EntireRow.Hidden = True
		Else
			Rows("31").EntireRow.Hidden = False
		End If
		
		'checking for c31 is 0 again and checking for c32 is 0 again. Do you need the prior to if statements?
		If Range("C31").Value = 0 and Range("C32").Value = 0 Then
			Rows("30:35").EntireRow.Hidden = True
		Else
			Rows("30:35").EntireRow.Hidden = False
		End If
			
	End If

End Sub

Open in new window

0
 

Author Comment

by:Boston617
ID: 40513610
Hi Anthony,

I  apologize for not explaining clearly.

I need cells C30:C35 to collapse if both C31 and C32 are zero.
Rows 32-35 would need to collapse if C31>0
and only Row 31 would need to collapse if C32>0
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40513620
What are you trying to do with intersect?
To check if target is in the range you defined?

If so, then code should be better as follows.
Then you will need to click in range A12:A1500 for your code to run.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("$A$12:$A$1500")) Is Nothing Then
        Rows("24:27").EntireRow.Hidden = Range("C24").Value = 0
        Rows("23").EntireRow.Hidden = Range("C23").Value = 0
        Rows("32:35").EntireRow.Hidden = Range("C32").Value = 0
        Rows("31").EntireRow.Hidden = Range("C31").Value = 0
        Rows("30:35").EntireRow.Hidden = Range("C31").Value2 = 0 And Range("C32").Value2 = 0
    End If
End Sub

Open in new window

0
 
LVL 10

Accepted Solution

by:
Anthony Berenguel earned 500 total points
ID: 40513642
try this
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

	If Not Intersect(Target, Range("$A$12:$A$1500")) Then

		If Range("C24").Value = 0 Then
			Rows("24:27").EntireRow.Hidden = True
		Else
			Rows("24:27").EntireRow.Hidden = False
		End If
		
		If Range("C23").Value = 0 Then
			Rows("23").EntireRow.Hidden = True
		Else
			Rows("23").EntireRow.Hidden = False
		End If
		
		If Range("C31").Value = 0 and Range("C32").Value = 0 Then
			'both ranges are zero, hide rows 30-35
			Rows("30:35").EntireRow.Hidden = True
		Elseif Range("C32").Value > 0  then
			'range > 0, display rows 32-35
			Rows("32:35").EntireRow.Hidden = False
			'hide row 31
			Rows("31").EntireRow.Hidden = true
		elseif range("c31").value > 0 then
			'range > 0, display row 31
			Rows("31").EntireRow.Hidden = False
			'hide rows 32-35
			Rows("32:35").EntireRow.Hidden = true
		else
			'display rows 30-35
			Rows("30:35").EntireRow.Hidden = False
		End If
			
	End If

End Sub

Open in new window

0
 

Author Closing Comment

by:Boston617
ID: 40513669
Yes! Thank you!
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40513671
Awesome! I'm glad you're squared away. Have a great day!
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40513857
@Boston617 You still need to be more clear.

Do you want  Row 30 to be visible when C31 or C32 is zero? If you don't mind this, it may randomly change.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question