Automate Collapse/Expand code for two cells

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

Boston617Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony BerenguelCommented:
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
Anthony BerenguelCommented:
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
Boston617Author Commented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Hakan YılmazTechnical Office MEP EngineerCommented:
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
Anthony BerenguelCommented:
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
Boston617Author Commented:
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
Hakan YılmazTechnical Office MEP EngineerCommented:
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
Anthony BerenguelCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Boston617Author Commented:
Yes! Thank you!
0
Anthony BerenguelCommented:
Awesome! I'm glad you're squared away. Have a great day!
0
Hakan YılmazTechnical Office MEP EngineerCommented:
@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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.