VBA: count sevens

  • Thread starter Thread starter fins812
  • Start date Start date
F

fins812

i need to write a VBA program that reads in one five-digit integer an
determines and prints how many of the digits are sevens. A textbo
needs to be used for input and a label for display.

can anyone help or lead me in the right direction?

thanks
 
Hi
to give you something to start with

Sub foo()
Dim ret As String
Dim count
Do
ret = InputBox("Enter 5 digit value")
Loop Until Len(ret) = 5 And IsNumeric(ret)
count = Len(ret) - Len(Replace(ret, "7", ""))
MsgBox "Entry " & ret & " contains " & count & " seven(s)."
End Sub
 
Are you talking about a form?

Private Sub CommandButton1_Click()
Dim cSevens
With TextBox1
cSevens = Len(.Text) - Len(Replace(.Text, "7", ""))
End With
Label1.Caption = cSevens & " seven(s) input"
End Sub

Private Sub TextBox1_Enter()
Label1.Caption = ""
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
If Len(.Text) <> 5 Then
MsgBox "Please inpout a 5 digit number"
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
Cancel = True
End If
End With
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top