Results 1 to 11 of 11

Thread: Help! Macro code does not works on button!

  1. #1
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Help! Macro code does not works on button!

    Theres a range of cells from A:9 to D:9 and down until it have data.
    I must copy paste those cells with the data to the worksheet 2, and I have to sort them by their year in ascending order!

    Thing is, I made the code with recording a macro, but if I put it under the command button section it doesn't work!
    Unfortunately if I debug that command button code, and run it step by step the code works like a charm!!

    What am I doing wrong?
    Here's the code:

    Code:
    Private Sub CommandButton2_Click()
    Worksheets("Munka1").Range("A9:D10000").Cells.Copy
    Worksheets("Munka2").Range("A1:D10000").PasteSpecial  'I copy paste a lot of spaces because I don't know how to set it to only copy those cells wheres data!
    
    'This would be the macro I've recorded this part should work fine at least it does when in debug mode...
    Application.CutCopyMode = False
        ActiveWorkbook.Worksheets("Munka2").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Munka2").Sort.SortFields.Add Key:=ActiveCell. _
            Offset(0, 2).Range("A1:A9992"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Munka2").Sort
            .SetRange ActiveCell.Range("A1:D9992")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    
    End Sub

  2. #2
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    985

    Re: Help! Macro code does not works on button!

    What DOES it do when you try to run it via the command button?

  3. #3
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: Help! Macro code does not works on button!

    Only the first two lines
    Worksheets("Munka1").Range("A910000").Cells.Copy
    Worksheets("Munka2").Range("A110000").PasteSpecial

  4. #4
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    985

    Re: Help! Macro code does not works on button!

    Then it exits the sub? or gives you an error?

  5. #5
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: Help! Macro code does not works on button!

    no error just exits like its done it correctly but only the first two line runs.
    I've tried to put them into the bottom under the code which I made with macro record, but still the same happens. Its just skips it, if i click on the button but in debug mode it works!

  6. #6
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    985

    Re: Help! Macro code does not works on button!

    Can't explain that...can you zip and attach?

  7. #7
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: Help! Macro code does not works on button!

    Here's my whole file I hope you can get something out of it!

    test2.zip

  8. #8
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    985

    Re: Help! Macro code does not works on button!

    When I run it by clicking the button, it runs all the way through the code for me. I put an additional line of (meaningless) code at the end of your Sorts just to make sure (for example, I updated the caption property of the application).

  9. #9
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: Help! Macro code does not works on button!

    So its sorts the copied data for you? Its need to be sorted by the year value. Is it copied and then sorted in this way to the sheet2 (Munka2) for you?

  10. #10
    Junior Member
    Join Date
    Aug 12
    Posts
    23

    Re: Help! Macro code does not works on button!

    I've got it work!
    Just had to re record the macro and now its works as it should!

  11. #11
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    985

    Re: Help! Macro code does not works on button!

    Yes, it copies for me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •