+ Reply to Thread
Results 1 to 8 of 8

Code works in macro but not command button

  1. #1
    Registered User
    Join Date
    09-10-2009
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2013
    Posts
    47

    Code works in macro but not command button

    Hi there
    I've got the code below which works fine when I run it from a macro but when I put it behind a command button it only puts an 'A' into row N2 and no further. I wonder if anyone can explain why.

    Thanks, as always, for any help anyone can provide.

    datafiend

    Please Login or Register  to view this content.
    Last edited by datafiend; 12-10-2014 at 02:58 AM.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Code works in macro but not command button

    cannot tell by the code
    are you able to post the example?

    also you dont need the extra selects or the activesheet as you already selected it

    Please Login or Register  to view this content.
    Last edited by humdingaling; 12-09-2014 at 11:56 PM.
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    09-10-2009
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Code works in macro but not command button

    Thanks, humdingaling. I don't think I've posted an attachment before. The only instructions I can find for it are as follows:

    To attach a file to your post, you need to be using the main 'New Post' or 'New Thread' page and not 'Quick Reply'. To use the main 'New Post' page, click the 'Post Reply' button in the relevant thread.
    On this page, below the message box, you will find a button labelled 'Manage Attachments'. Clicking this button will open a new window for uploading attachments. You can upload an attachment either from your computer or from another URL by using the appropriate box on this page. Alternatively you can click the Attachment Icon to open this page.

    which seems to suggest I need a new post or new thread to do this. I can't find anything here to use. Is this correct?

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Code works in macro but not command button

    click on go advanced

    scroll down to the Manage Attachments button

    pop up will show up
    Click on add files then select files
    select your file and press open
    click on upload file and you will see attachment on bottom half o the screen
    then just press done

  5. #5
    Registered User
    Join Date
    09-10-2009
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Code works in macro but not command button

    Great. Thanks very much for those instructions.

    I've re-done the macro using your simplified code, as below. There are actually 2 sheets so I've included the code for both, but still the same result.

    The command button is on 'Sheet 3'.

    Thanks for your help.

    datafiend

    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Code works in macro but not command button

    two things were happening
    your lastrow wasnt calculating properly so i replaced it with something that did

    Please Login or Register  to view this content.
    this takes all rows into account instead of just "C"....if you just want C for some reason i would suggest adding activesheet in

    Please Login or Register  to view this content.
    secondly your autofill wasnt picking up the right sheet so i added activesheet into the file
    Attached Files Attached Files
    Last edited by humdingaling; 12-10-2014 at 02:12 AM.

  7. #7
    Registered User
    Join Date
    09-10-2009
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2013
    Posts
    47

    Re: Code works in macro but not command button

    Thank you very much for that. Always have trouble with last rows but when I see your code I can see how logical it is.

    Very much appreciated.

    datafiend

  8. #8
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Code works in macro but not command button

    your method of finding last row was fine....just when i debugged it using the button....the sheet switching after you selected

    instead of using all those activesheet, this is how i would have streamline the code

    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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. Code fails when ran within Userform, but works when ran from Macro button on worksheet.
    By Nitefox in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 6
    Last Post: 11-29-2013, 02:28 AM
  2. [SOLVED] code behind the command button that runs another macro
    By Vogelmann in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-10-2012, 06:04 AM
  3. [SOLVED] vba code, command bar, command bar button, one button works but not two
    By amazingg64 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 09:55 AM
  4. Replies: 3
    Last Post: 05-05-2006, 06:35 PM
  5. Replies: 1
    Last Post: 04-06-2005, 10:06 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