Inputbox to enter numbers using VBA

Dear Experts:

Below macro, courtesy by an EE expert, splits the only table in the current document into several tables (separated by page breaks) based on rows where the there are two blank cells.

The macro works just fine.

I would like to be able to enter the two blank cells on which to split by means of an inputbox instead of doing a hard coding every time (see line 26 and 27).

The inputbox should restrict the entries to ...

... just two numbers (cell addresses) separated by one semicolon, such as (3;4 or 2;5 or 1;2)
... the numbers are not to exceed the number of columns, e.g. on a 5 column table, entering 4;6 is not possible)

I hope I could make myself clear.

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

Regards, Andreas

Sub Td_SplitTable_OnNewSet()

Dim t As Table
Dim r As Long
Dim tc As Long
Dim a As String
Dim b As String
Dim c As String
Dim cel As Cell

 If MsgBox("Would you like to split the only table in this current document into several tables each time the macro hits a row where two specific cells are blank?" & vbCrLf & vbCrLf & _
 "A blank table cell has two characters, Chr(13) & Chr(7)!", vbQuestion + vbYesNo, "Split Table into several tables") = vbNo Then
        Exit Sub
        End If

'Right at the beginning, insert two spaces into blank cells of the only table, necessary for the macro to work
'This is the header row with column headers
    For Each cel In ActiveDocument.Tables(1).rows(1).Cells
        If Len(cel.Range.Text) = 2 Then cel.Range.Text = "  "

tc = 1

Set t = ActiveDocument.Tables(1)
  For r = 1 To t.rows.Count
  a = Len(t.Cell(r, 3).Range.Text)
    b = Len(t.Cell(r, 4).Range.Text)
     'A blank cell has two characters: Chr(13) & Chr(7)
       If a = 2 And b = 2 Then 'That is cells in columns 3 and 4 are blank
      t.Cell(r, 4).Select
          Selection.InsertBreak Type:=wdPageBreak
            tc = tc + 1
          Set t = ActiveDocument.Tables(tc)
        r = 2
      End If
  Next r
End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:

No disrespect but I am not finding the working code all that reliable therefore all I post here is one check for input data:

Dim tbl As Table
Dim str As String
Dim bolContinueLoop
Dim arrVals As Variant
Dim col1 As Integer
Dim col2 As Integer
Dim r As Integer
Dim a As Long
Dim b As Long
Dim tc As Integer

    col1 = 0
    bolContinueLoop = True
    If ActiveDocument.Tables.Count = 0 Then Exit Sub
    Set tbl = ActiveDocument.Tables(1)
    Do While bolContinueLoop
        str = InputBox("Enter the cell range as column numbers separated by ';'", "Split Table", "1;3")
        If Len(str) = 0 Then bolContinueLoop = False
        arrVals = Split(str, ";")
        If UBound(arrVals) <> 1 Then
            bolContinueLoop = False
            If ((IsNumeric(arrVals(0))) And (IsNumeric(arrVals(1)))) Then
                If CInt(arrVals(0)) >= 1 And CInt(arrVals(0)) < tbl.Columns.Count And CInt(arrVals(1)) > 1 And CInt(arrVals(1)) <= tbl.Columns.Count And CInt(arrVals(0)) < CInt(arrVals(1)) Then
                    bolContinueLoop = False
                    col1 = CInt(arrVals(0))
                    col2 = CInt(arrVals(1))
                End If
            End If
        End If
    If col1 > 0 Then
'    ...
                a = Len(tbl.Cell(r, col1).Range.Text)
                b = Len(tbl.Cell(r, col2).Range.Text)
' ...

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Hi Chris,

thank you very much for your swift report. You are right, there is no error handling on my initial code I posted. The conditions under which I run this macro do not require any error handling, nevertheless I will incorporate an ErrHandler soon.

I will do some testing and then let you know.

Again, thank you very much.

Regards, Andreas
Andreas HermleTeam leaderAuthor Commented:

I am indeed very impressed. Works like a charm! Thank you very much for your great and professional support. I really appreciate it.

Regards, Andreas
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.