Combining VBA code for different commands

jareed123

New Member
Joined
Jul 5, 2015
Messages
5
Hello, I am very new to VBA so this is probably a very easy question. I am trying to hide rows and hide columns using two scripts I have found. I have tested them both on separate tabs and they work. However, when I try to combine them on the same tab, I keep having problems. I don't know if it is a problem with how I am combining the code, or if you can't hide rows and columns at the same time.

Here is the code for hide columns:

Private Sub Worksheet_Calculate()


Dim myRange As Range


For Each myRange In Range("i20:ni20")


If myRange.Value = "show" Then


myRange.EntireColumn.Hidden = False

ElseIf myRange.Value = "no" Then


myRange.EntireColumn.Hidden = True

End If

Next myRange

End Sub


Here is the code for hide rows:

Private Sub Worksheet_Calculate()


Dim myRange As Range


For Each myRange In Range("d27:d60")


If myRange.Value = "show" Then


myRange.EntireRow.Hidden = False

ElseIf myRange.Value = "no" Then


myRange.EntireRow.Hidden = True

End If


Next myRange

End Sub


I would greatly appreciate any help on how to combine them!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try it with this modified version.
Code:
Private Sub Worksheet_Calculate()
 Dim myRange As Range
     For Each myRange In Range("i20:ni20")
         If LCase(myRange.Value) = "show" Then
             myRange.EntireColumn.Hidden = False
         ElseIf LCase(myRange.Value) = "no" Then
             myRange.EntireColumn.Hidden = True
         End If
     Next myRange
     For Each myRange In Range("d27:d60")
         If LCase(myRange.Value) = "show" Then
             myRange.EntireRow.Hidden = False
         ElseIf LCase(myRange.Value) = "no" Then
             myRange.EntireRow.Hidden = True
         End If
     Next myRange
 End Sub
 
Upvote 0
Tried it, but getting a "run time error '28' out of stack space" error. Any thoughts?

Thanks for your help!
 
Upvote 0
Re: VBA to hide rows and hide columns with formula

Changing the name of this post...

Please see original message
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,819
Members
449,469
Latest member
Kingwi11y

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