• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

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 Hermle
Andreas Hermle
  • 2
1 Solution
Chris BottomleyCommented:

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

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now