Looping and omit certain interations using an InputBox

A

andreas

Dear Experts:
I wonder whether it is possible to iterate through a collection of
objects and omit a couple of iterations by entering the iteration
numbers to be omitted into an input box and then let the macro run.
Here is an example

Sub TableMacro()
Dim i as integer
For i = 1 to 7
If (i <> 3) And (i <> 4) And (i<>12) Then
'the 3,4 and 12 should be entered in an Input Box such as "3;4;12"

Set tbl = ActiveDocument.Tables(i)

If tbl.Rows(1) etc. then
'do stuff
End If
End If
Next i

End Sub

I wonder whether this is feasible. I hope I have made myself made
clear.
Regards, Andreas
 
J

Jay Freedman

Sub demo()
Dim inpList As String
Dim skipList As Variant
Dim skipVal As Variant
Dim bSkipThis As Boolean
Dim i As Integer

inpList = InputBox$( _
"Enter values to skip, separated by semicolons")
If Len(inpList) > 0 Then
skipList = Split(inpList, ";")
Else
skipList = Array(" ")
End If

For i = 1 To 7
bSkipThis = False
For Each skipVal In skipList
If Trim(skipVal) = CStr(i) Then
bSkipThis = True
Exit For
End If
Next skipVal

If Not bSkipThis Then
' Set tbl = ActiveDocument.Tables(i)
'
' If tbl.Rows(1) etc. then
' 'do stuff
' End If
End If
Next
End Sub

However, I'd probably prefer to make a userform
(http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm) with a
multiselect list box or a set of check boxes, each with a description of the
table that number refers to, instead of forcing the user to type the proper
numbers and semicolons.

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

andreas

Sub demo()
    Dim inpList As String
    Dim skipList As Variant
    Dim skipVal As Variant
    Dim bSkipThis As Boolean
    Dim i As Integer

    inpList = InputBox$( _
        "Enter values to skip, separated by semicolons")
    If Len(inpList) > 0 Then
        skipList = Split(inpList, ";")
    Else
        skipList = Array(" ")
    End If

    For i = 1 To 7
        bSkipThis = False
        For Each skipVal In skipList
            If Trim(skipVal) = CStr(i) Then
                bSkipThis = True
                Exit For
            End If
        Next skipVal

        If Not bSkipThis Then
'            Set tbl = ActiveDocument.Tables(i)
'
'            If tbl.Rows(1) etc. then
'            'do stuff
'            End If
        End If
    Next
End Sub

However, I'd probably prefer to make a userform
(http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm) with a
multiselect list box or a set of check boxes, each with a description of the
table that number refers to, instead of forcing the user to type the proper
numbers and semicolons.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso
all may benefit.









- Show quoted text -

Dear Jay,
perfect. Exactly what I wanted. Thank you very much for your
professional and 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