+ Reply to Thread
Results 1 to 15 of 15

Command button to navigate between excel sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    11-24-2014
    Location
    Dakar
    MS-Off Ver
    2010
    Posts
    21

    Command button to navigate between excel sheets

    Hi
    I am new to this forum so I do not yet know its etiquette so please bear with me if I am breaking any rule.
    Also I am very very new at VBAs and macros
    I am trying to create a VBA using a command button to open a specific sheet.
    Here is my code:
    Private Sub CommandButton1_Click()
    ThisWorkbook.Sheets("Sheet2").Select
    End Sub
    I keep on getting error messages such as "error 438", "error 9"...
    Can someone please help and tell me what I am doing wrong.
    Thanks in advance
    Last edited by Marieme; 11-26-2014 at 08:40 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,703

    Re: Command button to navigate between excel sheets

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-27-2011
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    86

    Re: Command button to navigate between excel sheets

    Hi,

    The code is correct. Check sheet name "Sheet2" exists - else you will get the error 9- out of subscript.

    Regards,
    lokicl

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,703

    Re: Command button to navigate between excel sheets

    Unable to replicate the issue. I copied your code to a new workbook. Made sure that there were at least two sheets in the book. Added a button. It works for me. Suggest you upload your workbook for analysis.

  5. #5
    Registered User
    Join Date
    11-24-2014
    Location
    Dakar
    MS-Off Ver
    2010
    Posts
    21

    Re: Command button to navigate between excel sheets

    The code seems to work after debugging on a test file. When I however use it on my working file I get a error 9 message as if the sheet did not exist. Yet it does.

  6. #6
    Registered User
    Join Date
    11-24-2014
    Location
    Dakar
    MS-Off Ver
    2010
    Posts
    21

    Re: Command button to navigate between excel sheets

    Alan,
    So here is my file as suggested (sorry am taking a while to respond but I am working on something else at the same time).
    Thank you both for your support.
    Attached Files Attached Files

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Command button to navigate between excel sheets

    Hello Marieme,

    According to your sample Workbook your Code should actually read:

    Private Sub CommandButton1_Click()
    ThisWorkbook.Sheets("Sheet4").Select
    End Sub
    Otherwise you could refer to the Actual Sheet as follow;

    Private Sub CommandButton1_Click()
    ThisWorkbook.Sheet1.Select ' which is named Sheet4
    End Sub
    Hope that helps.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  8. #8
    Registered User
    Join Date
    11-24-2014
    Location
    Dakar
    MS-Off Ver
    2010
    Posts
    21

    Re: Command button to navigate between excel sheets

    Hi Winon,
    Thank you for your suggestion. Tried that but it's not opening the sheet I am interested in which is the one that have the data.

  9. #9
    Registered User
    Join Date
    11-24-2014
    Location
    Dakar
    MS-Off Ver
    2010
    Posts
    21

    Re: Command button to navigate between excel sheets

    It's finally sorted!!! I retried the 2nd option Winon suggested and somehow it worked (must've made a mistake in the command the first time).
    Thank you very much you all

  10. #10
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Command button to navigate between excel sheets

    Why would you want to use code for that? You don't need code at all for this kind of functionality.

    Insert a shape into the sheet. Select the shape and create a hyperlink. Select a location in this workbook, select a sheet and click OK.

    See attached file. No code. Each sheet has buttons that link to each other sheet.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    10-30-2014
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2007 Pro
    Posts
    27

    Re: Command button to navigate between excel sheets

    teylyn - that's really interesting but how come the shapes don't have a hyperlink when you're on their own page (I know it wouldn't make sense if they did - I just wondered)?

  12. #12
    Registered User
    Join Date
    11-24-2014
    Location
    Dakar
    MS-Off Ver
    2010
    Posts
    21

    Re: Command button to navigate between excel sheets

    The file has actually more worksheets than what I sent. I first used hyperlinks but I could get only one sheet opened at the same time when I actually want to open 2 sheets at the same time.
    Unless there is a way I can use hyperlink to open 2 sheets for a given month. I'd be happy to use that.

  13. #13
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Command button to navigate between excel sheets

    Quote Originally Posted by Marieme View Post
    I actually want to open 2 sheets at the same time.
    How does that work? What do you expect to see?

  14. #14
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Command button to navigate between excel sheets

    Well, as you rightly state, it does not make sense to link to the sheet that you're already on. Therefore, the button that navigates to the "current" sheet is grayed out and does not have a link. That is the user experience we want, right?

    This does not happen automatically, but is the result of a little extra TLC required for a positive user experience.

    So, I created 5 shapes for Index and sheet 1 to sheet 4. I created these shapes on the Index sheet. I added links to the respective sheets on ALL buttons.

    Then I selected all the buttons with Ctrl-Click and then pasted them into the other four sheets.

    Then - magic!! - I edited each sheet and manually removed the link and changed the color of the button that referred to the current sheet.

    This is almost a reflex action for someone who does web design or other navigational user interfaces.

    It does not happen automatically, but the manual intervention required to create a seamless and logical user experience does not take long.

    It's all about how much you care about the user experience.

  15. #15
    Registered User
    Join Date
    10-30-2014
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2007 Pro
    Posts
    27

    Re: Command button to navigate between excel sheets

    Thanks for the explanation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Command Button to Print Grouped Sheets
    By KEMelton in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-30-2012, 01:21 PM
  2. Unhide Excel Sheets Using Command Button
    By David Brown in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-15-2010, 10:06 AM
  3. Excel Button to navigate?
    By MasterCoder84 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-05-2007, 03:29 PM
  4. Navigate command buttons
    By tqm1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-30-2007, 10:52 AM
  5. [SOLVED] command button:Can i use a command button to open diffrrent sheets
    By Hellboy in forum Excel General
    Replies: 0
    Last Post: 11-03-2005, 11:00 AM

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