Results 1 to 6 of 6

macro fails to function correctly when called from button form control

Threaded View

  1. #1
    Registered User
    Join Date
    11-05-2008
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    67

    macro fails to function correctly when called from button form control

    Hi Folks,
    I have the following workbook sample that includes the code below, the code when run from the project window works a treat, however when it's run via the button on the welcome sheet, its results are varied.

    I have tried the old caveat of "run it on a different machine" where the results are still as described above.

    Any and all assistance is appreciated

    Iain

    Public Sub Hide_Weeks()
    'http://www.excelforum.com/excel-programming/785512-conditional-column-hide.html
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim Ans As Integer
    Dim ReadyAns As Integer
    Dim i As Integer
    
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    Set wb = ActiveWorkbook                                                 ' Define the active workbook
    ReadyAns = Sheets("Welcome").Range("B2").Value - 1                      ' Give last week as the default to start hiding sheets
    
    Ans = Val(InputBox("Enter last week to hide.", "WEEKS TO HIDE", ReadyAns, , 100))
    
        For Each ws In Worksheets
            Select Case ws.Name
            Case "Welcome", "CRM Data", "GP Data", "Properties"             ' These sheets we do not want any rows hidden
                With ws
                    ws.Columns("A:BK").EntireColumn.Hidden = False
                    Range("A1").Select
                End With
             Case Else
                With ws
                    ws.Columns("A:BB").EntireColumn.Hidden = False
                    For i = 1 To 53
                        If Cells(2, 2).Offset(0, i - 1) <= Ans Then
                            ws.Cells(2, 2).Offset(0, i - 1).EntireColumn.Hidden = True
                        ElseIf ws.Columns(i + 1).Hidden = True Then
                            Cells(2, 2).Offset(0, i - 1).EntireColumn.Hidden = False
                        End If
                    Next i
                End With
            End Select
        Next ws
    
    With Application
        .DisplayAlerts = True
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
    End Sub
    Attached Files Attached Files
    Last edited by sarails; 08-22-2011 at 01:39 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1