last day (15 days later) » 

3:33 PM
0
A: How do I change my column sort to sort the entire sheet not just a column?

paul bicaI made a few changes to fix the range issue, but the parsing of the strings worked well My changes: I moved the main procedure to a new module I changed it to sort all sheets in the current file Renamed most of your variables to be a bit more intuitive Reduce interaction with the sheet objects...

 
Let me be clear about what I am trying to do.
Let me be clear about what I am trying to do. This subroutine sorts one column on a worksheet. When it sorts that column it sorts only the column not the row. When the column is sorted it becomes out of sync with the rest of the row. Some of my sheets have 64 columns. This sub breaks down column A into three parts; 1. the alphabetic prefix. 2. the numeric center, and 3. the alphabetic suffix. It then sorts the items into the correct sort order, that is 20 sorts before 1000. I need to keep all of the rows data together. It does me no go to associate the sku with the wrong product.Thanks
This subroutine also sorts 20 before 20a. It does this by adding a space at the end of the line: memArr3Col(thisRow, 3) = sufStr & " " to correct the sort order. When the sort is done I will need to TRIM that space off. It really meets my needs with the exception that it only sorts one column not the entire row or up to 64 columns.
How does it sort. It loops through each character in the cell. In the beginning all alphabetic characters go to column B. When the next character to be tested IsNumeric(Mid(rngcell, intLoops + 1, 1)) is a number I set the strPrefix = "False", we no longer add anything to column B. Then we loop through the numbers adding them to column C. We look at the next character to be tested IF it is not a number or a "-" I set strSuffix = "True" and we no longer add anything to column C. The rest of the characters go to column D: alpSuffix = alpSuffix & Mid(rngcell, intLoops, 1).
When I write the suffix data to column D I add a space at the end to force the correct sort order where 20 comes before 20A. Then I set the initial conditions: strAlpha = vbNullString, lngNumeric = 0, alpSuffix = vbNullString, strPrefix = "True", strSuffix = "False" and move to the next row down, Cell(i, 1) and start breaking the contents down into prefix, numeric, and suffix. During the sort I respect the header in column A1 and leave it at the top of the row. I just need to figure out how to sort the entire row, from column A to column CG. Thanks for your help, I don't know how to fix it
 
When I updated the code I understood that you wanted to sort on three sections of the string "AB00017C": 1 "AB", 2 "00017", 3 whatever comes after the first 2 sections. What I'm not sure about is if you were deliberate about concatenating the bottle weight to the number. I'll updated my answer to illustrate how the parsing is performed and show you what I mean
 
The helper columns I add are of no consequence to the final result. They are needed for the sort only. I am thinking I need to select all the rows before I sort, with the helper columns in place. Then at the end I just delete those helper columns. I fully intend to pass in the sheet and row to be sorted once I figure out how to sort the entire sheet. I am even willing to invoke an SQL for the sort for speed it that works, but I have not explored that option yet. I am just unclear how to move all of the columns associated with column A when I sort. I can't get my mind around it just yet.
 
I updated my answer to include an image showing exactly how the strings are parsed and how the sorting is applied. Try to run the code in my answer to see if it solves the problem of sorting the entire rows on the sheet or not, and if it does I can explain any details you may need
 
SortAllSheets eror: Type Mismatch: on thisStr = memArr1Col(thisRow, 1). SortAllSheets sortCol:=2, row1:=2 gives the same: Type Mismatch. I am running your code on a sheet with 8 columns. What I can see happening on the sheet before the Mismatch is out of order.
 
3:33 PM
Can you tell me the value of "memArr1Col(thisRow, 1)" when the error occurs?
 
memArr1Col(thisRow, 1) Debugger says <Type mismatch>
thisRow= 1
 
thanks
when you click Debug, it takes you to that line
 
I've been working on my solution all morning without success.
 
I think we're close to fixing it, but I need to know tha data type that shows the error
when the code stops and takes you to that line, select the text "memArr1Col(thisRow, 1)", then right-click and select "Add Watch..."
in the lower window, what is the value of "memArr1Col(thisRow, 1)"?
 
I just lost my changes it will take a couple of minutes.
 
3:42 PM
ok
another way we can find that value is if you copy and paste the entire string of data from the first cell on the sheet (you said "thisRow = 1")
 
memArr1Col Value = Empty
 
good. let me test it
 
First three lines of data
sku post_title Barcode Sales Price Account UPC Prefix UPC Proposed UPC PostFIx
AB00017 Acrobird, Playland, 20" 757822000175 28.49 47900 ACBD
AB00018 Acrobird, Playland, 24" 757822000182 36.99 47900 ACBD
AB00021 Acrobird, Playland, 14" 757822000212 14.69 47900 ACBD
AB00022 Acrobird, Pyramid, 14" 757822000342 13.29 47900 ACBD
AB00042 Acrobird, Carosel, 24" 757822000427 47900 ACBD
Thanks for your help
 
no problem - I know how frustrating it can get
can you tell me the value of "sortCol" when it shows the error
 
sortCol = 1 Type Long
 
3:48 PM
is your data that you pasted above in Column 1 ? (A)
 
Now it is five rows of data in 8 columns
 
what sheet number are you working with?
 
Sheet 1 named "Master of Masters"
Last two columns of my sheet are empty, no data
 
In the VBA editor (top-left) is it called "Sheet1" ? (without the space)
 
No, Sheet3 (Master of Masters), sorry I keep importing the data from the source file.
 
3:52 PM
ok, that's fine as long as we know the proper name
I'll try a generic fix (I made a change), and I'd like you to replace the function with a new one
but I'm not sure if it will paste properly here...
here we go
.
 
The digit will change depending on the number of times I import data. I have a routine that deletes all sheets that end with a dash and a number.
 
I don't think it works
 
I dont see the code
 
I'll update my answer
 
Roger that
 
3:58 PM
I updated it the main function - can you replace it and try it again
 
I'm on it
Type mismatch again memArr1Col = Empty, sortCol = 1
If Len(memArr1Col(thisRow, 1)) > 0 Then
ThisRow = 1
 
your data in some of the sheets may contain NULL values (special values that Excel cannot handle properly)
 
Yes, its is possible.
I have tried IS NOT NULL which would not work for me instead <> Null does work. I use it in another sheet
 
excellent !
but you ned to try this instead
If not IsNull(value)
 
memArr1Col = Empty
memArr3Col = Empty
 
4:05 PM
replace this line:
 
lastCell = "asdgsdfgsdfg" I have no idea where that came from.
 
If Len(memArr1Col(thisRow, 1)) > 0 Then
 
with?
 
with this:
If Len(memArr1Col(thisRow, 1)) > 0 And Not IsNull(memArr1Col(thisRow, 1)) And Not IsEmpty(memArr1Col(thisRow, 1)) Then
 
Doing it
 
4:07 PM
k
(I like a good challenge )
 
Type Mismatch - -If Len(memArr1Col(thisRow, 1)) > 0 And Not IsNull(memArr1Col(thisRow, 1)) And Not IsEmpty(memArr1Col(thisRow, 1)) Then
 
I think we need to change the order:
 
I'm struggling to follow along
 
use this line:
If Not IsEmpty(memArr1Col(thisRow, 1)) And Not IsNull(memArr1Col(thisRow, 1)) And Len(memArr1Col(thisRow, 1)) > 0 Then
 
On it
 
4:09 PM
(I'll explain what we do)
 
Nope type mismatch
 
hm...
 
Order should not matter in a mismatch
It is data type vs data type.
 
let me try something - we need to get it working specifically for Sheet3
 
MemArr1Col = Empty, cell(1,1)
That cell always has text data in it.
Or activesheet
 
4:12 PM
can you add "ws" to the watch and scroll down the list of properties until you see the Name (what is the sheet name?)
 
Here we go Worksheet/Sheet26
It comes from me having to refresh my data every time we try the code
 
we need to work on Sheet3 (sheet 26 contains some bad data)
 
When we hit the type mismatch I end up with only one column of data, Column A
 
I'll hard-code the name Sheet3 and update the answer for you to try
 
do not sheet3 will work
I have updated my data again.
Right now it is 6
Sheet6 (Master of Masters)
 
4:17 PM
ok, just a sec...
I updated it to filter Sheet6
 
ok, I'm ready
 
did you replace the main function from the answer
 
Yes, no mismatch. Let me look at the data
 
Data looks good
 
4:23 PM
so it worked for Sheet6?
 
I highlighted a couple of individual rows, hey stayed together so I know we are working on complete rows.
Then I looked at the data with suffixes after the number and they too look good. Yes, it worked for sheet6
 
ok, than all rows on sheet6 are sorted as you wanted?
 
I think so.
 
ok
now, if you want
 
Wait
THe sheet names change. Sheet3 is gone
 
4:25 PM
sheet3, or sheet6?
 
I work with about 8 different sheets, importing fresh data everytime I run.
 
aha
 
TO work I need Active Sheet or "Master of Masters" so that I can pass in the name.
 
ok, just a sec
 
Next sheet might be "Discontinued" or "M-Records"
 
4:26 PM
I'll use the name "Master of Masters" (with that exact spelling)
 
It would be better to passing the sheet name.
SortAllSheets "Master of Masters
SortAllSheets "Discontinued"
SortAllSheets "BF_Discontinued"
 
I'll change the name of the main function to "SortSheet()"
ok
 
Yes, that would be better, Then latter I can passing the column to be sorted by name "sku" or "ID"
The sort key will not always be in column A, will that be a problem?
 
Ok, it's updated
 
Let me try it
 
4:36 PM
ok
you can change the sort column here:
            'set sort keys to helper columns
            Set sortKey1 = .Range(.Cells(row1, lCol + 1), .Cells(lRow, lCol + 1))   'pre Fix
            Set sortKey2 = .Range(.Cells(row1, lCol + 2), .Cells(lRow, lCol + 2))   'middle number (ID)
            Set sortKey3 = .Range(.Cells(row1, lCol + 3), .Cells(lRow, lCol + 3))   'post fix
 
Right on. THis works! I tried it on three different sheets with success
 
great !
 
I dont understand lCol is not the sort column is it
 
do you see how you can change the sort column?
 
I havent looked at it
 
4:41 PM
lCol is the last used column (with data)
we place the 3 helper columns after it
 
Where do you specify the sort column?
 
Set sortKey1 = .Range(.Cells(row1, lCol + 1), .Cells(lRow, lCol + 1)) 'pre Fix
Set sortKey2 = .Range(.Cells(row1, lCol + 2), .Cells(lRow, lCol + 2)) 'middle number (ID)
Set sortKey3 = .Range(.Cells(row1, lCol + 3), .Cells(lRow, lCol + 3)) 'post fix
 
Dooh as I smack my forehead
 
:))
 
I got this. Many many thanks for your help.
 
4:44 PM
let's say that you will want to sort by ID
Set sortKey1 = .Range(.Cells(row1, lCol + 1), .Cells(lRow, lCol + 2)) 'pre Fix
Set sortKey2 = .Range(.Cells(row1, lCol + 2), .Cells(lRow, lCol + 1)) 'middle number (ID)
Set sortKey3 = .Range(.Cells(row1, lCol + 3), .Cells(lRow, lCol + 3)) 'post fix
 
I was thinking about adding the columns at the end but did not go there.
 
sorry... you have to change 2 numbers on each line
 
Because they are at the end does that make the data to the right sort as a row?
 
Set sortKey1 = .Range(.Cells(row1, lCol + 2), .Cells(lRow, lCol + 2)) 'middle number (ID)
Set sortKey2 = .Range(.Cells(row1, lCol + 1), .Cells(lRow, lCol + 1)) 'pre Fix
Set sortKey3 = .Range(.Cells(row1, lCol + 3), .Cells(lRow, lCol + 3)) 'post fix
 
Better to pass them in.
 
4:45 PM
your initial problem was the sort range
 
I was working on that all morning again.
 
you were sorting only the first column, instead of sorting all columns
 
Without any luck
 
hope this helpt a bit
 
Thats what I saw but I was not able to figure it out
This is great thanks for the HUGH help!
I've already saved the code.
 
4:47 PM
this is the line that fixed the initial problem:
Set sortRng = .Range(.Cells(row1, START_COL), .Cells(lRow, lCol + 3))
 
Don't want to redo this
 
I can imagine
 
Pual, I really want to say thank you. You have saved a bit of the sanity I have left.
 
hahaha
I'm glad it worked out (and hopefully move on)
 
Thanks, If I can help you please let me know.
 
4:49 PM
thanks
good luck
 
Same to you, Bye!
 
bye!
 
:-)))
 
 
2 hours later…
6:40 PM
Hey Craig any way you can help with a VBA macro?
 

last day (15 days later) »