InputBox allowing only numeric values

A

andreas

Dear Experts:

below macro allows me to set the width of a picture/in-line-shape (via
an InputBox) and apply this width to all pictures.

I would like to restrict the data entry to just numeric values. As
long as no numeric value is entered the inputbox is to re-appear, i.e
there shouldn't be any error messages just the re-appearance of the
InputBox.


Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Public Sub ResizePics()
Dim oDoc As Document, oShape As InlineShape
Dim strData As String

Set oDoc = Application.ActiveDocument
strData = InputBox("Resize all images in the document to ...
cm", "Enter width value in cm")

If iILShapeCount > 0 Then
MsgBox "No shapes in current document!", vbOKOnly, "No pictures
found!"


For Each oShape In oDoc.InlineShapes
oShape.LockAspectRatio = msoTrue
oShape.Width = 28.34 * strData

Next oShape

Set oDoc = Nothing

End Sub
 
P

Pesach Shelnitz

Hi Andreas,

Replace the line

strData = InputBox("Resize all images in the document to ...
cm", "Enter width value in cm")

by these lines

strData = ""
Do While IsNumeric(strData) = False
strData = InputBox("Resize all images in the document to ... cm", _
"Enter width value in cm")
Loop
 
J

Jay Freedman

Leaving out the picture sizing code and concentrating on handling the input,

Sub x()
Dim strData As String
Dim numData As Single

strData = InputBox( _
"Resize all images in the document to ... cm", _
"Enter width value in cm")

If StrPtr(strData) = 0 Then ' canceled
Exit Sub
Else
While Val(strData) = 0
strData = InputBox( _
"Resize all images in the document to ... cm", _
"Enter width value in cm")
If StrPtr(strData) = 0 Then ' canceled
Exit Sub
End If
Wend
End If

numData = Val(strData)

MsgBox strData & " read as " & CStr(numData)
End Sub

Things you need to know about this:

- The Val() function returns the number represented by the characters in the
string up to (but not including) the first character that isn't a digit or a
decimal or thousands separator in the current localization. If the string is
zero-length or if the first character isn't a digit or a decimal separator, the
Val() function returns zero.

- Val() will accept an input such as '4.5 cm' with the number at the beginning,
but strData still contains the extra characters at the end. Using this string in
the expression 28.34 * strData will cause a type mismatch error. Instead, you
should use numData in computing the width.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

Jay Freedman

I over-complicated; the part before the While statement isn't needed. This will
do:

Sub x()
Dim strData As String
Dim numData As Single

While Val(strData) = 0
strData = InputBox( _
"Resize all images in the document to ... cm", _
"Enter width value in cm")
If StrPtr(strData) = 0 Then ' canceled
Exit Sub
End If
Wend

numData = Val(strData)

MsgBox strData & " read as " & CStr(numData)
End Sub

Pesach's solution will work as well, but won't accept entries that contain
non-numeric characters anywhere in the string (some users may try entries like
'3.5 cm' and then get frustrated).
 
A

andreas

I over-complicated; the part before the While statement isn't needed. This will
do:

Sub x()
    Dim strData As String
    Dim numData As Single

    While Val(strData) = 0
        strData = InputBox( _
          "Resize all images in the document to ... cm", _
          "Enter width value in cm")
        If StrPtr(strData) = 0 Then ' canceled
            Exit Sub
        End If
    Wend

    numData = Val(strData)

    MsgBox strData & " read as " & CStr(numData)
End Sub

Pesach's solution will work as well, but won't accept entries that contain
non-numeric characters anywhere in the string (some users may try entrieslike
'3.5 cm' and then get frustrated).











- Show quoted text -

Hi Jay,

very great coding.Thank you very much for your terrific help. Regards,
Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top