Restricting the input to certain values in an input box

A

andreas

Dear Experts:

below macro - which is running just fine - performs the following
tasks:

1. Ask the user to input a row number
2. The entered row number and all subsequent row numbers till the last
one will acquire a user defined paragraph style.

Now here comes my additional requirement to be incorporated into the
code:

The input is to be restricted to ....
1) only numeric values
2) should range only between 1 and the total number of rows of the
currently selected table
3) figures entered that are not within that range should throw an
error message (Input not within the range of row numbers 1 to total
count of rows (e.g. 20). After pressing ok, the input box is to
reappear.

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

Regards, Andreas



Sub Tbl_BodyStyle_SelectRow()
Dim oRng As Word.range
Dim oTbl As Word.Table
Dim AskRowNumber As String

If Not Selection.Information(wdWithInTable) Then
MsgBox "Please place the cursor into the table", _
vbOKOnly + vbCritical, "User-defined style for selected table
rows"
Exit Sub
End If


AskRowNumber = InputBox("Please indicate the first row number" &
vbCrLf & _
"to acquire <Dis_Tbl_Body_Text>", "style for rows x to last one")

Set oTbl = Selection.Tables(1)
Set oRng = oTbl.range
oRng.Start = oTbl.rows(AskRowNumber).range.Start
oRng.Style = "Dis_Tbl_Body_Text"
Set oTbl = Nothing
Set oRng = Nothing
End Sub
 
M

macropod

Hi Andreas,

Try:
Sub Tbl_BodyStyle_SelectRow()
Dim oRng As Word.Range
Dim oTbl As Word.Table
Dim AskRowNumber As String

If Not Selection.Information(wdWithInTable) Then
MsgBox "Please place the cursor into the table", _
vbOKOnly + vbCritical, "User-defined style for selected table Rows"
Exit Sub
End If

Set oTbl = Selection.Tables(1)
Set oRng = oTbl.Range

getrow:
AskRowNumber = InputBox("Please indicate the first row number" & vbCrLf & _
"to acquire <Dis_Tbl_Body_Text>", "style for rows x to " & oTbl.Rows.Count)
If Not IsNumeric(AskRowNumber) Then
MsgBox "Only numbers are valid!", vbCritical
GoTo getrow
ElseIf AskRowNumber < 1 Or AskRowNumber > oTbl.Rows.Coun Then
MsgBox "Only numbers between 0 and " & oTbl.Rows.Count & " are valid!", vbCritical
GoTo getrow
End If
oRng.Start = oTbl.Rows(AskRowNumber).Range.Start
oRng.Style = "Dis_Tbl_Body_Text"
Set oTbl = Nothing
Set oRng = Nothing
End Sub
 
A

andreas

Hi Andreas,

Try:
Sub Tbl_BodyStyle_SelectRow()
Dim oRng As Word.Range
Dim oTbl As Word.Table
Dim AskRowNumber As String

If Not Selection.Information(wdWithInTable) Then
  MsgBox "Please place the cursor into the table", _
  vbOKOnly + vbCritical, "User-defined style for selected table Rows"
  Exit Sub
End If

Set oTbl = Selection.Tables(1)
Set oRng = oTbl.Range

getrow:
AskRowNumber = InputBox("Please indicate the first row number" & vbCrLf& _
"to acquire <Dis_Tbl_Body_Text>", "style for rows x to " & oTbl.Rows.Count)
If Not IsNumeric(AskRowNumber) Then
  MsgBox "Only numbers are valid!", vbCritical
  GoTo getrow
ElseIf AskRowNumber < 1 Or AskRowNumber > oTbl.Rows.Coun Then
  MsgBox "Only numbers between 0 and " & oTbl.Rows.Count & " are valid!", vbCritical
  GoTo getrow
End If
oRng.Start = oTbl.Rows(AskRowNumber).Range.Start
oRng.Style = "Dis_Tbl_Body_Text"
Set oTbl = Nothing
Set oRng = Nothing
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]



andreas said:
Dear Experts:
below macro - which is running just fine - performs the following
tasks:
1. Ask the user to input a row number
2. The entered row number and all subsequent row numbers till the last
one will acquire a user defined paragraph style.
Now here comes my additional requirement to be incorporated into the
code:
The input is to be restricted to ....
1) only numeric values
2) should range only between 1 and the total number of rows of the
currently selected table
3) figures entered that are not within that range should throw an
error message (Input not within the range of row numbers 1 to total
count of rows (e.g. 20). After pressing ok, the input box is to
reappear.
Help is much appreciated. Thank you very much in advance.
Regards, Andreas
Sub Tbl_BodyStyle_SelectRow()
Dim oRng As Word.range
Dim oTbl As Word.Table
Dim AskRowNumber As String
If Not Selection.Information(wdWithInTable) Then
   MsgBox "Please place the cursor into the table", _
    vbOKOnly + vbCritical, "User-defined style for selected table
rows"
    Exit Sub
End If
AskRowNumber = InputBox("Please indicate the first row number" &
vbCrLf & _
"to acquire <Dis_Tbl_Body_Text>", "style for rows x to last one")
Set oTbl = Selection.Tables(1)
Set oRng = oTbl.range
oRng.Start = oTbl.rows(AskRowNumber).range.Start
oRng.Style = "Dis_Tbl_Body_Text"
Set oTbl = Nothing
Set oRng = Nothing
End Sub- Hide quoted text -

- Show quoted text -

Hi Macropod.

that's it. The macro is just what I wanted. Thank you very much for
your professional help. I really appreciate it.

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