ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sort options by Excel VBA (https://www.excelbanter.com/excel-programming/307167-sort-options-excel-vba.html)

Joey11

Sort options by Excel VBA
 
The first time I'm here...

My question is about the sort function in VBA Excel.
I want my address-database yellow-white striped, but when I use a
standard sort function, the layout is sorted too.
That's why I'm looking for an extra option to escape the layout for
sorting.

Code:
--------------------
Rows("3:22").Sort Key1:=Range(first), Order1:=ord, Header:=xlNo, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom

--------------------

I hope there's somebody who can help my find this option!

Joey11 (Newbie)


---
Message posted from http://www.ExcelForum.com/


BrianB

Sort options by Excel VBA
 
Looks like you need additional code to remove the stripes and replac
after sorting

--
Message posted from http://www.ExcelForum.com


Sean[_11_]

Sort options by Excel VBA
 
What if you just copied the formatted cells to somewhere else and put
then back after sorting, like below.


Sean
"Just press the off switch, and go to sleep!"


'------------------------------------------------------
'------------------------------------------------------
Sub sort_keep_format()
Dim rangeSORT As Range, sheetTEMP As Worksheet


Set rangeSORT = Rows("3:22")

' create a worksheet to store a copy of the formats
Set sheetTEMP = rangeSORT.Worksheet.Parent.Sheets.Add
rangeSORT.Copy
sheetTEMP.Cells(1, 1).PasteSpecial Paste:=xlFormats

' set these parameters as per your wishes
rangeSORT.Sort Key1:=Range(first), Order1:=ord, Header:=xlNo,
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

' put back the original formats
sheetTEMP.UsedRange.Copy
rangeSORT.PasteSpecial Paste:=xlPasteFormats

' remove the temporary worksheet
Application.DisplayAlerts = False
sheetTEMP.Delete
Application.DisplayAlerts = True

' it's good to be tidy
Set sheetTEMP = Nothing
Set rangeSORT = Nothing
End Sub
'------------------------------------------------------
'------------------------------------------------------


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Joey11[_2_]

Sort options by Excel VBA
 
@BrianB:
That's exactly what I do now, and with my test-sheet of 19 rows is i
OK; but I thought there would be a faster method. ;)
@Sean:
I think that's more ingenious then my current solution and work
probably faster!! :)

Is there an option such as the "Paste:=xlValues" with PasteSpecial fo
Sorting? :confused

--
Message posted from http://www.ExcelForum.com


Sean[_11_]

Sort options by Excel VBA
 
What if you used conditional formatting to colour the cells. The
condition should be :-
Condition1: Formulae is: =(MOD(ROW(),2)=0)
Condition2: Formulae is: TRUE
if you set different pattern colours for each of these conditions they
will appear as stripes (you do not even need the second condition if you
just want to leave it plain).
I am never sure if conditional formatting is quicker as it means more
calculations, but it would mean no need for any VB code.


Sean
"Just press the off switch, and go to sleep!"

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Tom Ogilvy

Sort options by Excel VBA
 
Your question is confusing. You have formulas in the range you sort and you
want to permanenlty replace those cells with the values displayed and
destroy the formulas

'------------------------------------------------------
'------------------------------------------------------
Sub sort_keep_format()
Dim rangeSORT As Range, sheetTEMP As Worksheet


Set rangeSORT = Rows("3:22")

' Change formulas to values
rngSort.Formula = rngSort.Value

' create a worksheet to store a copy of the formats
Set sheetTEMP = rangeSORT.Worksheet.Parent.Sheets.Add
rangeSORT.Copy
sheetTEMP.Cells(1, 1).PasteSpecial Paste:=xlFormats

' set these parameters as per your wishes
rangeSORT.Sort Key1:=Range(first), Order1:=ord, Header:=xlNo,
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

' put back the original formats
sheetTEMP.UsedRange.Copy
rangeSORT.PasteSpecial Paste:=xlPasteFormats

' remove the temporary worksheet
Application.DisplayAlerts = False
sheetTEMP.Delete
Application.DisplayAlerts = True

' it's good to be tidy
Set sheetTEMP = Nothing
Set rangeSORT = Nothing
End Sub
'------------------------------------------------------
'------------------------------------------------------

--
Regards,
Tom Ogilvy

"Joey11 " wrote in message
...
@BrianB:
That's exactly what I do now, and with my test-sheet of 19 rows is it
OK; but I thought there would be a faster method. ;)
@Sean:
I think that's more ingenious then my current solution and works
probably faster!! :)

Is there an option such as the "Paste:=xlValues" with PasteSpecial for
Sorting? :confused:


---
Message posted from http://www.ExcelForum.com/




Joey11[_3_]

Sort options by Excel VBA
 
I'll try to be more precise:
I want to sort only values, no formulas;
problem: the layout is automaticly sorted with the values.

What I want:
A quick method to freeze the layout and sort the values in it

The current solution I have (based on Sean):
1) Copy
2) PasteSpecial Paste:=xlValues -on back-up sheet-
3) Sort
4) Copy
5) PasteSpecial Paste:=xlValues -on main sheet

--
Message posted from http://www.ExcelForum.com


Tom Ogilvy

Sort options by Excel VBA
 
Sean's code does a paste special formats on the backup sheet.

sheetTEMP.Cells(1, 1).PasteSpecial Paste:=xlFormats


If you don't want to sort formulas and they are in specific columns of your
data, you would probably have to not sort those columns - restrict you sort
to only sort the columns that don't contain formulas.


If you paste special values back on top of your data, it will overwrite your
formulas if they are include in the area pasted to. But doing a paste
special values won't restore your formatting - only a paste or paste special
formats (as Sean's code as posted does).

--
Regards,
Tom Ogilvy

"Joey11 " wrote in message
...
I'll try to be more precise:
I want to sort only values, no formulas;
problem: the layout is automaticly sorted with the values.

What I want:
A quick method to freeze the layout and sort the values in it

The current solution I have (based on Sean):
1) Copy
2) PasteSpecial Paste:=xlValues -on back-up sheet-
3) Sort
4) Copy
5) PasteSpecial Paste:=xlValues -on main sheet-


---
Message posted from http://www.ExcelForum.com/




Sean[_11_]

Sort options by Excel VBA
 
Maybe I missed the point here. I thought the problem was how do you sort
a range whilst retaining the initial formatting. As you cannot (as far
as I can see) "freeze" the BACKGROUND and PATTERNS cell formats, I
thought it best to either :-

1) Backup the formats, sort the range, put back the formats
2) Use conditional formatting if you formats can be defined
mathematically

I think Joey11 writing
2) PasteSpecial Paste:=xlValues -on back-up sheet-


was just a mis-typing


----
Sean
"Just press the off switch, and go to sleep!"

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Tom Ogilvy

Sort options by Excel VBA
 
perhaps, but he seems to know the difference:

Is there an option such as the "Paste:=xlValues" with PasteSpecial for
Sorting? :confused:


--
Regards,
Tom Ogilvy

"Sean" wrote in message
...
Maybe I missed the point here. I thought the problem was how do you sort
a range whilst retaining the initial formatting. As you cannot (as far
as I can see) "freeze" the BACKGROUND and PATTERNS cell formats, I
thought it best to either :-

1) Backup the formats, sort the range, put back the formats
2) Use conditional formatting if you formats can be defined
mathematically

I think Joey11 writing
2) PasteSpecial Paste:=xlValues -on back-up sheet-


was just a mis-typing


----
Sean
"Just press the off switch, and go to sleep!"

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!




Joey11[_4_]

Sort options by Excel VBA
 
:) It was no mistyping, they're two solutions in like that:
A) Copy the _values_ to a back-up sheet, sort them and put them back
(My solution in the last post)
B) Copy the _format_ to a back-up sheet, sort the data, put the format
back on it. (Seans solution)

I think my solution is faster :P , 'cause there's no need to sort the
format. ;)
There just may be a faster solution to do this at once with such as a
function

Code:
--------------------
Rows("3:22").Sort Sort:=XlValues
--------------------

Or something :(


---
Message posted from http://www.ExcelForum.com/


Joey11[_6_]

Sort options by Excel VBA
 
:) It was no mistyping, they're two solutions in like that:
A) Copy the _values_ to a back-up sheet, sort them and put them back
(My solution in the last post)
B) Copy the _format_ to a back-up sheet, sort the data, put the format
back on it. (Seans solution)

I think my solution is faster :P , 'cause there's no need to sort the
format. ;)

There just may be a faster solution to do this at once with such as a
function

Code:
--------------------
Rows("3:22").Sort Sort:=XlValues
--------------------

Or something :(


---
Message posted from http://www.ExcelForum.com/


Joey11[_5_]

Sort options by Excel VBA
 
:) It was no mistyping, they're two solutions in like that:
A) Copy the _values_ to a back-up sheet, sort them and put them bac
(My solution in the last post)
B) Copy the _format_ to a back-up sheet, sort the data, put the forma
back on it. (Seans solution)

I think my solution is faster :P , 'cause there's no need to sort th
format. ;)

There just may be a faster solution to do this at once with such as
function

Code
-------------------
Rows("3:22").Sort Sort:=XlValue
-------------------

Or something :( (doesn't exist

--
Message posted from http://www.ExcelForum.com


gably

Sort options by Excel VBA
 
Hi all,

Just wondering if you can use a variable instead of a value for settin
range rows.

eg, in the above code the line:
Set rangeSORT = Rows(2:33)

Can I use a variable for the second value. (33) If the user enters mor
data, then the rows to be sorted will increase. (obviously) I've got
variable called endRow as an integer, but can't use it in th
Rows(int:int). Any suggestions would be appreciated. Cheers.

G

--
Message posted from http://www.ExcelForum.com


Stan Scott

Sort options by Excel VBA
 
Use set RangeSORT = Rows(INDIRECT("2:"&xxx))

Stan

"gably " wrote in message
...
Hi all,

Just wondering if you can use a variable instead of a value for setting
range rows.

eg, in the above code the line:
Set rangeSORT = Rows(2:33)

Can I use a variable for the second value. (33) If the user enters more
data, then the rows to be sorted will increase. (obviously) I've got a
variable called endRow as an integer, but can't use it in the
Rows(int:int). Any suggestions would be appreciated. Cheers.

G.


---
Message posted from http://www.ExcelForum.com/




gably[_2_]

Sort options by Excel VBA
 
Stan,

Thanks, works fine now. You are the man. I haven't done any macro pro
for a while, forgot about the old & symbol. It's all coming back to m
now, slowly.

Gabe

--
Message posted from http://www.ExcelForum.com


Sean[_11_]

Sort options by Excel VBA
 
OK Joey,
Yes, your solution is faster if you do not mind losing your formulae (if
there are any), and do not forget the "Conditional Formatting" solution
as well.

I think the answer is that your idea method :-
Code:
--------------------
Rows("3:22").Sort Sort:=XlValues
--------------------
does not exist as far as I can see in Excel (I only have up to Excel XP
so cannot answer for Excel 2003), so you will have to choose from the
existing three solutions

Good luck


----
Sean
"Just press the off switch, and go to sleep!"

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


All times are GMT +1. The time now is 05:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com