Macro not working correctly when called from a button

wtom0412

Board Regular
Joined
Jan 3, 2015
Messages
180
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a macro that is misbehaving when it is called by a button.

I have the following code...

Code:
Sub HideColumns()

Application.ScreenUpdating = False 


Range("I8:AL8").EntireColumn.Hidden = False ' Unhides all columns

' Search through the range and hide columns where cell in row 8 = zero
    For Each d In Range("I8:AL8").Cells
        If d.Value = "0" Then
            d.EntireColumn.Hidden = True

        End If
    Next d
    
Application.ScreenUpdating = True

End Sub

If I run it from within developer using F5 or step through it with F8, it works as it should and hides all of the columns it should (I.E., any column between I and AL that has a value of zero in row 8 is hidden).

When I call this macro from a button, it won't hide Column I, even though the value in I8 = zero.

For the life of me, I can't work out what's wrong!!

I have tried using a form control and an ActiveX button and neither will run the code correctly.

Any help would be greatly appreciated.

Cheers

WT
 
I cant say as I understand now why this isn't working for you. Id need to see the spreadsheet.
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You got it, there is a merged title cell immediately above the range. I removed the merge from Column I and it worked!!

The strange thing is that there is a separate merged range above AH:AL, and that works perfectly, it was just the merged cells from I:AG that did the bad thing. I am sure there is a perfectly good explanation.

Steve, thank you so much, I would not have figured that out by my self.
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,608
Members
449,174
Latest member
ExcelfromGermany

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