RE: Different tables by JeanGuyMarcil
JeanGuyMarcil
Wed Apr 02 09:31:03 PDT 2008
"Doc60" wrote:
> I have two tables in the same document with the same information, which is a
> row of values. The information in Row A below is entered as fields in
> file/properties/custom. I want the same values to be entered in each table so
> the person doesn't have to enter them twice(once for each table) therefore
> the field names for each column value is the same in each table. Is there a
> way to designate in two different macros (one for each table) that it is a
> seperate table so that when Row B is calculated for the second table it
> doesn't revert back to the first table.
>
> For example
>
> RowA 3.5 3.7 3.8 9.5 9.8
>
> however each table uses a different value to multiple to get row B. For
> example
>
> 1st table Row A 3.5 3.7 3.8 9.5 9.8
> each value is multiplied by 10 to get row B
>
> 1st table Row B 350 370 380 950 980
> Seperate Macro same document
> 2nd table Row A 3.5 3.7 3.8 9.5 9.8
> each value is multiplied by 100 to get Row B
>
> 2nd table Row B 3500 3700 3800 9500 9800
Why two macros? One will do the job...
Sub test()
Dim lngTableIdx As Long
Dim tblCurrent As Table
If Not Selection.Information(wdWithInTable) Then
MsgBox "You must place the cursor in a table for " _
& "this procedure to run properly.", vbExclamation, _
"Cancelled"
Exit Sub
End If
Set tblCurrent = Selection.Tables(1)
With ActiveDocument
lngTableIdx = .Range(.Range.Start, tblCurrent.Range.End).Tables.Count
End With
Select Case lngTableIdx
Case 1
MsgBox "Mutiply by 10."
With tblCurrent
' .Cell(2, 1).Range.Text = CStr(CLng(ActiveDocument _
.CustomDocumentProperties("Name").Value * 10))
End With
Case 2
MsgBox "Mutiply by 100."
Case Else
MsgBox "Do something else."
End Select
End Sub
If you have to do heavy table manipulations, then it might be worth it to
create another sub to handle that:
Option Explicit
Sub test()
Dim lngTableIdx As Long
Dim tblCurrent As Table
If Not Selection.Information(wdWithInTable) Then
MsgBox "You must place the cursor in a table for " _
& "this procedure to run properly.", vbExclamation, _
"Cancelled"
Exit Sub
End If
Set tblCurrent = Selection.Tables(1)
With ActiveDocument
lngTableIdx = .Range(.Range.Start, tblCurrent.Range.End).Tables.Count
End With
Select Case lngTableIdx
Case 1
HandleTable tblCurrent, 10
Case 2
HandleTable tblCurrent, 100
Case Else
MsgBox "Do something else."
End Select
End Sub
Sub HandleTable(tblTarget As Table, lngMult As Long)
With tblTarget
.Cell(2, 1).Range.Text = CStr(CLng(ActiveDocument _
.CustomDocumentProperties("NameA").Value * lngMult))
.Cell(2, 2).Range.Text = CStr(CLng(ActiveDocument _
.CustomDocumentProperties("NameB").Value * lngMult))
' etc.
End With
End Sub