PDA

View Full Version : Counting reset when going between Sub



Designer6
02-19-2008, 10:17 AM
Hi,

I have a few questions i am not clear with;

1. How can i make the counter "mycount" from resetting to 1 when going between sub?
2. How do i pass counter "mycount" value like 1, 2, 3...etc from one sub "findbutton sub" to another sub "hiderows sub"?

When the program is in the findbutton sub() mycount is equal to 1. When the program exits findbutton sub() to commandbutton_click sub() and back to findbutton sub() mycount first equals to 0 then1. For somehow it didn't increment at all or resetting when going back and forth between sub.

Thanks
Designer6



private sub commandbutton_click()
call findbuttons (checkbox1, rng)
call findbuttons (checkbox2, rng)
-------------
------------
'for 30 checkboxes

call hiderows
End sub

private sub findbutton()
Dim mycount as integer
mycount = 0
if checkbox & i.value = true Then
code in here
mycount = mycount+1

End If
End sub
private sub hiderows()
code
End Sub

Designer6
02-19-2008, 10:50 AM
The first question is already solved. Basically set mycount to static integer

Bob Phillips
02-19-2008, 10:53 AM
You try to call findbuttons, but the sub is called findbutton.

The calls have parameters, but there are no corresponding arguments in the called sub.

Where is hiderows called?

Designer6
02-19-2008, 11:14 AM
You try to call findbuttons, but the sub is called findbutton.

The calls have parameters, but there are no corresponding arguments in the called sub.

Where is hiderows called?
sorry it was a spelling mistake it should be findbuttons
Bellow is the current code

private sub commandbutton_click()
Set rng = .Range("P8:IA254")
call findbuttons (checkbox1, rng)
call findbuttons (checkbox2, rng)
-------------
------------
'for 30 checkboxes

call hiderows (mycount) 'this give me an error
End sub

private sub findbuttons(cb As msforms.CheckBox, rng As Range)
Static mycount as integer
If cb.Value = True Then = true Then
code in here
mycount = mycount+1

End If
End sub
private sub hiderows(mycount as integer) ' i don't know passing by this way is correct or not
code
End Sub

Designer6
02-19-2008, 11:33 AM
I got the problem solved


option explicit
public mycount as integer

sub commandbutton_click()
Set rng = .Range("P8:IA254")
call findbuttons (checkbox1, rng)
call findbuttons (checkbox2, rng)
-------------
------------
'for 30 checkboxes

call hiderows (mycount) 'this give me an error
End sub

private sub findbuttons(cb As msforms.CheckBox, rng As Range)

If cb.Value = True Then = true Then
code in here
mycount = mycount+1

End If
End sub
private sub hiderows(mycount as integer) ' i don't know passing by this way is correct or not
code
End Sub

Bob Phillips
02-19-2008, 01:52 PM
Another way



Private Sub commandbutton_click()
Dim cnt As Long
Dim rng As Range

cnt = 0
Set rng = .Range("P8:IA254")
Call findbuttons(CheckBox1, rng, cnt)
Call findbuttons(checkbox2, rng, cnt)
'-------------
'------------
'for 30 checkboxes

Call hiderows(cnt) 'this give me an error
End Sub

Private Sub findbuttons(cb As msforms.CheckBox, rng As Range, ByRef cnt As Long)
Static mycount As Integer
If cb.Value = True Then
'code in here
cnt = cnt + 1

End If
End Sub

Private Sub hiderows(mycount As Long)
'code
End Sub

Designer6
02-19-2008, 03:15 PM
Hi Xld,

Thanks, it works great.

Regards,

Designer6