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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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