Solved

Automate Collapse/Expand code for two cells

Posted on 2014-12-22
11
87 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vlookup for in-between dates 4 40
Error with a code discussed on this page 5 16
sort column using vba 2 19
VBA to add shapes inside a chart 9 20
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 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