Sort Routine Macro for Excel Worksheet

I have an excel that requires a sort routine.  I tried using the start/stop 
recording.  The code is shown below, however, I need this routine to work 
when you don't know how many rows are in the data.  My method has the 
filename hardcoded too.  I don't know if that matters.  This one subroutine 
of many.  Can someone show me how to make this routine more dynamic.  It 
will always sort by the same Column but it needs to work with all different 
Ranges of data (this example is hardcoded to sort data from row 2 through 
row 613.

Sub SortCategoryAssistance()
    Cells.Select
    ActiveWorkbook.Worksheets("NEEDWest Error Report").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("NEEDWest Error Report").Sort.SortFields.Add 
Key:= _
        Range("P2:P613"), SortOn:=xlSortOnValues, Order:=xlAscending, 
DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("NEEDWest Error Report").Sort
        .SetRange Range("A1:T613")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
End Sub
 

0
JCO
6/7/2010 5:31:30 PM
excel.programming 6508 articles. 2 followers. Follow

1 Replies
1453 Views

Similar Articles

[PageSpeed] 7

never mind.  I figured it out as shown below:
'
' SortCategoryAssistanceWithHeadersMacro
'
'
    Cells.Sort Key1:=Range("P2"), _
    Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
    MatchCase:=False, Orientation:=xlTopToBottom
    '
    Range("A17").Select


"JCO" <someone@somewhere.com> wrote in message 
news:OHGNSHoBLHA.5584@TK2MSFTNGP06.phx.gbl...
> I have an excel that requires a sort routine.  I tried using the 
> start/stop recording.  The code is shown below, however, I need this 
> routine to work when you don't know how many rows are in the data.  My 
> method has the filename hardcoded too.  I don't know if that matters. 
> This one subroutine of many.  Can someone show me how to make this routine 
> more dynamic.  It will always sort by the same Column but it needs to work 
> with all different Ranges of data (this example is hardcoded to sort data 
> from row 2 through row 613.
>
> Sub SortCategoryAssistance()
>    Cells.Select
>    ActiveWorkbook.Worksheets("NEEDWest Error 
> Report").Sort.SortFields.Clear
>    ActiveWorkbook.Worksheets("NEEDWest Error Report").Sort.SortFields.Add 
> Key:= _
>        Range("P2:P613"), SortOn:=xlSortOnValues, Order:=xlAscending, 
> DataOption _
>        :=xlSortNormal
>    With ActiveWorkbook.Worksheets("NEEDWest Error Report").Sort
>        .SetRange Range("A1:T613")
>        .Header = xlYes
>        .MatchCase = False
>        .Orientation = xlTopToBottom
>        .SortMethod = xlPinYin
>        .Apply
>    End With
>    Range("A1").Select
> End Sub
>
> 
0
JCO
6/7/2010 9:25:06 PM
Reply:

Similar Artilces:

Where's the Sort by New Message option?
Not sure when the layout of the newsgroups changed but I can't seem to find the ability to sort by new message in a thread instead of sorting by just the original thread date. That was hugely helpful as I could follow along and see what threads were still active. If you're gonna hang around in the newsgroups (any newsgroups), you may want to start using a newsreader. Microsoft Outlook Express can do it for you. Saved from a previous post: You may want to connect to the ms newsserver directly: If you have Outlook Express installed, try clicking on these links (or copy and paste i...

Cannot open Excel attachments
Hello, I have a user that cannot open Excel attachments from inside Outlook. He gets a security message and is given an option to save the attachments or cancel. He can open all other attachments without this warning appearing. I checked his registry and he has .xls files in the exception that is posted on the MS KB. He has Excel 2002 and Outlook 2002. His e-mail comes through an Exchange server. I do not have special security settings in place for this user. I cannot find anything else on Google or MS. Please help. Thank you. Aaron Aaron hu kiteb: > Hello, I have a user that cannot op...

Sorting
Hi, is it possible to use variable for sorting? Something like: declare @sort varchar(10) SET @sort='column1 DESC' SELECT ROW_NUMBER() OVER (ORDER BY @sort)AS rowId, * FROM table Is dynamic SQL only option? Thank you, Simon On 15/04/2010 11:28, simon wrote: > Hi, > > is it possible to use variable for sorting? > > Something like: > > declare @sort varchar(10) > SET @sort='column1 DESC' > > SELECT ROW_NUMBER() OVER (ORDER BY @sort)AS rowId, * FROM table > > Is dynamic SQL only option? You can use CASE to he...

protected worksheet won't unprotect
Hello-- I have an Excel spreadsheet out on our network which is protected; I need to make changes to it. When I go to make a change it brings up the message "The cell or chart you are trying to change is protected and therefore read- only." It says to remove protection via Tools->Protection- >Unprotect Sheet command. I go to that menu, but the Unprotect option is grayed out. Can anyone please tell me what I'm missing or doing wrong? Thanks! :) LB Hi is this workbook shared?. If yes then this procedure is not possible -- Regards Frank Kabel Frankfurt, Germany ...

How to sort account list?
I'm using MS Money 2004 Small Business. In the account list, my accounts are catagorized by and in this order: - bank accounts - credit accounts - investment accounts - loans and liabilities - asset accounts TOTAL BALANCE In the above view, I cannot easily determine totals for assets and liabilities. The following view does this. - bank accounts - investment accounts - asset accounts SUBTOTAL -loans and liabilities SUBTOTAL TOTAL BALANCE How do I get the above view? Thanks, Brett I don't use Small Business. But you can run a Net Worth report to get total assets and ...

Row names will not move with sort
Each row in my summary table in Sheet1 is hyperlinked to a corresponding table in Sheet2 with additional details. I need to move the rows in my summary table (Sheet1) and preserve the links to and from the details tables (Sheet2). I found that I can move rows in Sheet1 with cut & insert and the hyperlinks are maintained. I can travel from a row in summary table (Sheet1) to details table (Sheet2) and back, even after the corresponding row (Sheet1) was moved. But if I sort the rows in Sheet1 the links from Sheet2 back to corresponding rows in Sheet1 lead to presorted locations. Apparen...

looking for Excel VBA work
I am looking for Excel VBA work... I have been working with excel and vba for the past 3 years. I am very involved in it. I love helping people. Although I do require some money for my work, I do not charge as high as most programmer's do. If you need some help or some work done, please contact me at conspiringfate@earthlink.net Mike -- conspiringfate ------------------------------------------------------------------------ conspiringfate's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=31745 View this thread: http://www.excelforum.com/showthread.php?...

Search and replace macro needed
I need a macro that will replace a space with a non-breaking space <b>for the selected text only</b> Hi Greg, Why do you need a macro, when you can use a simple Find/Replace operation? Select the desired range then: Find: ' ' Replace: '^s' Click 'Replace All'. -- Cheers macropod [Microsoft MVP - Word] "GregNga" <GregNga@discussions.microsoft.com> wrote in message news:FBBBB44D-6B2D-4445-9DFA-50EBC8BF20FE@microsoft.com... >I need a macro that will replace a space with a non-breaking space <b>for the > se...

Excel 97 file size limit?
Hi, I use Excel 97(version created in 1996) on windows 98. I am not a computer expert and I need some perspective on how large I could eventually make a file before it reaches it's limit and will not function anymore. I use one file and only one workspace in that file. I will keep adding data in cells and formulas in some cells to calculate entered data. My total file size is 10,500KB so far. I have a bunch of numeric data entered from cell A1 to DZ1320. Then I have a number of formulas in cells from A1325 to P1460. My formulas are basicly simple, average "this row"...

Macro filter on query
I need to run 8 append and update queries using the same date range. I created a Form with the "Month" field as the filter and a command button to run the macro to run the Append & Update queries for records in the month range. I can't get the Apply Filter macro to work on the Query Date field. Any suggestions? Apply Filter action will not do what you seek. Your queries must have a WHERE clause that refers in some way to the textbox on the form where you've entered the Month value/data that you want to use in the queries. To assist you further, you need to post ...

opening excel attachment from e-mai
The last couple weeks I cannot open any excel attachments that are sent to me. It appears the path cannot be found. Word attachments open fine. Even old attachments that we used to open now do not open. Files that are saved in excel open fine. Any ideas on what got changed? two things to try with excel open, go to tools=>Options=>General tab and make sure Ignore Other Applications is not checked. with excel closed, go to Windows=>Run type in Excel.Exe /regserver and click OK. This will reregister the Excel settings in the registry. Hopefully one of them will fix your ...

sorts not sorting
OK- Thanks to everyones help I now i have a list i can work with... but there are a few problems.... the zips are all 5 digit and now i have to sort them... the problem is, THEY WONT SORT CORRECTLY!!!!!!! The numbers are not in order... some are but a lot are not... is there anyway to fix this???? i need to sort them by zip code but cant seem to get them to do it correctly.. any ideas??? ------------------------------------------------ ~~ Message posted from http://www.ExcelTip.com/ ~~ View and post usenet messages directly from http://www.ExcelForum.com/ You are going to be far better of...

Email Macro with Outlook Express
Once a week, I am in a position where I have to send between 80-160 emails to 80-160 different email addresses (the store locations in our company). I have an attachment that needs to be sent to each store, and each attachment is unique. However, I have all the attachments in one folder, and all of the attachments have filenames such as Store 101, Store 102, Store 103, etc... I have another file which lists all store numbers in Column A and their respective emails in Column B. Is there a way I can have a macro that does the following: 1. Creates an email with a Subject being the same as the ...

How do I set up my expense/income statement in excel?
...

Excel charts won't appear or preview. Help!
Hello, In Excell 2003 I cannot get my charts or graphs to appear or preview. I was able too at first. But I accidentally hit right click and then clear several times after having created template line chart for the first time. This subsequently one at a time removed different aspects of my chart until my chart was gone. And now i cannot get the charts to appear again. Not even a preview. Thank you in advance foe your help and advice. This problim has been solved. Thank you. ...

Excel Oddity
Dear all, Someone showed me an if statement, which was not working. The cells to which it referred were all formatted correctly (currency / accountancy) yet the statement returned an error. I deleted the cells in the logical test and re-entered them...I did not change the formatting, and this time it worked. To the best of my knowledge currency signs, commas etc were not entered manually but via cell formatting. Anyone met this before? Thanks Danny Perhaps you could post the formula in question. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www...

Receiving excel files
Whenever I receive an Excel file through email and try to open it, I receive the error message 'file format is not valid'. These are all .xls files. Any suggestions on how to fix this problem ??? Doug What versions of XL? (if you have versions before 97 and you are sent 97 or above, the file formats are different) -- HTH Nick Hodge Southampton, England nick_hodge@btinternet.com "Doug" <dwire90@hotmail.com> wrote in message news:01ae01c3903a$31837eb0$a301280a@phx.gbl... > Whenever I receive an Excel file through email and try to > open it, I receive the...

import- number- sort?
I imported this data into a spread sheet- I tried to format it to number, but when I sort it- I get : 37.08 4.33 4.79 4.8 42.56 5.94 Whats up with this? Thanks Brent Hi Brent, Install the TrimAll macro, select the column and invoke the macro from Alt+F8 http://www.mvps.org/dmcritchie/excel/join.htm#trimall Directions to install and use a macro at http://www.mvps.org/dmcritchie/excel/getstarted.htm#havemacro I expect that you have spaces or CHAR(128) "Required Blank" characters in you data you can check is =LEN(A1) --- HTH, David...

sorting macro #3
I need to sort on 8 cells in a row and on 1500+rows. I can do it one at a time but I know there must be an easier way. Can ANYONE HELP? C3:J3 I need these cells sorted and then down to C1532:J1532 I did create a macro to sort after I selected them but still one at a time. Does it have to be a macro? I can edit a macro but don't really know how to program them. Yes, it has to be a macro. The following should do it. This macro loops through all the entries in Column C, and in each row it sorts the values in Columns C:J. HTH Otto Sub SortRows() Dim rColC As Range Dim i As...

excel macro...PLEASE HELP!
Hi there, Here's what I would like to know how to execute. Assuming I have created a macro in place in a file. When I open that file, I want that macro to run...how do I do it? Thank your for reading and any suggestions are welcomed! Hi Jack Rename it Sub Auto_open() -- HTH. Best wishes Harald Followup to newsgroup only please. "Jack" <tangjack@yahoo.com> wrote in message news:00e601c38ceb$78b050d0$a301280a@phx.gbl... > Hi there, > > Here's what I would like to know how to execute. > > Assuming I have created a macro in place in a file. When...

Printing Form to Include All Options in Excel
Hello, I am extremely new to creating forms in Excel so bare with me here! Is there a way that I can print my form so that all options in my list box appear? I have a list box that is extremely long, so on the electronic version I want to have a scroll bar to save space. However, when I go to print, I would like all options to be visible. Can I do this w/o having to make the list box extremely long on the elctronic version so that all options are visible? Maybe you could print the list from a worksheet range? LostAndConfused wrote: > > Hello, > > I am extremely new to cr...

Mail merging with multiple worksheets
In Office 2000, when mail merging to data in Excel 2000 -- a workbook with several worksheets -- Word is not recognizing the worksheets, just the entire workbook and therefore is not merging properly. When I move target worksheet to file/workbook of its own, it works properly. Does anyone have any ideas on this? Please help, as I don't want to have to split 40-odd worksheets out into separate mail-merge sheets! Thanks, Jo Hi Try it by defining your merge tables as named ranges - it'll be the only possible way I can think of at moment. (Don't forget to save the workboo...

Display error message when openning excel file
When openning excel and word file, the error message is: Program error..... x0C... I reinstall Windows 2000 and MS office 2000,and change new Memory, No virus found.the error message is the same. How to resolve this problem? thanks. Maybe you could write down the error code and search the MS Knowledge base: http://support.microsoft.com/default.aspx Huayang wrote: > > When openning excel and word file, the error message is: > > Program error..... x0C... > > I reinstall Windows 2000 and MS office 2000,and change new Memory, No virus > found.the error message is th...

Excel date problems
Our company uses a report writer/formatter that creates Excel spreadsheets and then sends them as email attachments to specified users. The data being formatted is coming from a file shipped to the report software from an I5 environment. The data coming in is in the format mmddyyyy but some of the cells in the SS are being converted to ddmmyyyy. When this problem occurs it only happens for months 10, 11, and 12. These dates are being used in calculations so this is a big problem. We are using Excel 2003. Any ideas? ...

starting Word from an Excel macro
I found this piece of code by Bob Phillips that opens a Word document from within an Excel macro. Dim oWord as Object Dim oDoc as Object Set oWord = CreateObject("Word.Application") oWord.Visible = True Set oDoc = oWord.Documents.Open("C:\personal\bob\personal\CV - RP (short).doc") It works fine but it also closes the Excel file that contains the macro. Is there a way to prevent the Excel file from closing? -- russ Russ, There's nothing in that snippet that will close the Excel Application. I think you need to post all yo...