Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 394
Default Loop Not Working

Hi everyone,

Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next

Thanks in Advance.
All the Best.
Paul

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Loop Not Working

What is in BitCount?

What do you expect to see?

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Paul Black" wrote in message
ps.com...
Hi everyone,

Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next

Thanks in Advance.
All the Best.
Paul



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Loop Not Working

Because you always write to the same cell.

Range("b32").Select '<== move this line outside the loop
For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & _
" numbers " & Format(tly,"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next

--
Regards,
Tom Ogilvy


"Paul Black" wrote:

Hi everyone,

Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next

Thanks in Advance.
All the Best.
Paul


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 394
Default Loop Not Working

Thanks for the replies Bob & Tom,

Here is the code that I can't get to work. It should list several rows
of data starting in cell "B32" and continuing down. The code below
works fine if I want the output to start in cell "A1" but I can't get
it to start in cell "B32".
The other thing is that other output which works fine leaves the
active cell as "B29".

Option Explicit

Private Type Wheel
A As Currency
End Type

Private Type Digits
B(0 To 7) As Byte
End Type

Private BC(0 To 255) As Byte
Private WHL(0 To 20) As Wheel
Private Tested As Long
Private P As Integer

Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)

For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

Worksheets("Statistics").Select
With ActiveCell
.Offset(0, 0).Value = "Title"
.Offset(0, 1).Value = "For " & P & " numbers there are " & tly &
_
" different groups of " & cmb & " numbers. "
.Offset(1, 0).Select
End With
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

Private Function BitCount(ByVal X As Currency) As Long
Dim W As Wheel
Dim d As Digits
Dim idx As Long
Dim cnt As Long

W.A = X
LSet d = W
For idx = 0 To 7
cnt = cnt + BC(d.B(idx))
Next
BitCount = cnt
End Function

Private Function Nibs(ByVal Value As Byte) As Long
Select Case Value
Case 0
Exit Function
Case 1, 2, 4, 8
Nibs = 1
Exit Function
Case 3, 5, 6, 9, 10, 12
Nibs = 2
Exit Function
Case 7, 11, 13, 14
Nibs = 3
Exit Function
Case 15
Nibs = 4
End Select
End Function

Thanks in Advance.
All the Best.
Paul

On Aug 28, 11:02 pm, Tom Ogilvy
wrote:
Because you always write to the same cell.

Range("b32").Select '<== move this line outside the loop
For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & _
" numbers " & Format(tly,"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next

--
Regards,
Tom Ogilvy



"Paul Black" wrote:
Hi everyone,


Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.


For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next


Thanks in Advance.
All the Best.
Paul- Hide quoted text -


- Show quoted text -



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Loop Not Working

Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)

For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With Worksheets("Statistics").Range("B32")
.Offset(cmb - 1, 0).Value = "Title"
.Offset(cmb - 1, 1).Value = "For " & P & _
" numbers there are " & tly & _
" different groups of " & cmb & " numbers. "
End With
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

--
Regards,
Tom Ogilvy

"Paul Black" wrote:

Thanks for the replies Bob & Tom,

Here is the code that I can't get to work. It should list several rows
of data starting in cell "B32" and continuing down. The code below
works fine if I want the output to start in cell "A1" but I can't get
it to start in cell "B32".
The other thing is that other output which works fine leaves the
active cell as "B29".

Option Explicit

Private Type Wheel
A As Currency
End Type

Private Type Digits
B(0 To 7) As Byte
End Type

Private BC(0 To 255) As Byte
Private WHL(0 To 20) As Wheel
Private Tested As Long
Private P As Integer

Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)

For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

Worksheets("Statistics").Select
With ActiveCell
.Offset(0, 0).Value = "Title"
.Offset(0, 1).Value = "For " & P & " numbers there are " & tly &
_
" different groups of " & cmb & " numbers. "
.Offset(1, 0).Select
End With
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

Private Function BitCount(ByVal X As Currency) As Long
Dim W As Wheel
Dim d As Digits
Dim idx As Long
Dim cnt As Long

W.A = X
LSet d = W
For idx = 0 To 7
cnt = cnt + BC(d.B(idx))
Next
BitCount = cnt
End Function

Private Function Nibs(ByVal Value As Byte) As Long
Select Case Value
Case 0
Exit Function
Case 1, 2, 4, 8
Nibs = 1
Exit Function
Case 3, 5, 6, 9, 10, 12
Nibs = 2
Exit Function
Case 7, 11, 13, 14
Nibs = 3
Exit Function
Case 15
Nibs = 4
End Select
End Function

Thanks in Advance.
All the Best.
Paul

On Aug 28, 11:02 pm, Tom Ogilvy
wrote:
Because you always write to the same cell.

Range("b32").Select '<== move this line outside the loop
For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & _
" numbers " & Format(tly,"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next

--
Regards,
Tom Ogilvy



"Paul Black" wrote:
Hi everyone,


Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.


For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next


Thanks in Advance.
All the Best.
Paul- Hide quoted text -


- Show quoted text -






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 394
Default Loop Not Working

Brilliant Tom, thanks very much.
One last thing please.
I have a sheet named "Statistics". The code below caters for what I
need except for one thing. I need it to add a sheet named "Statistics"
if it does not exist when the program is run.
So basically, if the sheet named "Statistics" exists, delete it, add a
sheet named "Statistics" and make the whole of the sheet Tahoma font.
If it does NOT already exist then add it and make the whole of the
sheet Tahoma font..
This is what I have so far :-

' Delete the existing [Statistics] sheet and ADD a new one
Worksheets("Statistics").Select
Worksheets("Statistics").Delete
Worksheets.Add.Name = "Statistics"

' Format the WHOLE [Statistics] sheet as Tahoma
Cells.Font.Name = "Tahoma"

Thanks in Advanve.
All the Best.
Paul

On Aug 29, 2:06 pm, Tom Ogilvy
wrote:
Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)

For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With Worksheets("Statistics").Range("B32")
.Offset(cmb - 1, 0).Value = "Title"
.Offset(cmb - 1, 1).Value = "For " & P & _
" numbers there are " & tly & _
" different groups of " & cmb & " numbers. "
End With
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

--
Regards,
Tom Ogilvy



"Paul Black" wrote:
Thanks for the replies Bob & Tom,


Here is the code that I can't get to work. It should list several rows
of data starting in cell "B32" and continuing down. The code below
works fine if I want the output to start in cell "A1" but I can't get
it to start in cell "B32".
The other thing is that other output which works fine leaves the
active cell as "B29".


Option Explicit


Private Type Wheel
A As Currency
End Type


Private Type Digits
B(0 To 7) As Byte
End Type


Private BC(0 To 255) As Byte
Private WHL(0 To 20) As Wheel
Private Tested As Long
Private P As Integer


Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)


For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next


For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next


Worksheets("Statistics").Select
With ActiveCell
.Offset(0, 0).Value = "Title"
.Offset(0, 1).Value = "For " & P & " numbers there are " & tly &
_
" different groups of " & cmb & " numbers. "
.Offset(1, 0).Select
End With
Next


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub


Private Function BitCount(ByVal X As Currency) As Long
Dim W As Wheel
Dim d As Digits
Dim idx As Long
Dim cnt As Long


W.A = X
LSet d = W
For idx = 0 To 7
cnt = cnt + BC(d.B(idx))
Next
BitCount = cnt
End Function


Private Function Nibs(ByVal Value As Byte) As Long
Select Case Value
Case 0
Exit Function
Case 1, 2, 4, 8
Nibs = 1
Exit Function
Case 3, 5, 6, 9, 10, 12
Nibs = 2
Exit Function
Case 7, 11, 13, 14
Nibs = 3
Exit Function
Case 15
Nibs = 4
End Select
End Function


Thanks in Advance.
All the Best.
Paul


On Aug 28, 11:02 pm, Tom Ogilvy
wrote:
Because you always write to the same cell.


Range("b32").Select '<== move this line outside the loop
For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next


With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & _
" numbers " & Format(tly,"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next


--
Regards,
Tom Ogilvy


"Paul Black" wrote:
Hi everyone,


Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.


For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next


Thanks in Advance.
All the Best.
Paul- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Loop Not Working

Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range
Dim sh as Worksheet
Dim sh1 as Worksheet

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

set sh1 = Activesheet
Application.DisplayAlerts = False
On Error Resume Next
worksheets("Statistics").Delete
On Error goto 0
Application.Displayalerts = True
set sh = worksheets.Add( After:=Worksheets(worksheets.count))
sh.Name = "Statistics"
sh.Cells.Font.Name = "Tahoma"
sh1.activate

Set rng = Worksheets("Data").UsedRange '.Rows
P = Application.Max(rng)

For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With Worksheets("Statistics").Range("B32")
.Offset(cmb - 1, 0).Value = "Title"
.Offset(cmb - 1, 1).Value = "For " & P & _
" numbers there are " & tly & _
" different groups of " & cmb & " numbers. "
End With
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

--
Regards,
Tom Ogilvy

"Paul Black" wrote:

Brilliant Tom, thanks very much.
One last thing please.
I have a sheet named "Statistics". The code below caters for what I
need except for one thing. I need it to add a sheet named "Statistics"
if it does not exist when the program is run.
So basically, if the sheet named "Statistics" exists, delete it, add a
sheet named "Statistics" and make the whole of the sheet Tahoma font.
If it does NOT already exist then add it and make the whole of the
sheet Tahoma font..
This is what I have so far :-

' Delete the existing [Statistics] sheet and ADD a new one
Worksheets("Statistics").Select
Worksheets("Statistics").Delete
Worksheets.Add.Name = "Statistics"

' Format the WHOLE [Statistics] sheet as Tahoma
Cells.Font.Name = "Tahoma"

Thanks in Advanve.
All the Best.
Paul

On Aug 29, 2:06 pm, Tom Ogilvy
wrote:
Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)

For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next

With Worksheets("Statistics").Range("B32")
.Offset(cmb - 1, 0).Value = "Title"
.Offset(cmb - 1, 1).Value = "For " & P & _
" numbers there are " & tly & _
" different groups of " & cmb & " numbers. "
End With
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

--
Regards,
Tom Ogilvy



"Paul Black" wrote:
Thanks for the replies Bob & Tom,


Here is the code that I can't get to work. It should list several rows
of data starting in cell "B32" and continuing down. The code below
works fine if I want the output to start in cell "A1" but I can't get
it to start in cell "B32".
The other thing is that other output which works fine leaves the
active cell as "B29".


Option Explicit


Private Type Wheel
A As Currency
End Type


Private Type Digits
B(0 To 7) As Byte
End Type


Private BC(0 To 255) As Byte
Private WHL(0 To 20) As Wheel
Private Tested As Long
Private P As Integer


Sub Generate_Statistics()
Dim idx As Currency
Dim tly As Long
Dim cmb As Long
Dim rng As Range


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Set rng = Worksheets("Data").UsedRange.Rows
P = Application.Max(rng)


For idx = 0 To 255
BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next


For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next


Worksheets("Statistics").Select
With ActiveCell
.Offset(0, 0).Value = "Title"
.Offset(0, 1).Value = "For " & P & " numbers there are " & tly &
_
" different groups of " & cmb & " numbers. "
.Offset(1, 0).Select
End With
Next


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub


Private Function BitCount(ByVal X As Currency) As Long
Dim W As Wheel
Dim d As Digits
Dim idx As Long
Dim cnt As Long


W.A = X
LSet d = W
For idx = 0 To 7
cnt = cnt + BC(d.B(idx))
Next
BitCount = cnt
End Function


Private Function Nibs(ByVal Value As Byte) As Long
Select Case Value
Case 0
Exit Function
Case 1, 2, 4, 8
Nibs = 1
Exit Function
Case 3, 5, 6, 9, 10, 12
Nibs = 2
Exit Function
Case 7, 11, 13, 14
Nibs = 3
Exit Function
Case 15
Nibs = 4
End Select
End Function


Thanks in Advance.
All the Best.
Paul


On Aug 28, 11:02 pm, Tom Ogilvy
wrote:
Because you always write to the same cell.


Range("b32").Select '<== move this line outside the loop
For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next


With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & _
" numbers " & Format(tly,"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next


--
Regards,
Tom Ogilvy


"Paul Black" wrote:
Hi everyone,


Why does this not work please. It is suppose to produce a list several
lines long but only produces 1 line.


For cmb = 1 To P
tly = 0
For idx = 0 To (2 ^ P) - 1
If BitCount(idx / 5000) = cmb Then
tly = tly + 1
End If
Next
Range("b32").Select
With ActiveCell
.Offset(0, 0).Value = "W"
.Offset(1, 0).Value = "For " & P & " numbers " & Format(tly,
"#,##0") & _
" different " & cmb & " num. "
.Offset(2, 0).Select
End With
Next


Thanks in Advance.
All the Best.
Paul- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -




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
Why is this loop not working? Mike R. Excel Programming 12 July 25th 07 02:26 PM
Loop not working!! Simon Excel Programming 2 August 2nd 05 04:16 PM
Do...Loop not working Sunny Lin Excel Programming 1 April 14th 05 01:19 AM
for next loop not working Tom Ogilvy Excel Programming 0 September 27th 04 05:36 PM
for next loop not working Ron Rosenfeld Excel Programming 0 September 25th 04 04:07 AM


All times are GMT +1. The time now is 12:08 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"