VBA中的标签总和

时间:2019-07-10 11:05:45

标签: excel vba

enter image description here 基本上,选择一天后,我将输入每个员工的开始时间,结束时间和休息时间,并输入该天的输出。

选择第二天后,我要对给定的一天进行准确的计算,并在最后三列中列出。

Option Explicit

Dim hour1 As Date
Dim hour2 As Date
Dim hour3 As Date
Dim Totalhour As Date
Dim StartNumber As Integer
Dim EndNumber As Integer
Dim Sum As Double
Dim i As Integer
Dim x As Integer
Dim Total As Double

Private Sub cmdHoursWorked_Click()

If OpMon.Value = True Then

hour1 = txtTimeIn1.Text
hour2 = txtTimeOut1.Text
hour3 = cmbBreak1.Text
Total = txtUnitCom1.Text
Totalhour = ((hour2 - hour1) - hour3)
lblHrsWorked1.Caption = Round((Totalhour * 24), 2)
lblMon1.Caption = Round(((lblHrsWorked1.Caption * 3600) / txtUnitCom1.Text), 1)
'lblUnitWeek1.Caption = txtUnitCom1.Text'
'lblTHW1.Caption = lblHrsWorked1'

ElseIf OpWed.Value = True Then

hour1 = txtTimeIn1.Text
hour2 = txtTimeOut1.Text
hour3 = cmbBreak1.Text
Totalhour = ((hour2 - hour1) - hour3)
lblHrsWorked1.Caption = Round((Totalhour * 24), 2)
lblWed1.Caption = Round(((lblHrsWorked1.Caption * 3600) / txtUnitCom1.Text), 1)
'lblUnitWeek1.Caption = txtUnitCom1.Text'
'lblHrsWorked1.Caption = lblTHW1.Caption'
lblSecUnit1.Content = (lblMon1.Caption + lblTue1.Caption + lblWed1.Caption + lblThu1.Caption + lblFri1.Caption)

End If

End Sub

说,如果选择“星期一”,我将输入9:00和17:30作为开始时间和结束时间(间隔30分钟)。然后在“单位完成”文本框中输入500。然后,我计算工作时间(标签),并计算标签星期一标签上每秒完成的单位。

现在,如果选择了另一天,我想进行完全相同的计算,并根据用户的输入添加到最后三列

ElseIf OpTue.Value = True Then
    start_time = CDate(txtTimeIn1.Text)
    end_time = CDate(txtTimeOut1.Text)
    break_time = CInt(cmbBreak1.Text)
    units = CSng(txtUnitCom1.Text)
    ttl_secUnit1 = CSng(lblSecUnit1.Caption)
    sec_Mon1 = CSng(lblMon1.Caption)
    'sec_Tue1 = CSng(lblTue1.Caption)

   ' Calculation
    hours_worked = (CSng(end_time) - CSng(start_time)) * 24! - break_time / 60!
    rate = units / (3600! * hours_worked)
    ttl_secUnit1 = (CSng(sec_Mon1) + CSng(sec_Tue1))
    lblSecUnit1.Caption = WorksheetFunction.Sum(lblMon1.Caption, lblTue1.Caption)


    ' Results
    lblHrsWorked1.Caption = Round((hours_worked), 2)
    lblTue1.Caption = Format(rate, "0.000")
    'lblSecUnit1.Caption = Format(ttl_secUnit1, "0.000")

End If

1 个答案:

答案 0 :(得分:0)

您正在使用字符串进行数学运算,并希望VBA解释器能够实现您的预​​期。不要那样做相反,请通过三个不同的步骤处理数据:

  1. 从标签中获取输入,然后使用IntegerSingle或{{将其转换为类型DateCInt()CSng()的值1}}。
  2. 使用值进行数学运算。
  3. 使用CDate()函数将值转换为字符串以显示。

在下面的简单示例中

output

Format()

请注意,编译器会解释以下文字

  • Public Sub CalculateRate() ' Input values Dim start_time As Date, end_time As Date Dim break_time As Integer, units As Single start_time = CDate(Label1.Caption) end_time = CDate(Label2.Caption) break_time = CInt(Label3.Caption) units = CSng(Label4.Caption) ' Calculation Dim hours_worked As Single, rate As Single hours_worked = (CSng(end_time) - CSng(start_time)) * 24! - break_time / 60! rate = units / (3600! * hours_worked) ' Results Label5.Caption = Format(rate, "0.000") End Sub 作为1000(16位)
  • Integer作为100000(32位)
  • Long作为1000!浮点数(32位)
  • Single作为1000#浮点数(64位)