If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Sorting problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-07-04, 13:47
giggs11 giggs11 is offline
Registered User
 
Join Date: Feb 2004
Location: Jordan
Posts: 137
Question Sorting problem

Hi all,
I have alist of records containing the name and income number, as follows:

A Name B Income
----- -------
1 John 9000
2 Mary 8500
3 Smith 10000


I want to sort them in the same sheet but not in the same columns, i want them to be displayed in columns E and F, and when i change the value of field B2 to become 12000, i want it to automatically sorted and refreshed in columns E and F.

thanx
Reply With Quote
  #2 (permalink)  
Old 09-07-04, 17:55
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
If I understand correctly, you want Columns A and B sorted, then Columns E and F to reflect that new sorted order. You can use a worksheet Change macro to accomplish.

In E2, put =A2

and in F2, put = B2

Then, copy E and F down for a few. If you have Excel 2002 or later it will keep adding as you add to A and B.

Right-click on the worksheet tab, and select View Code.

The VBE window will appear. Then paste in this code.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim VRange As Range
    Set VRange = Range("B2:B" & Range("B65536").End(xlUp))
    If Not Intersect(Target, VRange) Is Nothing Then _
    Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
End Sub
Adjust to meet your needs.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #3 (permalink)  
Old 09-08-04, 11:20
giggs11 giggs11 is offline
Registered User
 
Join Date: Feb 2004
Location: Jordan
Posts: 137
Unhappy

Dear Shades,

I tried the solution you suggested, but i faced this problem
run-time error 1004
Application-defined or object-defined error

Is it possible to send me a simple excel sheet with this solution, cauase i'm a beginner in excel coding.

thanx
Reply With Quote
  #4 (permalink)  
Old 09-08-04, 16:38
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Okay, here is a sample. I put formulas in columns E and F. Be sure that you go to Tools > Options > Edit, and check the box "Extend List formats and Formulas".

Now when you add a name in column A (next one should be A14, then E14 will automatically fill in. Then when you add a number in B14, the macro will automatically sort Columns A:B, and Columns E:F.

To view the code, right click the worksheet name, and select View Code. The VBE window will appear.

-------------------------------------------
BTW, if this isn't quite what you need, post back and we can modify it based on your needs.
-------------------------------------------
Attached Files
File Type: zip WorksheetChgEvent.zip (9.1 KB, 42 views)
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums

Last edited by shades; 09-08-04 at 16:42.
Reply With Quote
  #5 (permalink)  
Old 09-13-04, 10:49
giggs11 giggs11 is offline
Registered User
 
Join Date: Feb 2004
Location: Jordan
Posts: 137
Exclamation

Dear Shades,
thanks for the assistance, and i'm sorry for not responding, i was working outside the office, i tried to modify a value in the excel sheet but i faced this error:
compile error:
variable not defined
(x1SortNoraml).

what shall i do?

Thanks in advance
Giggs
Reply With Quote
  #6 (permalink)  
Old 09-13-04, 10:58
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
what version of excel are you using i use 2000 to sort this change shades sort to

Code:
   Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False
Dave
Reply With Quote
  #7 (permalink)  
Old 09-13-04, 11:16
giggs11 giggs11 is offline
Registered User
 
Join Date: Feb 2004
Location: Jordan
Posts: 137
Thumbs up

Hi,

I tried Dave suggestion, and it's working just fine.

Thanx Dave
Thanx Shades

best regrads
Giggs
Reply With Quote
  #8 (permalink)  
Old 09-13-04, 15:17
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Glad that it is working.

Thanks, Dave. I use XL 2002, and don't have the opportunity to test on other versions.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On