+ Reply to Thread
Results 1 to 9 of 9

Help needed creating a "Next" macro to scroll through columns

Hybrid View

  1. #1
    Registered User
    Join Date
    02-07-2011
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    4

    Help needed creating a "Next" macro to scroll through columns

    Hi all,
    Let me start by saying a big Hello to all here! I'm new to the forum and to VBA. In fact Excel and VBA have been relatively unknown to me until late last week. I've been surprised at the ease to which I have taken up the programming side of VBA and creating Macro's.

    I've so far created Macro's for hiding any cell with a blank and all other columns around a certain column etc. E.g

    Selection.AutoFilter
        Selection.AutoFilter Field:=14, Criteria1:="<>" 'column O
        Columns("B:N").Select
        Selection.EntireColumn.Hidden = True
        Columns("O:O").Select
        Selection.EntireColumn.Hidden = False
    I've created macro button (shapes) within Excel for Columns B > O to do the same as above, but for each column. I'm now trying to create a "Next" buttom macro which will go through all my macro's one by one by clicking on a single macro "Next" button in turn. So for example, I click on the "Next" macro and it hides all except for column B, then I click on the Next macro again and it hides all except column C etc.

    My question to this is, Is it possible and if so, how?? Any help would be much appreciated!

    Thanks
    Spike
    Last edited by spikerules; 02-07-2011 at 10:42 AM. Reason: Removed unnecessary parts form title and added Code tags

  2. #2
    Registered User
    Join Date
    02-07-2011
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Creating a "Next" macro to scroll through columns

    Sorry I forgot to add that I tried to do the following: -

    Call Macro1
    Call Macro2
    Call Macro3 etc.

    but this just scrolled through all my macro's without stopping after each call... I'm guessing there is a line of code that can be put in to stop the macro at Macro 2 and then set it off again with another click of the mouse button?

    Thanks in advance for the replies
    Spike

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Help needed creating a "Next" macro to scroll through columns

    Hi Spike,
    Is this an example of what you are looking for?
    Option Explicit
    Sub HideColumnsExample()
        Dim ColCtr As Double 'Column Counter
        For ColCtr = 1 To 10
            Columns(ColCtr).Hidden = True
        Next ColCtr
    End Sub
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    02-07-2011
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Help needed creating a "Next" macro to scroll through columns

    Quote Originally Posted by MarvinP View Post
    Hi Spike,
    Is this an example of what you are looking for?
    Option Explicit
    Sub HideColumnsExample()
        Dim ColCtr As Double 'Column Counter
        For ColCtr = 1 To 10
            Columns(ColCtr).Hidden = True
        Next ColCtr
    End Sub
    Thanks Marvin for the reply,
    Unfortunately that is not what I am looking for. I have already created macro's for hiding column! I now need the macro's I have created to link together. For example: -

    Macro 1 hides all columns except B
    Macro 2 hides all columns except C etc

    So NOW I need to create a macro that will scroll through the macro's I have already created but stop as it gets to the next macro call. For example: -

    Call Macro 1
    Stop then
    Start Call Macro 2
    Stop then
    Start Call Macro 3
    Stop

    I also need this macro to be placed into a single shape or box in excel called the 'Forward' button, which I can assign by right clicking on. Therefore the "Forward" button can be clicked onece to get to Macro1, then clicked again to get to Macro 2 etc. Does this make sense? Sorry I find it hard to explain myself sometimes!

    Thanks again
    Spike
    Last edited by spikerules; 02-07-2011 at 12:08 PM.

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help needed creating a "Next" macro to scroll through columns

    Hi spikerules
    one way
    Option Explicit
    
    Dim m_MsgStoreValue As String
    Private Sub ValueStore()
    Select Case m_MsgStoreValue
    Case "One"
    m_MsgStoreValue = "Two"
    Case "Two"
    m_MsgStoreValue = "Three"
    Case "Three"
    m_MsgStoreValue = "One"
    Case Else
    m_MsgStoreValue = "Two"
    End Select
    End Sub
    
    Sub run()
    
    Select Case m_MsgStoreValue
    Case "One"
    Call one
    Case "Two"
    Call two
    Case "Three"
    Call three
    Case Else
    Call one
    End Select
    Call ValueStore
    End Sub
    Sub one()
    MsgBox "one"
    End Sub
    Sub two()
    MsgBox "two"
    End Sub
    Sub three()
    MsgBox "three"
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  6. #6
    Registered User
    Join Date
    02-07-2011
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Help needed creating a "Next" macro to scroll through columns

    Quote Originally Posted by pike View Post
    Hi spikerules
    one way
    Option Explicit
    
    Dim m_MsgStoreValue As String
    Private Sub ValueStore()
    Select Case m_MsgStoreValue
    Case "One"
    m_MsgStoreValue = "Two"
    Case "Two"
    m_MsgStoreValue = "Three"
    Case "Three"
    m_MsgStoreValue = "One"
    Case Else
    m_MsgStoreValue = "Two"
    End Select
    End Sub
    
    Sub run()
    
    Select Case m_MsgStoreValue
    Case "One"
    Call one
    Case "Two"
    Call two
    Case "Three"
    Call three
    Case Else
    Call one
    End Select
    Call ValueStore
    End Sub
    Sub one()
    MsgBox "one"
    End Sub
    Sub two()
    MsgBox "two"
    End Sub
    Sub three()
    MsgBox "three"
    End Sub
    Thanks Pike!
    I see how this will call a msg box in order of 1 to 3, but I'm a little confused on how to make the code run my macro's? I know this is asking a lot, but is there a way to explain how to run this code with my macros?

    My macro's are called NullB, NullC, NullD etc all the way to NullO... thanks!

  7. #7
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Question Re: Help needed creating a "Next" macro to scroll through columns

    Hello spikerules... any thoughts on my solution ?

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Help needed creating a "Next" macro to scroll through columns

    This solution utlizes the current state of sheet to determine what to hide next ... i.e. if no columns hidden then hide all but Column A , if Column is only Visible Then hide all but Column B... etc etc ... and if any errors occur then show all columns. A few of the nice things about this solution is that there is no extra coding for adding new columns to your "hide cycle " all you need to do is modify the variable "MaxColToHide = 26" . A second benifit is that this cycle will continue from where you left off the last time the book was used... even if it hasn't been used for months.

    Please try and let me know what you think ... cheers

    Public Sub HideColumnBySequence()
       On Error GoTo ShowAll
       
       Dim ColToShow As Long
       Dim MaxColToHide As Long
       
       ' CONFIGURE how far up to cycle the column Hide here !
       MaxColToHide = 26
       
       With Rows("1:1").Cells
       If .SpecialCells(xlCellTypeVisible).Count = .Cells.Count Then
         ColToShow = 1
       Else
          For Col = 1 To MaxColToHide
             ColToShow = all
             If Cells(1, Col).EntireColumn.Hidden = False Then
                ColToShow = Col + 1
                GoTo Hide
             End If
          Next Col
          
       End If
       End With
       
    Hide:
          
          Cells.EntireColumn.Hidden = True
          Cells(1, ColToShow).EntireColumn.Hidden = False
          Cells(1, ColToShow).EntireColumn.Select
          
       
       Exit Sub
    ShowAll:
       Cells.EntireColumn.Hidden = False
       
    End Sub
    Last edited by nimrod; 02-07-2011 at 10:13 PM.

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Help needed creating a "Next" macro to scroll through columns

    hi spikerules

    you replace the
    Call one
    with
    Call NullB
    have you tried nimrod solution

+ Reply to Thread

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