+ Reply to Thread
Results 1 to 5 of 5

Please help...Assigning Macros to command buttons

  1. #1
    Registered User
    Join Date
    11-11-2006
    Posts
    2

    Question Please help...Assigning Macros to command buttons

    I have tried for hours to effectively assign a macro to a command button on the first page of my 19 page workbook. Basically, I have recorded a Marco to delete the contents of specific cell over several spreadsheets. I have pasted this macro to the Code area for the command button in VB.

    Private Sub Macro1_Click()
    ActiveWindow.SmallScroll Down:=-25
    Range("B5:I5").Select
    Selection.ClearContents
    Range("A9").Select
    Selection.ClearContents
    Range("B10:H10").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=8
    Range("A15").Select
    Selection.ClearContents
    Range("B16:H16").Select
    Selection.ClearContents
    Range("B25:H25").Select
    Selection.ClearContents
    ActiveWindow.SmallScroll Down:=16
    Range("B31:H31").Select
    Selection.ClearContents
    Sheets("Last Year Sales").Select
    ActiveWindow.SmallScroll Down:=-13
    Range("A4").Select
    Selection.ClearContents
    Range("B5:H5").Select
    Selection.ClearContents
    Range("B12:H12").Select
    Selection.ClearContents
    Range("B19:B27").Select
    Selection.ClearContents
    Range("B29:B33").Select
    Selection.ClearContents
    End Sub

    Please help. if you need any other info about the workbook let me know

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    Quote Originally Posted by schad77
    I have pasted this macro to the Code area for the command button in VB.
    This isn't how I would do it. Not sure if it works or not. Just put the macro on a normal module in the book. Then...

    There are 2 types of command buttons. I suggest you use the one on the Forms Toolbar. Put a button on your sheet and it will ask you for a macro to assign. Just assign it to the macro from the step above.

    Matt
    Last edited by Mallycat; 11-11-2006 at 01:25 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    Plus, you can tidy up your code quite a lot. This code below does the same as your code.

    Private Sub Macro1_Click()
    Range("B5:I5").ClearContents
    Range("A9").ClearContents
    Range("B10:H10").ClearContents
    Range("A15").ClearContents
    Range("B16:H16").ClearContents
    Range("B25:H25").ClearContents
    Range("B31:H31").ClearContents
    Sheets("Last Year Sales").Select
    Range("A4").ClearContents
    Range("B5:H5").ClearContents
    Range("B12:H12").ClearContents
    Range("B19:B27").ClearContents
    Range("B29:B33").ClearContents
    End Sub

  4. #4
    Registered User
    Join Date
    09-11-2006
    Posts
    56
    Quote Originally Posted by Mallycat
    Plus, you can tidy up your code quite a lot. This code below does the same as your code.

    Private Sub Macro1_Click()
    Range("B5:I5").ClearContents
    Range("A9").ClearContents
    Range("B10:H10").ClearContents
    Range("A15").ClearContents
    Range("B16:H16").ClearContents
    Range("B25:H25").ClearContents
    Range("B31:H31").ClearContents
    Sheets("Last Year Sales").Select
    Range("A4").ClearContents
    Range("B5:H5").ClearContents
    Range("B12:H12").ClearContents
    Range("B19:B27").ClearContents
    Range("B29:B33").ClearContents
    End Sub
    I tried Mallycats code in a standard module, the only way I could assign this macro to a button was to remove the private part of the sub routine. after that I was able to use the customise toolbar command and create a new button on the toolbar that I could assign this macro to. I prefer this to putting a button on a particular sheet because you can get to the button from any sheet, not just the one sheet.

    Sub Macro1_Click()
    Range("B5:I5").ClearContents
    Range("A9").ClearContents
    Range("B10:H10").ClearContents
    Range("A15").ClearContents
    Range("B16:H16").ClearContents
    Range("B25:H25").ClearContents
    Range("B31:H31").ClearContents
    Sheets("Last Year Sales").Select
    Range("A4").ClearContents
    Range("B5:H5").ClearContents
    Range("B12:H12").ClearContents
    Range("B19:B27").ClearContents
    Range("B29:B33").ClearContents
    End Sub[/QUOTE]

    I hope this was helpfull

  5. #5
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    whooops, yes you need to remove the private bit

+ 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