G
Greg Maxey
I have been playing around with some code to rearrange the columns of
a table.
E.g. existing data in a five column table
GRID NAME A B C D
1 2 3 4
Move column 5 before column 3. Then end result:
GRID NAME A D C B
1 4 3 2
I know (or think I know that you can't set a range = to a column so I
figured the best approach would be to write the data from the source
column (column 5) to an array. Add a new column before column 3.
Write the array results into the new column 3 and then delete the
source columne (now column 5).
Here is the basic code (stripped of error handling, etc.):
Sub ScrachMacroII()
Dim bProcess As Boolean
Dim myArray1() As String
Dim oCol1 As Long
Dim oCol2 As Long
Dim oTbl As Word.Table
Dim i As Long
Dim pStr1 As String
Dim newCol As Column
Dim lngLS As Long
bProcess = True
Do While bProcess
oCol1 = InputBox("Move column: ", "Source Column")
oCol2 = InputBox("Before column: ", "New Location")
On Error GoTo 0
For Each oTbl In ActiveDocument.Tables
If InStr(oTbl.Cell(1, 1).Range.Text, "GRID NAME") <> 0 Then
i = oTbl.Rows.Count
ReDim myArray1(i)
For i = 1 To oTbl.Rows.Count
pStr1 = oTbl.Cell(i, oCol1).Range.Text
myArray1(i - 1) = Left(pStr1, Len(pStr1) - 2)
Next i
Set newCol = oTbl.Columns.Add(BeforeColumn:=oTbl.Columns(oCol2))
lngLS = newCol.Next.Borders(wdBorderRight).LineStyle
newCol.Borders(wdBorderRight).LineStyle = lngLS
For i = 1 To oTbl.Rows.Count
oTbl.Cell(i, oCol2).Range.Text = myArray1(i - 1)
Next i
oTbl.Columns(oCol1 + 1).Delete
End If
Next oTbl
If MsgBox("Do you want to continue with another move?", _
vbQuestion + vbYesNo, "Continue?") = vbNo Then
bProcess = False
End If
Loop
Exit Sub
End Sub
I am just wondering if I have attempted to reinvent the wheel and if
there is a better, more simplified approach.
Thanks.
a table.
E.g. existing data in a five column table
GRID NAME A B C D
1 2 3 4
Move column 5 before column 3. Then end result:
GRID NAME A D C B
1 4 3 2
I know (or think I know that you can't set a range = to a column so I
figured the best approach would be to write the data from the source
column (column 5) to an array. Add a new column before column 3.
Write the array results into the new column 3 and then delete the
source columne (now column 5).
Here is the basic code (stripped of error handling, etc.):
Sub ScrachMacroII()
Dim bProcess As Boolean
Dim myArray1() As String
Dim oCol1 As Long
Dim oCol2 As Long
Dim oTbl As Word.Table
Dim i As Long
Dim pStr1 As String
Dim newCol As Column
Dim lngLS As Long
bProcess = True
Do While bProcess
oCol1 = InputBox("Move column: ", "Source Column")
oCol2 = InputBox("Before column: ", "New Location")
On Error GoTo 0
For Each oTbl In ActiveDocument.Tables
If InStr(oTbl.Cell(1, 1).Range.Text, "GRID NAME") <> 0 Then
i = oTbl.Rows.Count
ReDim myArray1(i)
For i = 1 To oTbl.Rows.Count
pStr1 = oTbl.Cell(i, oCol1).Range.Text
myArray1(i - 1) = Left(pStr1, Len(pStr1) - 2)
Next i
Set newCol = oTbl.Columns.Add(BeforeColumn:=oTbl.Columns(oCol2))
lngLS = newCol.Next.Borders(wdBorderRight).LineStyle
newCol.Borders(wdBorderRight).LineStyle = lngLS
For i = 1 To oTbl.Rows.Count
oTbl.Cell(i, oCol2).Range.Text = myArray1(i - 1)
Next i
oTbl.Columns(oCol1 + 1).Delete
End If
Next oTbl
If MsgBox("Do you want to continue with another move?", _
vbQuestion + vbYesNo, "Continue?") = vbNo Then
bProcess = False
End If
Loop
Exit Sub
End Sub
I am just wondering if I have attempted to reinvent the wheel and if
there is a better, more simplified approach.
Thanks.