I have a worksheet that I run a list of macros from, and a sheet called Orders.xls that is edited by the worksheet. It has 4 columns, and a maximum of 3000 rows. I recorded a macro to sort the invoices by column D, by product number. This macro does not error, but does not sort any data. An example of what it sorts is below, also with the macro I am trying. Data needing sorted:
PRODUCT PRODUCT PRODUCT PRODUCT
CODE NAME COUNT NUMBER
----- ----- ----- -----
PHT PHOTOS 129 8
PRN PRINTER 1 139
PHN PHONE 3 23 Result of sort:
PRODUCT PRODUCT PRODUCT PRODUCT
CODE NAME COUNT NUMBER
----- ----- ----- -----
PHT PHOTOS 129 8
PHN PHONE 3 23
PRN PRINTER 1 139 - Windows("ORDERS.xls").Activate
-
Sheets("ORDERS").Range("A4:D3000").Select
-
Cells.Sort Key1:=Range("D4"), Order1:=xlAscending, Header:=xlGuess, _
-
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
-
DataOption1:=xlSortNormal
If anyone is able to assist, it would be greatly appreciated. Thank you!
10 2542
I'm not really familiar with using the Sort from VBA. However, I have a sneaking suspicion that your problem may be related to your use of Key1:=Range("D4"). I wouldn't claim to know what it should be, though. If someone else doesn't jump in, I'll look into this tomorrow morning, when I have the tools available.
I've tried changing the Range("D4") to about everything I could imagine, from the range of the cells to be edited, to the cell which is the header row, with no avail. I appreciate you taking the time to assist with this!
I've tried changing the Range("D4") to about everything I could imagine, from the range of the cells to be edited, to the cell which is the header row, with no avail. I appreciate you taking the time to assist with this!
Usually the easiest way to get the parameters right (or at least get some ideas on what to do with them) is to record a macro, and do the action (in this case the sort) yourself. Then the macro will contain the code to do what you want.
Having just tried this myself, I see that your Key seems fine (sorry about that). However, just to see whether it makes any difference, could you try this modified version? (I'm assuming for the moment that you're already in the ORDERS sheet) - Range("A4:D3000").Sort Key1:=Range("D4"), Order1:=xlAscending, Header:= _
-
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
-
DataOption1:=xlSortNormal
As you can see, I've only changed the way the cells are selected.
Ok, two points.
Firstly, I've been playing with various aspects of your Sort command, and your syntax appears to work perfectly well. Forget what I said about the range selection.
Secondly, I've just re-read the original question, and as far as I can see, your sort did work. Why do you think it doesn't?
The only possible problem I can see is that perhaps you wanted an alpha rather than a numeric sort, in which case you need to set the format of the cells to Text.
In other words, it appears that your sequence 8, 139, 23 was correctly sorted into 8, 23, 139 (ascending numbers). If you want to treat them as text, then I guess you'd expect the sequence to become 139, 23, 8.
Forgive me for the wording on my original question, when I quoted Result of Sort, I meant that this was the result I was looking for. When I run the sort, it actually does nothing. The data looks exactly the way it did before I ran it. I'm going to attempt your modified version, and I will let you know of the result as soon as possible. Thanks again!
Forgive me for the wording on my original question, when I quoted Result of Sort, I meant that this was the result I was looking for. When I run the sort, it actually does nothing. The data looks exactly the way it did before I ran it. I'm going to attempt your modified version, and I will let you know of the result as soon as possible. Thanks again!
Oh, ok.
I don't think it will help, though. I played around and the different ways of selecting the cells and so on didn't make any difference.
Um... is the sheet protected? Perhaps sorting is not allowed at that point.
Oh, a quick tip. If you want columns to line up in a post (like those in your original post that started this thread), put [C O D E] tags around the relevant part of the text, so it uses a non-proportional font. For example... Data needing sorted: - PRODUCT PRODUCT PRODUCT PRODUCT
-
CODE NAME COUNT NUMBER
-
----- ----- ----- -----
-
PHT PHOTOS 129 8
-
PRN PRINTER 1 139
-
PHN PHONE 3 23
Desired Result of sort: - PRODUCT PRODUCT PRODUCT PRODUCT
-
CODE NAME COUNT NUMBER
-
----- ----- ----- -----
-
PHT PHOTOS 129 8
-
PHN PHONE 3 23
-
PRN PRINTER 1 139
Hi-
I usually do my .xl VBA sorts like this: -
With Sheets("YourSheetName")
-
Set r1 = .Range("A4:D3000")
-
r1.Select
-
Selection.Sort Key1:=.Cells(4,4), Order1:=xlAscending, Header:=xlNo, _
-
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
-
DataOption1:=xlSortNormal
-
End With
-
This specifies a few things the XL macro recorder doesn't, and specifies them a bit differently (as I recall) and seems to be more robust.
Leo
Killer, thanks for the input on the [ C O D E ] tags, and for assisting with my sort macro. I tried the modified version, and it did not work. However, I tried solomonp's version, and that did work. Seems the key my recorder was using was not correct. Thank you both so much for your assistance with this, I greatly appreciate it!
Glad that worked out.
I guess we've both learned something from this experience.
if you're still checking, this is the REAL reason i believe your sort isn't working they way you intend. the fact that your "numbers" are left-justified in the cells leads me to believe that they're not formatted as numbers at all, but are actually text. so as far as excel is concerned, the sort works because "words" starting with 3 are greater than "words" starting with 2, etc. what you want to do is edit your code to look like this
Range("A4:D3000").Sort Key1:=Range("D4"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers
(all i did was change DataOption1 from xlSortNormal to xlSortTextAsNumbers). and i've had bad luck with setting header to xlGuess. probably safer to use xlNo if you know there's no header there. hope that helped!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Chris Reedy |
last post by:
For everyone -
Apologies for the length of this message. If you don't want to look
at the long example, you can skip to the end of the message.
And for the Python gurus among you, if you can...
|
by: Stuart |
last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the
degenerate case in a common situation). I have a replacement.
I would like to globally do "using namespace std; except use my...
|
by: Peter Stalder |
last post by:
I want to alter a menu bar based on user's actions. My menu bar is
established using macros. Is there any way to edit the menu bar macro
using Visual Basic, so that the menu name will change. I...
|
by: Aravind |
last post by:
Hi folks.
My form, frmHistory, has a combo box, cboName, that filters frmHistory
by member's name (using a macro). frmHistory also has a few labels
that I use for sorting the form (using VBA...
|
by: Jamal |
last post by:
I am working on binary files of struct ACTIONS
I have a recursive qsort/mergesort hybrid that
1) i'm not a 100% sure works correctly
2) would like to convert to iteration
Any comments or...
|
by: pozz |
last post by:
Hi all,
I have the below #defines
#define NUMBER1 30
#define NUMBER2 50
#define SUM (NUMBER1+NUMBER2)
#define STRING1 "Byte: \x30"
#define STRING2 "Byte: \x50"...
|
by: Ross A. Finlayson |
last post by:
Hi,
I hope you can help me understand the varargs facility.
Say I am programming in ISO C including stdarg.h and I declare a
function as so:
void log_printf(const char* logfilename, const...
|
by: =?Utf-8?B?TGV0emRvXzF0?= |
last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines,
remove some lines based upon a condition in a certain column. Then copy this
data into a new spreadsheet and sort the ...
|
by: brokkoli88 |
last post by:
Hi!
I am having sort of a problem with a school assingnment.
I need to create a macro that checks if the content in a cell is a number. If the value is a percentage, I need the macro to check if...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |