How do I use a macro button

G

Guest

I would like to add a macro button that will automatically re-sort a string
of numbers that are calculated at random. Column A are the numbers to which
I want to sort while Column B is @rand. Does anyone know how I can
accomplish this?
 
L

L. Howard Kittle

Try this macro assigned to a button from the Forms menu bar.

Sub SortIt()
Dim Lrow As Long
Dim Drng As Range
Lrow = Cells(Rows.Count, "A").End(xlUp).Row
Set Drng = Range("A1:B" & Lrow)
Drng.Sort Key1:=Range("B1"), Order1:=xlAscending
End Sub

HTH
Regards,
Howard
 
F

flummi

Or:

select tools-->macro-->record a macro,
when asked for the macro name type: sort
under "Store in" select "This workbook"
click ok
select the range to sort,
select data-->sort and perform the sort,
select tools-->macro-->stop recording
select view-->toolbars-->control toolbox
click the design button (green triangle)
click the command button icon
click somewhere on your sheet, hold and drag to the right and down to
create a button
right click in the new button and from the context menu select 'view
code'
between the lines "private sub" and "end sub" type the name of your
macro which was 'sort'
close the VBA window
on your sheet click the design button
click the new button to test it.

Mind that the sort range is "fixed" in the macro! So if you need to
expand the sort range be sure to reflect the change in the macro.

Hans
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top