从值更改和复选框运行宏

时间:2015-03-09 20:43:55

标签: excel vba excel-vba

我有一些代码工作,当标记复选框时,执行以下操作:取消隐藏“旅行计算器”表,解锁当前工作表,根据“旅行计算器”表单中的单元格更改单元格的值,然后再次锁定当前工作表。如果没有标记复选框,则单元格应保持解锁状态,并且“旅行计算器”应保持隐藏状态。这很好,但是,我希望在标记复选框(值= True)时,根据对旅行计算器所做的更改,不断更新当前工作表中的值。

当框值= True并且“旅行计算器”表格中的旅行总额值发生变化时,是否有人对下面的代码进行了任何更改,我可以尝试对其进行更新? 谢谢!

Private Sub travelcalc_Click()
ActiveSheet.Unprotect Password:="password"
    If Me.travelcalc.Value = False Then
    Range("C19:L20").Locked = False
    Sheets("Travel Calculator").Visible = False
Else
    Sheets("Travel Calculator").Visible = True
    Range("$C$19") = Worksheets("Travel Calculator").Range("$N$14")
    Range("$D$19") = Worksheets("Travel Calculator").Range("$P$14")
    Range("$E$19") = Worksheets("Travel Calculator").Range("$R$14")
    Range("$F$19") = Worksheets("Travel Calculator").Range("$T$14")
    Range("$G$19") = Worksheets("Travel Calculator").Range("$V$14")
    Range("$H$19") = Worksheets("Travel Calculator").Range("$X$14")
    Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$14")
    Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$14")
    Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$14")
    Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$14")
    Range("$C$20") = Worksheets("Travel Calculator").Range("$N$29")
    Range("$D$20") = Worksheets("Travel Calculator").Range("$P$29")
    Range("$E$20") = Worksheets("Travel Calculator").Range("$R$29")
    Range("$F$20") = Worksheets("Travel Calculator").Range("$T$29")
    Range("$G$20") = Worksheets("Travel Calculator").Range("$V$29")
    Range("$H$20") = Worksheets("Travel Calculator").Range("$X$29")
    Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$29")
    Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$29")
    Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$29")
    Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$29")
    Range("C19:L20").Locked = True
End If
    ActiveSheet.Protect Password:="password", AllowFormattingColumns:=True, AllowFormattingRows:=True
End Sub

2 个答案:

答案 0 :(得分:0)

将以下代码分配给旅行计算器工作表。它将评估每次在旅行计算器表单中移动单元格时是否对总值进行了更改,并在必要时检查并更新travelcalc框。如果选中了travelcalc框,它也会在您停用工作表后立即更新。

Public originalTotalValue As Currency

Private Sub Worksheet_Activate()
origionalTotalValue = Worksheets("Travel Calculator").Range("Your Travel Value").Value

End Sub

Private Sub Worksheet_Deactivate()
If Me.travelcalc.Value = True Then
    Worksheets("Sheet Where Original Range is Located").Range("$C$19") = Worksheets("Travel Calculator").Range("$N$14")
    Worksheets("Sheet Where Original Range is Located").Range("$D$19") = Worksheets("Travel Calculator").Range("$P$14")
    Worksheets("Sheet Where Original Range is Located").Range("$E$19") = Worksheets("Travel Calculator").Range("$R$14")
    Worksheets("Sheet Where Original Range is Located").Range("$F$19") = Worksheets("Travel Calculator").Range("$T$14")
    Worksheets("Sheet Where Original Range is Located").Range("$G$19") = Worksheets("Travel Calculator").Range("$V$14")
    Worksheets("Sheet Where Original Range is Located").Range("$H$19") = Worksheets("Travel Calculator").Range("$X$14")
    Worksheets("Sheet Where Original Range is Located").Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$14")
    Worksheets("Sheet Where Original Range is Located").Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$14")
    Worksheets("Sheet Where Original Range is Located").Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$14")
    Worksheets("Sheet Where Original Range is Located").Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$14")
    Worksheets("Sheet Where Original Range is Located").Range("$C$20") = Worksheets("Travel Calculator").Range("$N$29")
    Worksheets("Sheet Where Original Range is Located").Range("$D$20") = Worksheets("Travel Calculator").Range("$P$29")
    Worksheets("Sheet Where Original Range is Located").Range("$E$20") = Worksheets("Travel Calculator").Range("$R$29")
    Worksheets("Sheet Where Original Range is Located").Range("$F$20") = Worksheets("Travel Calculator").Range("$T$29")
    Worksheets("Sheet Where Original Range is Located").Range("$G$20") = Worksheets("Travel Calculator").Range("$V$29")
    Worksheets("Sheet Where Original Range is Located").Range("$H$20") = Worksheets("Travel Calculator").Range("$X$29")
    Worksheets("Sheet Where Original Range is Located").Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$29")
    Worksheets("Sheet Where Original Range is Located").Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$29")
    Worksheets("Sheet Where Original Range is Located").Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$29")
    Worksheets("Sheet Where Original Range is Located").Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$29")
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim updatedTotalValue As Currency
updatedTotalValue = Worksheets("Travel Calculator").Range("Your Travel Value").Value
If Me.travelcalc.Value = True And originalTotalValue <> updatedTotalValue Then
    Worksheets("Sheet Where Original Range is Located").Range("$C$19") = Worksheets("Travel Calculator").Range("$N$14")
    Worksheets("Sheet Where Original Range is Located").Range("$D$19") = Worksheets("Travel Calculator").Range("$P$14")
    Worksheets("Sheet Where Original Range is Located").Range("$E$19") = Worksheets("Travel Calculator").Range("$R$14")
    Worksheets("Sheet Where Original Range is Located").Range("$F$19") = Worksheets("Travel Calculator").Range("$T$14")
    Worksheets("Sheet Where Original Range is Located").Range("$G$19") = Worksheets("Travel Calculator").Range("$V$14")
    Worksheets("Sheet Where Original Range is Located").Range("$H$19") = Worksheets("Travel Calculator").Range("$X$14")
    Worksheets("Sheet Where Original Range is Located").Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$14")
    Worksheets("Sheet Where Original Range is Located").Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$14")
    Worksheets("Sheet Where Original Range is Located").Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$14")
    Worksheets("Sheet Where Original Range is Located").Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$14")
    Worksheets("Sheet Where Original Range is Located").Range("$C$20") = Worksheets("Travel Calculator").Range("$N$29")
    Worksheets("Sheet Where Original Range is Located").Range("$D$20") = Worksheets("Travel Calculator").Range("$P$29")
    Worksheets("Sheet Where Original Range is Located").Range("$E$20") = Worksheets("Travel Calculator").Range("$R$29")
    Worksheets("Sheet Where Original Range is Located").Range("$F$20") = Worksheets("Travel Calculator").Range("$T$29")
    Worksheets("Sheet Where Original Range is Located").Range("$G$20") = Worksheets("Travel Calculator").Range("$V$29")
    Worksheets("Sheet Where Original Range is Located").Range("$H$20") = Worksheets("Travel Calculator").Range("$X$29")
    Worksheets("Sheet Where Original Range is Located").Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$29")
    Worksheets("Sheet Where Original Range is Located").Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$29")
    Worksheets("Sheet Where Original Range is Located").Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$29")
    Worksheets("Sheet Where Original Range is Located").Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$29")
    origionalTotalValue = updatedTotalValue
End If
End Sub

答案 1 :(得分:0)

这是我的修改代码的样子。这段代码似乎工作得很好。谢谢你的帮助!

Private Sub travelcalc_Click()
ActiveSheet.Unprotect Password:="password"
    If Me.travelcalc.Value = False Then
    Range("C19:L20").Locked = False
    Sheets("Travel Calculator").Visible = False
Else
    Sheets("Travel Calculator").Visible = True
    Range("$C$19") = Worksheets("Travel Calculator").Range("$N$14")
    Range("$D$19") = Worksheets("Travel Calculator").Range("$P$14")
    Range("$E$19") = Worksheets("Travel Calculator").Range("$R$14")
    Range("$F$19") = Worksheets("Travel Calculator").Range("$T$14")
    Range("$G$19") = Worksheets("Travel Calculator").Range("$V$14")
    Range("$H$19") = Worksheets("Travel Calculator").Range("$X$14")
    Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$14")
    Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$14")
    Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$14")
    Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$14")
    Range("$C$20") = Worksheets("Travel Calculator").Range("$N$29")
    Range("$D$20") = Worksheets("Travel Calculator").Range("$P$29")
    Range("$E$20") = Worksheets("Travel Calculator").Range("$R$29")
    Range("$F$20") = Worksheets("Travel Calculator").Range("$T$29")
    Range("$G$20") = Worksheets("Travel Calculator").Range("$V$29")
    Range("$H$20") = Worksheets("Travel Calculator").Range("$X$29")
    Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$29")
    Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$29")
    Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$29")
    Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$29")
    Range("C19:L20").Locked = True
End If
ActiveSheet.Protect Password:="password", AllowFormattingColumns:=True, AllowFormattingRows:=True

End Sub

'the code below is entered into the travel sheet
Private Sub Worksheet_Calculate()
Worksheets("Direct Costs").Unprotect Password:="password"
Dim updatedTotalValue As Currency
Static oldval
    If Range("AG32").Value <> oldval Then
    oldval = Range("AG32").Value
    If Range("AL2").Value = True Then
    Worksheets("Direct Costs").Range("C19:L20").Locked = False
    Worksheets("Direct Costs").Range("$C$19") = Worksheets("Travel Calculator").Range("$N$14")
    Worksheets("Direct Costs").Range("$D$19") = Worksheets("Travel Calculator").Range("$P$14")
    Worksheets("Direct Costs").Range("$E$19") = Worksheets("Travel Calculator").Range("$R$14")
    Worksheets("Direct Costs").Range("$F$19") = Worksheets("Travel Calculator").Range("$T$14")
    Worksheets("Direct Costs").Range("$G$19") = Worksheets("Travel Calculator").Range("$V$14")
    Worksheets("Direct Costs").Range("$H$19") = Worksheets("Travel Calculator").Range("$X$14")
    Worksheets("Direct Costs").Range("$I$19") = Worksheets("Travel Calculator").Range("$Z$14")
    Worksheets("Direct Costs").Range("$J$19") = Worksheets("Travel Calculator").Range("$AB$14")
    Worksheets("Direct Costs").Range("$K$19") = Worksheets("Travel Calculator").Range("$AD$14")
    Worksheets("Direct Costs").Range("$L$19") = Worksheets("Travel Calculator").Range("$AF$14")
    Worksheets("Direct Costs").Range("$C$20") = Worksheets("Travel Calculator").Range("$N$29")
    Worksheets("Direct Costs").Range("$D$20") = Worksheets("Travel Calculator").Range("$P$29")
    Worksheets("Direct Costs").Range("$E$20") = Worksheets("Travel Calculator").Range("$R$29")
    Worksheets("Direct Costs").Range("$F$20") = Worksheets("Travel Calculator").Range("$T$29")
    Worksheets("Direct Costs").Range("$G$20") = Worksheets("Travel Calculator").Range("$V$29")
    Worksheets("Direct Costs").Range("$H$20") = Worksheets("Travel Calculator").Range("$X$29")
    Worksheets("Direct Costs").Range("$I$20") = Worksheets("Travel Calculator").Range("$Z$29")
    Worksheets("Direct Costs").Range("$J$20") = Worksheets("Travel Calculator").Range("$AB$29")
    Worksheets("Direct Costs").Range("$K$20") = Worksheets("Travel Calculator").Range("$AD$29")
    Worksheets("Direct Costs").Range("$L$20") = Worksheets("Travel Calculator").Range("$AF$29")
    Worksheets("Direct Costs").Range("C19:L20").Locked = True
End If
End If
    Worksheets("Direct Costs").Protect Password:="password", AllowFormattingColumns:=True, AllowFormattingRows:=True
End Sub