ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help needed with Excel VBA sort (https://www.excelbanter.com/excel-programming/406730-help-needed-excel-vba-sort.html)

freshforlife

Help needed with Excel VBA sort
 
Hi Guys,

I am trying to sort a range of cells using VBA inside an excel
application using this code:

Worksheets(ProdGeog).Range("B" & gchk1 & ":M" & gchk2).Sort _
Key1:=Worksheets(ProdGeog).Range("J" & gchk1),
Order1:=xlDescending, _
Key2:=Worksheets(ProdGeog).Range("I" & gchk1),
Order2:=xlAscending, _
Key3:=Worksheets(ProdGeog).Range("B" & gchk1),
Order3:=xlAscending, _
Header:=xlNo

This sort is used to update certain graphs in the application.

If I run the whole application, it doesn't sort properly, however if I
debug the subroutine using F8 ( step-by-step iteration), it sorts
correctly.

Any pointers as to why it doesn't do it in the first place.

Thanks,
Ravi

joel

Help needed with Excel VBA sort
 
You have to careful when you step through code and then go back and look at
the worksheets. You sometimes change the cell or worksheet that is active
which can make the code work or not work.



The best way to determine where the problem is is to put a break point (F9)
after the sort instruction. Run the code from the beginning and then check
if it sorts correctly.

Then start from the beginning step through the code without looking at the
speadsheet until you get past the sort. Then see if the sort is correct.

If both sort are correct above, then something after the sort is giving you
the probem.

"freshforlife" wrote:

Hi Guys,

I am trying to sort a range of cells using VBA inside an excel
application using this code:

Worksheets(ProdGeog).Range("B" & gchk1 & ":M" & gchk2).Sort _
Key1:=Worksheets(ProdGeog).Range("J" & gchk1),
Order1:=xlDescending, _
Key2:=Worksheets(ProdGeog).Range("I" & gchk1),
Order2:=xlAscending, _
Key3:=Worksheets(ProdGeog).Range("B" & gchk1),
Order3:=xlAscending, _
Header:=xlNo

This sort is used to update certain graphs in the application.

If I run the whole application, it doesn't sort properly, however if I
debug the subroutine using F8 ( step-by-step iteration), it sorts
correctly.

Any pointers as to why it doesn't do it in the first place.

Thanks,
Ravi


freshforlife

Help needed with Excel VBA sort
 
Thanks Joel I will try your suggestion............


On Feb 26, 10:17 pm, Joel wrote:
You have to careful when you step through code and then go back and look at
the worksheets. You sometimes change the cell or worksheet that is active
which can make the code work or not work.

The best way to determine where the problem is is to put a break point (F9)
after the sort instruction. Run the code from the beginning and then check
if it sorts correctly.

Then start from the beginning step through the code without looking at the
speadsheet until you get past the sort. Then see if the sort is correct.

If both sort are correct above, then something after the sort is giving you
the probem.

"freshforlife" wrote:
Hi Guys,


I am trying to sort a range of cells using VBA inside an excel
application using this code:


Worksheets(ProdGeog).Range("B" & gchk1 & ":M" & gchk2).Sort _
Key1:=Worksheets(ProdGeog).Range("J" & gchk1),
Order1:=xlDescending, _
Key2:=Worksheets(ProdGeog).Range("I" & gchk1),
Order2:=xlAscending, _
Key3:=Worksheets(ProdGeog).Range("B" & gchk1),
Order3:=xlAscending, _
Header:=xlNo


This sort is used to update certain graphs in the application.


If I run the whole application, it doesn't sort properly, however if I
debug the subroutine using F8 ( step-by-step iteration), it sorts
correctly.


Any pointers as to why it doesn't do it in the first place.


Thanks,
Ravi




All times are GMT +1. The time now is 12:01 PM.

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