Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Solve Using VBA Routine

I have a table: A1:C11 as follows (Cells C2:C11 are Blank), but Macro
should fill in answer (given to the right):

Name Amt Order Answer to Order (=Rank ascending by Name)
Jon 13 ? 1
Jon 23 ? 3
Jon 15 ? 2
Peter 27 ? 2
Peter 10 ? 1
Alex 23 ? 2
Alex 34 ? 3
Alex 18 ? 1
Alex 36 ? 4
Mary 15 ? 1

The VBA routine should fill in C2:C11 with the above #'s
Can someone help me along?
TIA,



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Solve Using VBA Routine

Jim,

Determine the range
Find or insert a blank column adjacent to the range.
AutoFill the blank column with sequential numbers.
Sort the range (by Name then Amt). Include the sequential number column.
Loop thru the names, incrementing a counter in Column C.
When the name changes, start the counter over again.
Re-sort the range using the column with the autofilled numbers as the index.

Regards,
Jim Cone
San Francisco, USA


"Jim May" wrote in message news:HqRTd.18310$7z6.11048@lakeread04...
I have a table: A1:C11 as follows (Cells C2:C11 are Blank), but Macro
should fill in answer (given to the right):

Name Amt Order Answer to Order (=Rank ascending by Name)
Jon 13 ? 1
Jon 23 ? 3
Jon 15 ? 2
Peter 27 ? 2
Peter 10 ? 1
Alex 23 ? 2
Alex 34 ? 3
Alex 18 ? 1
Alex 36 ? 4
Mary 15 ? 1

The VBA routine should fill in C2:C11 with the above #'s
Can someone help me along?
TIA,


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Solve Using VBA Routine

Thanks Jim;
I'll give it a go -- in the AM..
Jim

"Jim Cone" wrote in message
...
Jim,

Determine the range
Find or insert a blank column adjacent to the range.
AutoFill the blank column with sequential numbers.
Sort the range (by Name then Amt). Include the sequential number column.
Loop thru the names, incrementing a counter in Column C.
When the name changes, start the counter over again.
Re-sort the range using the column with the autofilled numbers as the

index.

Regards,
Jim Cone
San Francisco, USA


"Jim May" wrote in message

news:HqRTd.18310$7z6.11048@lakeread04...
I have a table: A1:C11 as follows (Cells C2:C11 are Blank), but Macro
should fill in answer (given to the right):

Name Amt Order Answer to Order (=Rank ascending by Name)
Jon 13 ? 1
Jon 23 ? 3
Jon 15 ? 2
Peter 27 ? 2
Peter 10 ? 1
Alex 23 ? 2
Alex 34 ? 3
Alex 18 ? 1
Alex 36 ? 4
Mary 15 ? 1

The VBA routine should fill in C2:C11 with the above #'s
Can someone help me along?
TIA,




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Solve Using VBA Routine

Jim:
I got it !!! << This is my first (to me,,) real programming exercise.
<<vbg
I'm really excited. I'd never got it without your breaking the project
down into it's pieces - which I can't seem to do (my biggest problem, I
think..)
Again thanks,
Here is my go at it (probably could be improved on) - Do you see any obvious
improvement(s) that could or should be made?
Jim May

Sub Foo()
Range("D1").Value = "AutoNo"
ActiveSheet.UsedRange.Offset(0, 3).Resize(, 1).Select
Mrows = Selection.Rows.Count
For i = 2 To Mrows
Cells(i, 4).Value = i
Next i
ActiveSheet.UsedRange.Sort Key1:=Range("A1"), _
Order1:=xlAscending, Key2:=Range("B1"), _
Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Range("C2:C" & Mrows).Select
p = 0
For n = 2 To Mrows
If Cells(n, 1).Value = Cells(n + 1, 1) Then
p = p + 1
Cells(n, 3).Value = p
Else
Cells(n, 3).Value = p + 1
p = 0
End If
Next n
Range("A1").Select
ActiveSheet.UsedRange.Sort Key1:=Range("D1"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Columns("D:D").Delete
Range("A1").Select
End Sub




"Jim Cone" wrote in message
...
Jim,

Determine the range
Find or insert a blank column adjacent to the range.
AutoFill the blank column with sequential numbers.
Sort the range (by Name then Amt). Include the sequential number column.
Loop thru the names, incrementing a counter in Column C.
When the name changes, start the counter over again.
Re-sort the range using the column with the autofilled numbers as the

index.

Regards,
Jim Cone
San Francisco, USA


"Jim May" wrote in message

news:HqRTd.18310$7z6.11048@lakeread04...
I have a table: A1:C11 as follows (Cells C2:C11 are Blank), but Macro
should fill in answer (given to the right):

Name Amt Order Answer to Order (=Rank ascending by Name)
Jon 13 ? 1
Jon 23 ? 3
Jon 15 ? 2
Peter 27 ? 2
Peter 10 ? 1
Alex 23 ? 2
Alex 34 ? 3
Alex 18 ? 1
Alex 36 ? 4
Mary 15 ? 1

The VBA routine should fill in C2:C11 with the above #'s
Can someone help me along?
TIA,




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Solve Using VBA Routine

Jim,

Only two things...
The variables have not been declared and
most of the "Select" statements are not needed.

I particularly like the way you incremented the Column C values.
For what it's worth, do not assume a "UsedRange" will always
start in row one. Of course, in this case it does.
'Slightly revised code follows...
'------------------------------------
Sub Foo()
Dim Mrows As Long
Dim i As Long
Dim n As Long
Dim p As Long

Range("D1").Value = "AutoNo"
' ActiveSheet.UsedRange.Offset(0, 3).Resize(, 1).Select
' Mrows = Selection.Rows.Count
Mrows = ActiveSheet.UsedRange.Rows.Count '*** Added

For i = 2 To Mrows
Cells(i, 4).Value = i
Next i

ActiveSheet.UsedRange.Sort Key1:=Range("A1"), _
Order1:=xlAscending, Key2:=Range("B1"), _
Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
' Range("C2:C" & Mrows).Select
p = 0

For n = 2 To Mrows
If Cells(n, 1).Value = Cells(n + 1, 1) Then
p = p + 1
Cells(n, 3).Value = p
Else
Cells(n, 3).Value = p + 1
p = 0
End If
Next n

' Range("A1").Select
ActiveSheet.UsedRange.Sort Key1:=Range("D1"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Columns("D:D").Delete
Range("A1").Select
End Sub
'---------------------------------------

Regards,
Jim Cone
San Francisco, USA


"Jim May" wrote in message
news:DX_Td.23233$7z6.5990@lakeread04...
Jim:
I got it !!! << This is my first (to me,,) real programming exercise.
<<vbg
I'm really excited. I'd never got it without your breaking the project
down into it's pieces - which I can't seem to do (my biggest problem, I
think..) Again thanks,
Here is my go at it (probably could be improved on) - Do you see any obvious
improvement(s) that could or should be made?
Jim May


-snip-


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default Solve Using VBA Routine

Thanks Jim for the additional comments;
appreciate your help on this.

PS:
I later came across a formula approach (as follows):
In Cell C2 (and copy down)..

{=COUNT(IF(($A$2:$A$11=A2)*($B$2:$B$11<=B2),$B$2:$ B$11))}

enter as CSE type.

Jim May



"Jim Cone" wrote in message
...
Jim,

Only two things...
The variables have not been declared and
most of the "Select" statements are not needed.

I particularly like the way you incremented the Column C values.
For what it's worth, do not assume a "UsedRange" will always
start in row one. Of course, in this case it does.
'Slightly revised code follows...
'------------------------------------
Sub Foo()
Dim Mrows As Long
Dim i As Long
Dim n As Long
Dim p As Long

Range("D1").Value = "AutoNo"
' ActiveSheet.UsedRange.Offset(0, 3).Resize(, 1).Select
' Mrows = Selection.Rows.Count
Mrows = ActiveSheet.UsedRange.Rows.Count '*** Added

For i = 2 To Mrows
Cells(i, 4).Value = i
Next i

ActiveSheet.UsedRange.Sort Key1:=Range("A1"), _
Order1:=xlAscending, Key2:=Range("B1"), _
Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
' Range("C2:C" & Mrows).Select
p = 0

For n = 2 To Mrows
If Cells(n, 1).Value = Cells(n + 1, 1) Then
p = p + 1
Cells(n, 3).Value = p
Else
Cells(n, 3).Value = p + 1
p = 0
End If
Next n

' Range("A1").Select
ActiveSheet.UsedRange.Sort Key1:=Range("D1"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Columns("D:D").Delete
Range("A1").Select
End Sub
'---------------------------------------

Regards,
Jim Cone
San Francisco, USA


"Jim May" wrote in message
news:DX_Td.23233$7z6.5990@lakeread04...
Jim:
I got it !!! << This is my first (to me,,) real programming exercise.
<<vbg
I'm really excited. I'd never got it without your breaking the project
down into it's pieces - which I can't seem to do (my biggest problem, I
think..) Again thanks,
Here is my go at it (probably could be improved on) - Do you see any

obvious
improvement(s) that could or should be made?
Jim May


-snip-



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Solve Using VBA Routine

Jim,

You are welcome and thanks for passing the formula along.
You may find, like me, that once you start programming,
it seems to be easier to write code rather than figure out formulas.

For what it's worth, replace the second occurrence of "Jon"
with "Alex", and see what you get using the array formula.

Regards,
Jim Cone


"Jim May" wrote in message
news:fK5Ud.23258$7z6.18254@lakeread04...
Thanks Jim for the additional comments;
appreciate your help on this.

PS:
I later came across a formula approach (as follows):
In Cell C2 (and copy down)..
{=COUNT(IF(($A$2:$A$11=A2)*($B$2:$B$11<=B2),$B$2:$ B$11))}
enter as CSE type.
Jim May




"Jim Cone" wrote in message
...
Jim,

Only two things...
The variables have not been declared and
most of the "Select" statements are not needed.

I particularly like the way you incremented the Column C values.
For what it's worth, do not assume a "UsedRange" will always
start in row one. Of course, in this case it does.
'Slightly revised code follows...
'------------------------------------
Sub Foo()
Dim Mrows As Long
Dim i As Long
Dim n As Long
Dim p As Long

Range("D1").Value = "AutoNo"
' ActiveSheet.UsedRange.Offset(0, 3).Resize(, 1).Select
' Mrows = Selection.Rows.Count
Mrows = ActiveSheet.UsedRange.Rows.Count '*** Added

For i = 2 To Mrows
Cells(i, 4).Value = i
Next i

ActiveSheet.UsedRange.Sort Key1:=Range("A1"), _
Order1:=xlAscending, Key2:=Range("B1"), _
Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
' Range("C2:C" & Mrows).Select
p = 0

For n = 2 To Mrows
If Cells(n, 1).Value = Cells(n + 1, 1) Then
p = p + 1
Cells(n, 3).Value = p
Else
Cells(n, 3).Value = p + 1
p = 0
End If
Next n

' Range("A1").Select
ActiveSheet.UsedRange.Sort Key1:=Range("D1"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Columns("D:D").Delete
Range("A1").Select
End Sub
'---------------------------------------

Regards,
Jim Cone
San Francisco, USA


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sub routine Gord Dibben Excel Discussion (Misc queries) 0 November 13th 09 12:15 AM
Using (what goes in here) for sub routine Wally Steadman[_4_] Excel Programming 2 December 30th 04 06:51 PM
SUB ROUTINE tokirk Excel Programming 1 January 19th 04 02:17 AM
Routine?? Zax Excel Programming 3 December 19th 03 05:50 PM
Need VBA Routine John M. Lembo Excel Programming 0 July 13th 03 01:51 AM


All times are GMT +1. The time now is 04:23 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"