• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

## Recommended Posts

Okay... I want to read in one five-digit Integer and determine how many digits in the Integer are 7''s. I was wondering if there is a VB function for this????

##### Share on other sites
Not really. But you can do this:

  Public Function count7(Num as integer) as integer Dim strNum as string, i as integer, cnt as integer strNum = Str(Num) For i = 1 to len(strNum) If mid(strNum, i, 1) = "7" then _ cnt = cnt + 1 Next i count7 = cntEnd Function

##### Share on other sites
Or even further, you can do this..

  Public Function count7(num as integer) as integer Dim strNum as string, i as integer, i2 as integer strNum = str(num) i = instr(strNum, "7") While Not (i=0) i2 = i i = instr(i+1, strNum, "7") Loop count7 = i2End Function

This will probably work faster.

Edited by - sporty on February 20, 2002 8:10:47 PM

##### Share on other sites
why not use do until instead of while not?

do until (i=0)

or even "while i" should work properly... since anything not = 0 SHOULD be true (I think VB handles this properly).

Also, i think Sporty messed up.. he put i2 = i, where he really meant i2 = i2 + 1

Billy - BillyB@mrsnj.com

I really should create a user name sometime

##### Share on other sites
Yes you''re right, I forgot that part

I only use While Not because I''m simply more used to reading it that way. Seems a bit clearer when I''m quickly browsing old code. Just a preference.