Solved

Automate Collapse/Expand code for two cells

Posted on 2014-12-22
11
85 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:Hakan Yılmaz
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Boston617
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes! Thank you!
0
 
LVL 10

Expert Comment

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

Expert Comment

by:Hakan Yılmaz
Comment Utility
@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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now