Elapsed Time Calculation... revisited!?!

  • Thread starter JinkyJulie via OfficeKB.com
  • Start date
J

JinkyJulie via OfficeKB.com

Hello again all...

I have two tables... First of varying row lengths... second is a fixed size.
..

Wish to subtract TIME value of first cell (first column) in the last row from
TIME value of first cell (first column) in the third row (all in the first
table) and then paste result in second cell in the second column of the
second table.

===========================================================
Illustration:

TIME LOCATION
01:00 Front Door
01:05 Side Door
third ==> 01:10 Large Door
01:15 Small Door
01:20 Black Door
last ==> 01:25 White Door

1:25 - 1:10 = 0:15

DOORS CHECKED: 6
NAME: Xiu Leng Fong
ELAPSED TIME: 0:15 <== Pasted here

========================================================

I'm getting confused in the gathering of the correct cells... I have found
some help (the time calculation I have OK) in the forums, but I cannot seem
to get it right... Cannot get this RANGE stuff down...

Can someone give me a push???

Thanks in advance for helping me and taking the time to read my post..

Julie
 
J

Jay Freedman

This will be easier to keep straight if you assign an understandable name to
each location and object you're dealing with. The following does that in
excruciating detail. :) When you get more comfortable with it, you can
collapse some of the steps.

Sub demo()
Dim FirstTable As Table
Dim SecondTable As Table

Dim StartTimeCell As Cell
Dim EndTimeCell As Cell
Dim ResultCell As Cell

Dim StartTimeRange As Range
Dim EndTimeRange As Range
Dim ResultRange As Range

Dim StartTime As String
Dim EndTime As String

' Assign the tables
Set FirstTable = ActiveDocument.Tables(1)
Set SecondTable = ActiveDocument.Tables(2)

' Assign the cells
' If your table has a header row,
' make this Row:=4 instead...
Set StartTimeCell = FirstTable.Cell( _
Row:=3, Column:=1)

Set EndTimeCell = FirstTable.Cell( _
Row:=FirstTable.Rows.Count, Column:=1)

Set ResultCell = SecondTable.Cell( _
Row:=2, Column:=2)

' Assign the ranges, and exclude the
' cell marker from each
Set StartTimeRange = StartTimeCell.Range
StartTimeRange.MoveEnd wdCharacter, -1

Set EndTimeRange = EndTimeCell.Range
EndTimeRange.MoveEnd wdCharacter, -1

Set ResultRange = ResultCell.Range
ResultRange.MoveEnd wdCharacter, -1

' Get the text from each range
StartTime = StartTimeRange.Text
EndTime = EndTimeRange.Text

' Now do your calculation with the values of
' StartTime and EndTime, and assign the
' resulting value to ResultRange.Text

End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

David Horowitz

Julie,
The following code should work, assuming these are the first two tables in
your document:
'-----------CODE-----------
Sub DoIt()
ActiveDocument.Tables(2).Cell(2, 2).Range.Text = _
GetCellText(ActiveDocument.Tables(1), _
ActiveDocument.Tables(1).Rows.Count, 1) - _
GetCellText(ActiveDocument.Tables(1), 3, 1)
End Sub

Function GetCellText(objTable As Word.Table, R As Long, C As Long) As String
' remove the trailing chars
On Error Resume Next
Dim s As String
s = objTable.Cell(R, C).Range.Text
GetCellText = Left$(s, Len(s) - 2)
End Function
'----------END CODE----------------
The GetCellText function lops off the two trailing special characters Word
has at the end of a Table Cell to give you just the text of the cell.
There are other ways to write that function. For example, the following also
works:
Function GetCellText(objTable As Word.Table, R As Long, C As Long) As String
On Error Resume Next
Dim rng As Word.Range
Set rng = objTable.Cell(R, C).Range
rng.End = rng.End - 1
GetCellText = rng.Text
End Function

HTH
 
D

David Horowitz

I like Jay's suggestion - in professional code, I always do that - it's too
painful to try to figure out convoluted lines of code sometimes.
Also, assigning a variable can be more efficient since you don't refer to
"ActiveDocument.Tables(n)" multiple times.
 
J

JinkyJulie via OfficeKB.com

Jay and David...

Thanks for taking the time... I understand both of them...

I cannot seem to test either though... This is a common problem for me...
As soon as I try something new, like your bits of code... I get this "File
Not Found: VBA6.DLL" error... I do not understand why...

I cannot find any answers on the WWW... (note the file exists on my computer,
in the right places and all of the appropriate references are in place...)

Maybe I should post a general thread to find out what's happening... I am
sure it is something that I am doing, but I do not know what... (FYI... XP,
Office 2003...)

I will keep trying.... I will let you know as soon as I test your codes....

Thanks again,

Julie
 
D

David Horowitz

Make sure when you go to Tools > References that Visual Basic for
Applications has a checkmark next to it. If not, check it, and try again. If
it is already checked, you might try unchecking it, saving, closing, coming
back in, and rechecking it and see if it works.
Another issue could be your VBA6.DLL may not be "registered" properly.
Locate your VBA6.DLL file, go to a Command Prompt, and type "regsvr32 <path
to file>\VBA6.DLL and press Enter. You should get a message saying it has
been registered properly. Now you can go try again, and be sure to
double-check Tools > References.
 

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