View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone Jim Cone is offline
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-