Solved

Automate Collapse/Expand code for two cells

Posted on 2014-12-22
11
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

739 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