2017-08-25 14 views
1

私はドロップダウンリストのコンテンツコントロールで開始時刻と終了時刻の表を作成しています。 Time InリストとTime Outリストの選択に応じて、合計時間を計算する方法を理解しようとしています。私はテーブルの行を追加したり削除したりするコマンドボタンがあるので、テーブルに未使用の行がたくさんあるのではなく、必要なものだけが表示されるため、ちょっとしたキャッチがあります。MS Word VBAテーブル内の時間と時間の計算を計算

たびに1/4時間間隔であると私はに関する0:15など、夜12時30分は0.50で、0.25である

もう一つれるように結果をフォーマットするために取得しようとしていますドロップダウンリスト。コードでは、ドロップダウンリストごとにいくつのエントリがあるかがわかります。これをより効率的にする方法はありますか、それとも実際には各項目をリストするための唯一の方法ですか?それは長く狂っている。

ありがとうございます!

Sub MakeLaborRow(oTable As Table) 
Dim oNewRow As Row 
Dim oRng As Range 
Dim oCell As Cell 
Dim iCell As Integer 
Dim oCC As ContentControl, oCC1 As ContentControl, oCC2 As ContentControl 
Dim lngCell1 As Long, lngCell2 As Long 
lngCell1 = 0: lngCell2 = 0 
If Not ActiveDocument.ProtectionType = wdNoProtection Then 
    ActiveDocument.Unprotect 
End If 
Set oNewRow = oTable.Rows.Add 
oNewRow.Range.Font.Bold = False 
For iCell = 1 To 6 
    Set oCell = oNewRow.Cells(iCell) 
    Set oRng = oCell.Range 
    oRng.End = oRng.End - 1 
    Select Case iCell 
     Case 1 
      Set oCC = oRng.ContentControls.Add _ 
         (Range:=oRng, _ 
         Type:=wdContentControlDate) 
      With oCC 
       .SetPlaceholderText , , "Select Date" 
       .DateDisplayFormat = "ddd MM/dd/yyyy" 
       .Tag = "Date" & oCell.RowIndex 
      End With 
     Case 2 
      Set oCC = oRng.ContentControls.Add(wdContentControlDropdownList) 
      With oCC 
       .SetPlaceholderText , , "Choose Description" 
       .DropdownListEntries.Add "Labor Time" 
       .DropdownListEntries.Add "Travel Time" 
       .DropdownListEntries.Add "Wait Time" 
       .Tag = "Description" & oCell.RowIndex 
      End With 
     Case 3 
      Set oCC = oRng.ContentControls.Add(wdContentControlDropdownList) 
      With oCC 
       .SetPlaceholderText , , "Time In " 
       .DropdownListEntries.Add "12:00 AM" 
       .DropdownListEntries.Add "12:15 AM" 
       .DropdownListEntries.Add "12:30 AM" 
       .DropdownListEntries.Add "12:45 AM" 
       .DropdownListEntries.Add "1:00 AM" 
       .DropdownListEntries.Add "1:15 AM" 
       .DropdownListEntries.Add "1:30 AM" 
       .DropdownListEntries.Add "1:45 AM" 
       .DropdownListEntries.Add "2:00 AM" 
       .DropdownListEntries.Add "2:15 AM" 
       .DropdownListEntries.Add "2:30 AM" 
       .DropdownListEntries.Add "2:45 AM" 
       .DropdownListEntries.Add "3:00 AM" 
       .DropdownListEntries.Add "3:15 AM" 
       .DropdownListEntries.Add "3:30 AM" 
       .DropdownListEntries.Add "3:45 AM" 
       .DropdownListEntries.Add "4:00 AM" 
       .DropdownListEntries.Add "4:15 AM" 
       .DropdownListEntries.Add "4:30 AM" 
       .DropdownListEntries.Add "4:45 AM" 
       .DropdownListEntries.Add "5:00 AM" 
       .DropdownListEntries.Add "5:15 AM" 
       .DropdownListEntries.Add "5:30 AM" 
       .DropdownListEntries.Add "5:45 AM" 
       .DropdownListEntries.Add "6:00 AM" 
       .DropdownListEntries.Add "6:15 AM" 
       .DropdownListEntries.Add "6:30 AM" 
       .DropdownListEntries.Add "6:45 AM" 
       .DropdownListEntries.Add "7:00 AM" 
       .DropdownListEntries.Add "7:15 AM" 
       .DropdownListEntries.Add "7:30 AM" 
       .DropdownListEntries.Add "7:45 AM" 
       .DropdownListEntries.Add "8:00 AM" 
       .DropdownListEntries.Add "8:15 AM" 
       .DropdownListEntries.Add "8:30 AM" 
       .DropdownListEntries.Add "8:45 AM" 
       .DropdownListEntries.Add "9:00 AM" 
       .DropdownListEntries.Add "9:15 AM" 
       .DropdownListEntries.Add "9:30 AM" 
       .DropdownListEntries.Add "9:45 AM" 
       .DropdownListEntries.Add "10:00 AM" 
       .DropdownListEntries.Add "10:15 AM" 
       .DropdownListEntries.Add "10:30 AM" 
       .DropdownListEntries.Add "10:45 AM" 
       .DropdownListEntries.Add "11:00 AM" 
       .DropdownListEntries.Add "11:15 AM" 
       .DropdownListEntries.Add "11:30 AM" 
       .DropdownListEntries.Add "11:45 AM" 
       .DropdownListEntries.Add "12:00 PM" 
       .DropdownListEntries.Add "12:15 PM" 
       .DropdownListEntries.Add "12:30 PM" 
       .DropdownListEntries.Add "12:45 PM" 
       .DropdownListEntries.Add "1:00 PM" 
       .DropdownListEntries.Add "1:15 PM" 
       .DropdownListEntries.Add "1:30 PM" 
       .DropdownListEntries.Add "1:45 PM" 
       .DropdownListEntries.Add "2:00 PM" 
       .DropdownListEntries.Add "2:15 PM" 
       .DropdownListEntries.Add "2:30 PM" 
       .DropdownListEntries.Add "2:45 PM" 
       .DropdownListEntries.Add "3:00 PM" 
       .DropdownListEntries.Add "3:15 PM" 
       .DropdownListEntries.Add "3:30 PM" 
       .DropdownListEntries.Add "3:45 PM" 
       .DropdownListEntries.Add "4:00 PM" 
       .DropdownListEntries.Add "4:15 PM" 
       .DropdownListEntries.Add "4:30 PM" 
       .DropdownListEntries.Add "4:45 PM" 
       .DropdownListEntries.Add "5:00 PM" 
       .DropdownListEntries.Add "5:15 PM" 
       .DropdownListEntries.Add "5:30 PM" 
       .DropdownListEntries.Add "5:45 PM" 
       .DropdownListEntries.Add "6:00 PM" 
       .DropdownListEntries.Add "6:15 PM" 
       .DropdownListEntries.Add "6:30 PM" 
       .DropdownListEntries.Add "6:45 PM" 
       .DropdownListEntries.Add "7:00 PM" 
       .DropdownListEntries.Add "7:15 PM" 
       .DropdownListEntries.Add "7:30 PM" 
       .DropdownListEntries.Add "7:45 PM" 
       .DropdownListEntries.Add "8:00 PM" 
       .DropdownListEntries.Add "8:15 PM" 
       .DropdownListEntries.Add "8:30 PM" 
       .DropdownListEntries.Add "8:45 PM" 
       .DropdownListEntries.Add "9:00 PM" 
       .DropdownListEntries.Add "9:15 PM" 
       .DropdownListEntries.Add "9:30 PM" 
       .DropdownListEntries.Add "9:45 PM" 
       .DropdownListEntries.Add "10:00 PM" 
       .DropdownListEntries.Add "10:15 PM" 
       .DropdownListEntries.Add "10:30 PM" 
       .DropdownListEntries.Add "10:45 PM" 
       .DropdownListEntries.Add "11:00 PM" 
       .DropdownListEntries.Add "11:15 PM" 
       .DropdownListEntries.Add "11:30 PM" 
       .DropdownListEntries.Add "11:45 PM" 
       .Tag = "TimeIn" & oCell.RowIndex 
      End With 
     Case 4 
      Set oCC = oRng.ContentControls.Add(wdContentControlDropdownList) 
      With oCC 
       .SetPlaceholderText , , "Time Out" 
       .DropdownListEntries.Add "12:00 AM" 
       .DropdownListEntries.Add "12:15 AM" 
       .DropdownListEntries.Add "12:30 AM" 
       .DropdownListEntries.Add "12:45 AM" 
       .DropdownListEntries.Add "1:00 AM" 
       .DropdownListEntries.Add "1:15 AM" 
       .DropdownListEntries.Add "1:30 AM" 
       .DropdownListEntries.Add "1:45 AM" 
       .DropdownListEntries.Add "2:00 AM" 
       .DropdownListEntries.Add "2:15 AM" 
       .DropdownListEntries.Add "2:30 AM" 
       .DropdownListEntries.Add "2:45 AM" 
       .DropdownListEntries.Add "3:00 AM" 
       .DropdownListEntries.Add "3:15 AM" 
       .DropdownListEntries.Add "3:30 AM" 
       .DropdownListEntries.Add "3:45 AM" 
       .DropdownListEntries.Add "4:00 AM" 
       .DropdownListEntries.Add "4:15 AM" 
       .DropdownListEntries.Add "4:30 AM" 
       .DropdownListEntries.Add "4:45 AM" 
       .DropdownListEntries.Add "5:00 AM" 
       .DropdownListEntries.Add "5:15 AM" 
       .DropdownListEntries.Add "5:30 AM" 
       .DropdownListEntries.Add "5:45 AM" 
       .DropdownListEntries.Add "6:00 AM" 
       .DropdownListEntries.Add "6:15 AM" 
       .DropdownListEntries.Add "6:30 AM" 
       .DropdownListEntries.Add "6:45 AM" 
       .DropdownListEntries.Add "7:00 AM" 
       .DropdownListEntries.Add "7:15 AM" 
       .DropdownListEntries.Add "7:30 AM" 
       .DropdownListEntries.Add "7:45 AM" 
       .DropdownListEntries.Add "8:00 AM" 
       .DropdownListEntries.Add "8:15 AM" 
       .DropdownListEntries.Add "8:30 AM" 
       .DropdownListEntries.Add "8:45 AM" 
       .DropdownListEntries.Add "9:00 AM" 
       .DropdownListEntries.Add "9:15 AM" 
       .DropdownListEntries.Add "9:30 AM" 
       .DropdownListEntries.Add "9:45 AM" 
       .DropdownListEntries.Add "10:00 AM" 
       .DropdownListEntries.Add "10:15 AM" 
       .DropdownListEntries.Add "10:30 AM" 
       .DropdownListEntries.Add "10:45 AM" 
       .DropdownListEntries.Add "11:00 AM" 
       .DropdownListEntries.Add "11:15 AM" 
       .DropdownListEntries.Add "11:30 AM" 
       .DropdownListEntries.Add "11:45 AM" 
       .DropdownListEntries.Add "12:00 PM" 
       .DropdownListEntries.Add "12:15 PM" 
       .DropdownListEntries.Add "12:30 PM" 
       .DropdownListEntries.Add "12:45 PM" 
       .DropdownListEntries.Add "1:00 PM" 
       .DropdownListEntries.Add "1:15 PM" 
       .DropdownListEntries.Add "1:30 PM" 
       .DropdownListEntries.Add "1:45 PM" 
       .DropdownListEntries.Add "2:00 PM" 
       .DropdownListEntries.Add "2:15 PM" 
       .DropdownListEntries.Add "2:30 PM" 
       .DropdownListEntries.Add "2:45 PM" 
       .DropdownListEntries.Add "3:00 PM" 
       .DropdownListEntries.Add "3:15 PM" 
       .DropdownListEntries.Add "3:30 PM" 
       .DropdownListEntries.Add "3:45 PM" 
       .DropdownListEntries.Add "4:00 PM" 
       .DropdownListEntries.Add "4:15 PM" 
       .DropdownListEntries.Add "4:30 PM" 
       .DropdownListEntries.Add "4:45 PM" 
       .DropdownListEntries.Add "5:00 PM" 
       .DropdownListEntries.Add "5:15 PM" 
       .DropdownListEntries.Add "5:30 PM" 
       .DropdownListEntries.Add "5:45 PM" 
       .DropdownListEntries.Add "6:00 PM" 
       .DropdownListEntries.Add "6:15 PM" 
       .DropdownListEntries.Add "6:30 PM" 
       .DropdownListEntries.Add "6:45 PM" 
       .DropdownListEntries.Add "7:00 PM" 
       .DropdownListEntries.Add "7:15 PM" 
       .DropdownListEntries.Add "7:30 PM" 
       .DropdownListEntries.Add "7:45 PM" 
       .DropdownListEntries.Add "8:00 PM" 
       .DropdownListEntries.Add "8:15 PM" 
       .DropdownListEntries.Add "8:30 PM" 
       .DropdownListEntries.Add "8:45 PM" 
       .DropdownListEntries.Add "9:00 PM" 
       .DropdownListEntries.Add "9:15 PM" 
       .DropdownListEntries.Add "9:30 PM" 
       .DropdownListEntries.Add "9:45 PM" 
       .DropdownListEntries.Add "10:00 PM" 
       .DropdownListEntries.Add "10:15 PM" 
       .DropdownListEntries.Add "10:30 PM" 
       .DropdownListEntries.Add "10:45 PM" 
       .DropdownListEntries.Add "11:00 PM" 
       .DropdownListEntries.Add "11:15 PM" 
       .DropdownListEntries.Add "11:30 PM" 
       .DropdownListEntries.Add "11:45 PM" 
       .Tag = "TimeOut" & oCell.RowIndex 
      End With 
     Case 5 
      Set oCC = oRng.ContentControls.Add(wdContentControlText) 
      With oCC 
       .SetPlaceholderText , , "Total Hrs." 
       .Tag = "TotalHrs" & oCell.RowIndex 
      End With 
     Case 6 
      Set oCC = oRng.ContentControls.Add(wdContentControlText) 
      With oCC 
       .SetPlaceholderText , , "---" 
       .Tag = "Mileage" & oCell.RowIndex 
      End With 
    End Select 
Next iCell 
oNewRow.Cells(1).Select 
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, _ 
Password:="" 

lbl_Exit: 
Set oCell = Nothing 
Set oCC = Nothing 
Set oCC1 = Nothing 
Set oCC2 = Nothing 
Set oRng = Nothing 
Set oTable = Nothing 
Set oNewRow = Nothing 
Exit Sub 
End Sub 

答えて

0

これを追加して、時差を10進数で計算する必要がある場合に呼び出してください。

Sub TimeConversion() 

Dim strStart As String 
Dim strEnd As String 
Dim starttime As Variant 
Dim endtime As Variant 
Dim SplitStart 
Dim SplitEnd 
Dim StartResult As Long, EndResult As Long 
Dim HoursWorked As Long 

strStart = "9:00 AM" 
strEnd = "5:00 PM" 

''''''' 
starttime = Left(strStart, Len(strStart) - 3) 
SplitStart = Split(starttime, ":") 
StartResult = SplitStart(0) + ((SplitStart(1) * 100)/60)/100 

''''''' 
endtime = Left(strEnd, Len(strEnd) - 3) 
SplitEnd = Split(endtime, ":") 
EndResult = (SplitEnd(0) + ((SplitEnd(1) * 100)/60)/100) + 12 

HoursWorked = (EndResult - StartResult) 
'MsgBox HoursWorked 

End Sub 
+0

これをContentControlOnExitに挿入すればよいですか? – BradBerger