+ Reply to Thread
Results 1 to 11 of 11

Command Button Code

  1. #1
    Registered User
    Join Date
    02-02-2007
    Posts
    35

    Command Button Code

    I've written a little macro that open a workbook, copies the data and pastes it into this workbook.
    To work the macro, I added a command button and copied the code I wrote into the sub for the command button. Now it doesn't work. It does strange things.......so firstly, does the VBA in a command button work in a different way, and how can I get around it?

    My code is as follows (Sheet "Menu" has the command button, and Sheet1 is the sheet I want to copy the data into)

    Please Login or Register  to view this content.
    Any advice gratefully received. Tks Caroline
    Last edited by chedges; 02-12-2009 at 12:18 PM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Command Button Code

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    02-02-2007
    Posts
    35

    Re: Command Button Code

    Sorry! I've edited it

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Command Button Code

    You need to specify which workbook and/or sheet you are deleting from. Something like this
    Please Login or Register  to view this content.
    You don't need to select or activate workbooks & ranges, so your other code could be improved
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Command Button Code

    You are using the sheets code name rather than it's tab name.

    Please Login or Register  to view this content.
    The above code uses code name and then tab name to display sheets name.

    If you look at the Project window in the VBE you will see the code/tab name of the sheets in your workbook. You should see Sheet1 has the name (Menu)
    Cheers
    Andy
    www.andypope.info

  6. #6
    Registered User
    Join Date
    02-02-2007
    Posts
    35

    Re: Command Button Code

    Quote Originally Posted by Andy Pope View Post
    You are using the sheets code name rather than it's tab name.

    Please Login or Register  to view this content.
    The above code uses code name and then tab name to display sheets name.

    If you look at the Project window in the VBE you will see the code/tab name of the sheets in your workbook. You should see Sheet1 has the name (Menu)

    Ok that makes sense, except that I still cant get it to work. I've renamed the sheet in question to Data, so now have two sheets, Menu and Data.
    I select the Data sheet and then go from there to do all the bits, clearcontents etc, but everything it does happens in the Menu sheet. What am I doing wrong?

    (I'm not an expert on VBA and have only picked up a few bits here and there)

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Command Button Code

    We need to see your latest code. Even better would be an example workbook

  8. #8
    Registered User
    Join Date
    02-02-2007
    Posts
    35

    Re: Command Button Code

    Sure

    Please Login or Register  to view this content.
    It debugs at the Cell.Select stage even. But if I skip that stage the Cells.Delete deletes all the data from the Menu tab. I can see that it must have something to do with the fact that the macro is run from the Menu sheet, but I cant figure out how to amend the (such simple) code.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Command Button Code

    The button is on the menu sheet, because you don't specify the code to run a different sheet it clears that sheet

  10. #10
    Registered User
    Join Date
    02-02-2007
    Posts
    35

    Re: Command Button Code

    ok got it. How can I run the Data Sheet?

  11. #11
    Registered User
    Join Date
    02-02-2007
    Posts
    35

    Re: Command Button Code

    I've figured it out. The reason it because when you use a command button it does it in private sub. So i've change the command button to a normal button.
    Thanks for all your replies though

    Tks Caroline

+ 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