+ Reply to Thread
Results 1 to 42 of 42

Adding macro buttons

  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Adding macro buttons

    I want to add a button to each row in column Z. I want 1000 rows to be covered. I have 1000 macros wrote. I have copied/pasted the macro buttons which are identical so there are 1000 buttons now in each row in the column Z. But how can I quickly make each button automatically assign to a specific macro without having to individually assign a macro to each button? The macros are titled Macro1 Macro2 Macro3 and so on.

  2. #2
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    I suppose a different way of having it would be to have some sort of loop code that would say that if in column Z row 7 was clicked then macro7 would run and if column Z row 101 was clicked then macro101 would run. Is this possible?

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    This will do what you want but I've got to ask, what do these 1000 macros do?

    Do they each do something completely different or are they basically the same/

    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Yeah pretty much the same. All that changes is one number which goes up by 1 each time.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    You mentioned using a loop code, presumably instead of the butttons, but what exactly do you mean?

    What would you be looping?

    Instead of the buttons or a loop how about using the worksheets before double-click event?

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Is there any way to have all the buttons say the same thing rather than Button1, Button2 Button3 etc?

  7. #7
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Yeah that might work as well. How would I then have it that if row 50 column Z was clicked it would call Macro50?

  8. #8
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Here's the macro below which I have to make multiple thousands of. The 0 highlighted in red is the only thing that changes with each macro. So for the macro Macro1 which I have below the value is 0. For macro Macro2 the value is 1, etc etc. Is there any easy way of making many thousands of these without manually copying pasting and changing the values by myself?
    Please Login or Register  to view this content.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    You don't need to repeat this code thousands of times.

    This macro could be assigned to all the buttons.
    Please Login or Register  to view this content.
    You could change the code similarly to work with double click.

  10. #10
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Would I use that in conjunction with your worksheet change event macro above?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    What change event macro?

  12. #12
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Quote Originally Posted by Norie View Post
    You mentioned using a loop code, presumably instead of the butttons, but what exactly do you mean?

    What would you be looping?

    Instead of the buttons or a loop how about using the worksheets before double-click event?

    Please Login or Register  to view this content.
    This one?

    I tried just using macro1 but it doesn't work. Gives me an application defined or object defined error and highlights "Set rng = .Resize(.Rows.Count - 1).Offset(lngOffset)"

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    That isn't for the Change event it's for the BeforeDoubleClick event.

    Can you attach a sample workbook where you'll be using this code?

  14. #14
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Here you go! Thanks!

    sample.xlsm

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    Slight error in the code.

    Try this.

    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    That is awesome! Thank you so much for saving me so much time! What does this mean though, as it seems to make the whole thing work!


    lngOffset = Val(Mid(Application.Caller, 7)) - 1

    I'm going to create tonnes of buttons now! Any way to make all the buttons say the word Data, instead of saying Button1 BUtton 2 etc??

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    Application.Caller returns the name of the button that was clicked which will be something like Button X, where X is 1,2,3,4...1000.

    We use Mid to extract the 1,2,3,4... part and to get the correct offset we subtract 1.

    If you want all the buttons to have the caption Data add this in the loop that creates them.
    Please Login or Register  to view this content.

  18. #18
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    I just deleted column Q and tried re adding the buttons again but now it starts from 301. So in row 1 its button 301 and so on. How can I get it to start at Button 1 again like the first time?

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    It might be difficult and if you are going to be adding and deleting buttons quite a lot you'd be best to name them yourself.

    Add this to the loop as well.
    Please Login or Register  to view this content.

  20. #20
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    I tried that but to no avail. It keeps continuing on. So if the last button number is 500 and I delete all the buttons and set I = 1 To 20 it will populate row 1 to 20 but the button number will start at 501.

  21. #21
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    That's not happening when I run the code, and that's after deleting the 1000 buttons quite a few times.

    Where exactly did you add btn.Name?

  22. #22
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Directly blow btn.on action

  23. #23
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    Can you post the exact code you have now?

    By the way, you could avoid all this faffing about if you used the Double Click event.

  24. #24
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Please Login or Register  to view this content.

  25. #25
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    What would I need to do for the double click event to make this work?

  26. #26
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    To get what to work?

  27. #27
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    To click on a cell in column Q and for just that row to be moved to the other sheet. I think ideally, I'd prefer the button method though if you can see any reason why it's not naming the buttons how I want?

  28. #28
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    The buttons aren't being called Button 301 etc, that's their caption.

    I posted code to change the captions of all the buttons to Data earlier, see post #17.

  29. #29
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    But button 301 in row 2 when clicked does nothing when it should remove row2

  30. #30
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    Can you arrach an updated workbook?
    Last edited by Norie; 11-21-2012 at 07:57 PM.

  31. #31
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    I'm also sure the code can be edited to make it more efficient. If I hit the button on row 11 I want that row range A to M cut and moved to the bottom of the other sheet and where the data is cut from I want the remaining data to be moved up.

  32. #32
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    I know I used autofilter in the code I sent but I'm sure for what I need its not needed ie. theres a better , simpler way? I'll post an updated sample in a while. Thanks for your continued help Norie. Really really great help you have been.

  33. #33
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    If that's all it's doing why are you filtering?

    You know the row you want to cut and you know where you want to paste to.
    Please Login or Register  to view this content.

  34. #34
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    And presumably in there I'll have instead of Offset(1) I'll have Offset(lngoffset) where lngOffset = Val(Mid(Application.Caller, 7)) - 1???
    Last edited by ScabbyDog; 11-22-2012 at 04:20 AM.

  35. #35
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    No, that code is for the DoubleClick event.

    It can be adapted for the buttons.

    The Offset doesn't change because it's for the destination.

  36. #36
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Ok, I think I'm almost there! Thankfully! I have the Buttons all inserted now and they all point to the below macro. How would I change the macro so that it incorporates the code above which doesn't use autofilter?

    So when I click on say Button 50 it just removes button 50 and puts it to the bottom of the other sheet instead of auto filtering.

    Please Login or Register  to view this content.

  37. #37
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Removes the button. Why?

  38. #38
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Oops. I meant to say how to I incorporate your code above that doesn't use auto filter? I have all the buttons listed down the page. So for eg, when I click button 50 I just want it to remove that row range A:M, and put it at the bottom of the other sheet.

  39. #39
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    This is how it would work.
    Please Login or Register  to view this content.

  40. #40
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    Brilliant Norie! Works great. The only bad thing is that since there are lile 1500 buttons whenever I hit one of the buttons it takes about 3 seconds to remove the row. Any way of quickening it up that you know of?

  41. #41
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Adding macro buttons

    Try turning off screen updating.
    Please Login or Register  to view this content.

  42. #42
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Adding macro buttons

    That makes it slower for me Norie! Strange?

+ 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