|
-
Aug 2nd, 2012, 09:54 AM
#1
Thread Starter
Junior Member
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
-
Aug 2nd, 2012, 10:21 AM
#2
Re: Help! Macro code does not works on button!
What DOES it do when you try to run it via the command button?
-
Aug 2nd, 2012, 10:47 AM
#3
Thread Starter
Junior Member
Re: Help! Macro code does not works on button!
Only the first two lines
Worksheets("Munka1").Range("A9 10000").Cells.Copy
Worksheets("Munka2").Range("A1 10000").PasteSpecial
-
Aug 2nd, 2012, 11:15 AM
#4
Re: Help! Macro code does not works on button!
Then it exits the sub? or gives you an error?
-
Aug 2nd, 2012, 11:22 AM
#5
Thread Starter
Junior Member
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!
-
Aug 2nd, 2012, 11:27 AM
#6
Re: Help! Macro code does not works on button!
Can't explain that...can you zip and attach?
-
Aug 2nd, 2012, 11:36 AM
#7
Thread Starter
Junior Member
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
-
Aug 2nd, 2012, 11:47 AM
#8
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).
-
Aug 2nd, 2012, 11:53 AM
#9
Thread Starter
Junior Member
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?
-
Aug 2nd, 2012, 01:07 PM
#10
Thread Starter
Junior Member
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!
-
Aug 2nd, 2012, 01:09 PM
#11
Re: Help! Macro code does not works on button!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|