Hide & Add Rows

timesareGMT

New Member
Joined
Aug 19, 2011
Messages
26
HI all,

I'm new in VBA, and I’m trying to make a report layout with the conditions below:

1. If Cell D30 on Sheet 2 = FALSE, automatically hide Row 30 on Sheet 1.
2. If Cell D30 on Sheet 2 = FALSE, automatically add a new row after row 47.

How should I set the code?

Thanks a lot.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Delete the the previous code. I forgot that linked cells don't trigger the Worksheet Change event.

Put this in a standard module and assign this Checker macro to the 5 checkboxes. Right-click on each checkbox and select assign macro.

Code:
Sub Checker()
    
    Dim r As Long, cb As CheckBox

    Set cb = ActiveSheet.CheckBoxes(Application.Caller)
    r = Range(cb.LinkedCell).Row + 11
    
    Sheets("page 1").Rows(r).Hidden = cb.Value = xlOff
    If cb.Value = xlOff Then Sheets("page 1").Rows(48).Insert
        
End Sub
 
Upvote 0
Delete the the previous code. I forgot that linked cells don't trigger the Worksheet Change event.

Put this in a standard module and assign this Checker macro to the 5 checkboxes. Right-click on each checkbox and select assign macro.

Code:
Sub Checker()
 
    Dim r As Long, cb As CheckBox
 
    Set cb = ActiveSheet.CheckBoxes(Application.Caller)
    r = Range(cb.LinkedCell).Row + 11
 
    Sheets("page 1").Rows(r).Hidden = cb.Value = xlOff
    If cb.Value = xlOff Then Sheets("page 1").Rows(48).Insert
 
End Sub

YES!!! It works.

Thank you so much. :-D
 
Upvote 0
Delete the the previous code. I forgot that linked cells don't trigger the Worksheet Change event.

Put this in a standard module and assign this Checker macro to the 5 checkboxes. Right-click on each checkbox and select assign macro.

Code:
Sub Checker()
 
    Dim r As Long, cb As CheckBox
 
    Set cb = ActiveSheet.CheckBoxes(Application.Caller)
    r = Range(cb.LinkedCell).Row + 11
 
    Sheets("page 1").Rows(r).Hidden = cb.Value = xlOff
    If cb.Value = xlOff Then Sheets("page 1").Rows(48).Insert
 
End Sub


I got some other questions.

I add a new spreadsheet (saying page 3) in the current file, in which there's a combo box offering 2 values (saying GEO and BS).

I try to make the layout:
1. If I select "GEO" from the combo box, row 36 to 48 will hide.
2. If I select "BS", row 19 to 35 will hide.

How should I set the combo box code for this purpose?

Thanks.
 
Upvote 0
Assign this macro to your Forms-type combobox.

Code:
Sub Geo_BS()
    Dim dd As DropDown
    Set dd = ActiveSheet.DropDowns(Application.Caller)
    With Sheets("Page 1")
        Select Case UCase(Range(dd.ListFillRange)(dd.Value, 1))
            Case "GEO"
                .Rows("19:35").Hidden = False
                .Rows("36:48").Hidden = True
            Case "BS"
                .Rows("19:35").Hidden = True
                .Rows("36:48").Hidden = False
            Case Else
                .Rows("19:48").Hidden = False
        End Select
    End With
End Sub
 
Upvote 0
Assign this macro to your Forms-type combobox.

Code:
Sub Geo_BS()
    Dim dd As DropDown
    Set dd = ActiveSheet.DropDowns(Application.Caller)
    With Sheets("Page 1")
        Select Case UCase(Range(dd.ListFillRange)(dd.Value, 1))
            Case "GEO"
                .Rows("19:35").Hidden = False
                .Rows("36:48").Hidden = True
            Case "BS"
                .Rows("19:35").Hidden = True
                .Rows("36:48").Hidden = False
            Case Else
                .Rows("19:48").Hidden = False
        End Select
    End With
End Sub


Thanks for the info.

But sorry, I forgot to tell you that the hiding effect is within the same page (page 3).

I replaced the "Page 1" above with "Page 3", but nothing can be seen.

Have I missed something?
 
Upvote 0
What's the name of the combobox?

What is the Input range used to populate the combobox?

Are the two items in the Input range exactly GEO and BS? No leading or trailing spaces.
 
Upvote 0
What's the name of the combobox?

What is the Input range used to populate the combobox?

Are the two items in the Input range exactly GEO and BS? No leading or trailing spaces.


...I don't know there's a name for the combobox... I used the Form toolbar to insert the combobox on the spreadsheet.

Input range = $B$55:$B$56
Cell link = $B$57

The words "GEO" & "BS" were input in the cell B55 & B56 respectively.

The result of the combo box selection was refected in the cell C2 with the formula "=INDEX(B55:B56,B57)"

So what I'm trying to do is that:
1. If "GEO" was selected, C2 will show "GEO" and row 35 to 47 on the same page will hide automatically.
2. If "BS" was selected, C2 will show "BS" and row 18 to 34 on the same page will hide automatically.

Thanks for your help.
 
Upvote 0
It should work. Double check that you assigned the correct maco to the combobox. The macro should be located in a standard module.

Here is a more simplified macro.
Code:
Sub Geo_BS()
    Select Case UCase(Trim(Range("C2"))).Value
        Case "GEO"
            Rows("19:35").Hidden = False
            Rows("36:48").Hidden = True
        Case "BS"
            Rows("19:35").Hidden = True
            Rows("36:48").Hidden = False
        Case Else
            Rows("19:48").Hidden = False
    End Select
End Sub
 
Upvote 0
It should work. Double check that you assigned the correct maco to the combobox. The macro should be located in a standard module.

Here is a more simplified macro.
Code:
Sub Geo_BS()
    Select Case UCase(Trim(Range("C2"))).Value
        Case "GEO"
            Rows("19:35").Hidden = False
            Rows("36:48").Hidden = True
        Case "BS"
            Rows("19:35").Hidden = True
            Rows("36:48").Hidden = False
        Case Else
            Rows("19:48").Hidden = False
    End Select
End Sub

After making selection from the combobox, the screen shows:
"Run-time error '424': Object required"

Some info missed?

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top