Different tables

D

Doc60

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
 
J

Jean-Guy Marcil

Doc60 said:
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
 
D

Doc60

Unfortunately I have already written two seperate macros due to the number of
other calculations that take place in the two different tables. So how would
I designate the different tables in the macro. So that when I run the second
macro for the second table it will not revert to the first table?

Thank you
 
J

Jean-Guy Marcil

Doc60 said:
Unfortunately I have already written two seperate macros due to the number of
other calculations that take place in the two different tables. So how would
I designate the different tables in the macro. So that when I run the second
macro for the second table it will not revert to the first table?

Look at my code. The first thing I do is identify the current table index so
that I know which Nth table I am dealing with.

Do the same at the top of each of your macros, or have a central macro that
will identify the table index, and then call the appropriate sub-macro once
the table index has been identified.

Also, see how I use a table object, make sure you do that, this way you
always know which table you are dealing with within the code.
 

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