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
Re: Help! Macro code does not works on button!
What DOES it do when you try to run it via the command button?
Re: Help! Macro code does not works on button!
Only the first two lines
Worksheets("Munka1").Range("A9:D10000").Cells.Copy
Worksheets("Munka2").Range("A1:D10000").PasteSpecial
Re: Help! Macro code does not works on button!
Then it exits the sub? or gives you an error?
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!
Re: Help! Macro code does not works on button!
Can't explain that...can you zip and attach?
1 Attachment(s)
Re: Help! Macro code does not works on button!
Here's my whole file I hope you can get something out of it!
Attachment 90137
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).
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?
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!
Re: Help! Macro code does not works on button!