Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ram Ram is offline
external usenet poster
 
Posts: 138
Default Do Until Question

I'm using the following code to number each cell in column A if there is a
value in column B. I need help setting my variable 1 when it start.

Sub count1()
Dim i As Integer
i = i + 1
Do Until Range("B" & i) = ""
i = i + 1
Range("a" & i) = i

Loop

End Sub

Thanks for any help


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Do Until Question


Hi Ram,

Does this do what you want?


Sub count1()
Dim i As Integer
i = 1
Do Until Range("B" & i) = ""
Range("a" & i) = i
i = i + 1
Loop

End Sub

Ken Johnson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Do Until Question

Hi Ram,

Or is this what you're trying to do?....

Sub count1()
Dim iLastRow As Long
Dim I As Long
Dim J As Long
iLastRow = Range("B" & Range("B:B").Rows.Count).End(xlUp).Row
For I = iLastRow To 1 Step -1
If Range("B" & I).Value < "" Then
J = J + 1
Range("A" & I).Value = _
WorksheetFunction.CountA(Range("B:B")) - J + 1
End If
Next I

End Sub

Ken Johnson

  #4   Report Post  
Posted to microsoft.public.excel.programming
Ram Ram is offline
external usenet poster
 
Posts: 138
Default Do Until Question

Thanks Ken this works great

"ram" wrote:

I'm using the following code to number each cell in column A if there is a
value in column B. I need help setting my variable 1 when it start.

Sub count1()
Dim i As Integer
i = i + 1
Do Until Range("B" & i) = ""
i = i + 1
Range("a" & i) = i

Loop

End Sub

Thanks for any help


  #5   Report Post  
Posted to microsoft.public.excel.programming
Ram Ram is offline
external usenet poster
 
Posts: 138
Default Do Until Question

Thanks Ken this works great

"Ken Johnson" wrote:

Hi Ram,

Or is this what you're trying to do?....

Sub count1()
Dim iLastRow As Long
Dim I As Long
Dim J As Long
iLastRow = Range("B" & Range("B:B").Rows.Count).End(xlUp).Row
For I = iLastRow To 1 Step -1
If Range("B" & I).Value < "" Then
J = J + 1
Range("A" & I).Value = _
WorksheetFunction.CountA(Range("B:B")) - J + 1
End If
Next I

End Sub

Ken Johnson




  #6   Report Post  
Posted to microsoft.public.excel.programming
Ram Ram is offline
external usenet poster
 
Posts: 138
Default Do Until Question

Thanks Ken this works great

"Ken Johnson" wrote:


Hi Ram,

Does this do what you want?


Sub count1()
Dim i As Integer
i = 1
Do Until Range("B" & i) = ""
Range("a" & i) = i
i = i + 1
Loop

End Sub

Ken Johnson


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Do Until Question


ram wrote:
Thanks Ken this works great


You're welcome ram. Thanks for the feedback.

Ken Johnson

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default Do Until Question


Hi ram,

After a little more thought I now realise my code is a little
defective. Nothing serious, it's just that as it stands it is a lot
slower than it can be. You might not notice the slow speed, the number
of rows in column B with values might not be all that large. However,
if the number of rows is substantial (I tried it out with around 65000)
the code takes about 300 seconds to run. The reason for the slow speed
is the count of non-blank cells in column B
(WorksheetFunction.CountA(Range("B:B"))) is inside the loop. I should
not have placed that inside the loop. Its value is constant and it only
needs to be evaluated the once.
The following code fixes up this problem. This amended code only takes
around 3 seconds to run, 100 times faster!...

Sub count1()
Dim iLastRow As Long
Dim I As Long
Dim J As Long
Dim iNonBlankCount As Long
iNonBlankCount = WorksheetFunction.CountA(Range("B:B"))
iLastRow = Range("B" & Range("B:B").Rows.Count).End(xlUp).Row
For I = iLastRow To 1 Step -1
If Range("B" & I).Value < "" Then
J = J + 1
Range("A" & I).Value = _
iNonBlankCount - J + 1
End If
Next I
End Sub


Ken Johnson

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
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good davegb Excel Programming 1 May 6th 05 06:35 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 22nd 05 03:30 PM


All times are GMT +1. The time now is 11:49 PM.

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

About Us

"It's about Microsoft Excel"