Excel VBA 复选框 - 运行 单击代码时自行选中和取消选中
Excel VBA Checkbox - Checking and Unchecking By Itself When Running Click Code
我在 Q 列的受保护作品sheet 的每一行上都有一个复选框。选中或未选中此复选框时将 运行 下面的代码。
当我选中我想要的复选框时
- 取消保护 sheet
- 解锁 R 列中的单元格(复选框旁边)
- 将单元格背景颜色更改为白色
- 保留单元格值
- 清除公式
- 保护sheet
当我取消选中我想要的复选框时
- 取消保护 sheet
- 将单元格背景颜色更改为灰色
- 在单元格中输入公式
- 锁定单元格
- 保护sheet
出于某种原因,当我选中复选框(未选中)代码 运行s 并且出于某种原因,复选框恢复为未选中状态。如果复选框最初被选中并且我取消选中它,代码 运行s 并且由于某种原因复选框恢复为选中状态,则会发生相反的情况。我的代码没有取消选中或选中复选框。
谁能帮我弄清楚为什么选中或取消选中后复选框会发生变化?
Sub PartQuantitiesCheckBox_Click()
Dim sCheckboxName, sCheckboxValue, sCheckboxChecked, sDS1BuildRange As String
Dim iCurrentRow As Integer
sCheckboxName = Application.Caller
' If this returns 1 then the checkbox was checked and we clicked it to uncheck it.
sCheckboxValue = ActiveSheet.Shapes(sCheckboxName).ControlFormat.Value
If sCheckboxValue = "1" Then
sCheckboxChecked = "False"
MsgBox ("sCheckboxChecked = False")
Else
sCheckboxChecked = "True"
MsgBox ("sCheckboxChecked = True")
End If
' The checkbox name is prefaced with "cbPartQtyNeeded" followed by the row number (eg. cbPartQtyNeeded4).
' Strip the row number out of the checkbox name.
iCurrentRow = Mid(sCheckboxName, 16, Len(sCheckboxName) - 15)
sDS1BuildRange = "R" & iCurrentRow
ActiveSheet.Unprotect
If sCheckboxChecked = "False" Then
Range(sDS1BuildRange).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Range(sDS1BuildRange).Formula = "=IFERROR(IF($P" & iCurrentRow & "*'Cover Sheet'!$M=0,"""",$P" & iCurrentRow & "*'Cover Sheet'!$M),"""")"
Range(sDS1BuildRange).Locked = True
Else
Range(sDS1BuildRange).Locked = False
Range(sDS1BuildRange).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
sValue = Range(sDS1BuildRange).Value
Range(sDS1BuildRange).Formula = ""
Range(sDS1BuildRange).Value = sValue
End If
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End Sub
我创建了一个全新的复选框,并用它替换了其他一个复选框。我之前复制了我创建的第一个复选框的所有复选框,然后将它们重命名。我认为这在某种程度上把他们搞砸了。创建一个新的似乎 运行 代码没有重新 checking/unchecking 复选框。
我在 Q 列的受保护作品sheet 的每一行上都有一个复选框。选中或未选中此复选框时将 运行 下面的代码。
当我选中我想要的复选框时
- 取消保护 sheet
- 解锁 R 列中的单元格(复选框旁边)
- 将单元格背景颜色更改为白色
- 保留单元格值
- 清除公式
- 保护sheet
当我取消选中我想要的复选框时
- 取消保护 sheet
- 将单元格背景颜色更改为灰色
- 在单元格中输入公式
- 锁定单元格
- 保护sheet
出于某种原因,当我选中复选框(未选中)代码 运行s 并且出于某种原因,复选框恢复为未选中状态。如果复选框最初被选中并且我取消选中它,代码 运行s 并且由于某种原因复选框恢复为选中状态,则会发生相反的情况。我的代码没有取消选中或选中复选框。
谁能帮我弄清楚为什么选中或取消选中后复选框会发生变化?
Sub PartQuantitiesCheckBox_Click()
Dim sCheckboxName, sCheckboxValue, sCheckboxChecked, sDS1BuildRange As String
Dim iCurrentRow As Integer
sCheckboxName = Application.Caller
' If this returns 1 then the checkbox was checked and we clicked it to uncheck it.
sCheckboxValue = ActiveSheet.Shapes(sCheckboxName).ControlFormat.Value
If sCheckboxValue = "1" Then
sCheckboxChecked = "False"
MsgBox ("sCheckboxChecked = False")
Else
sCheckboxChecked = "True"
MsgBox ("sCheckboxChecked = True")
End If
' The checkbox name is prefaced with "cbPartQtyNeeded" followed by the row number (eg. cbPartQtyNeeded4).
' Strip the row number out of the checkbox name.
iCurrentRow = Mid(sCheckboxName, 16, Len(sCheckboxName) - 15)
sDS1BuildRange = "R" & iCurrentRow
ActiveSheet.Unprotect
If sCheckboxChecked = "False" Then
Range(sDS1BuildRange).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Range(sDS1BuildRange).Formula = "=IFERROR(IF($P" & iCurrentRow & "*'Cover Sheet'!$M=0,"""",$P" & iCurrentRow & "*'Cover Sheet'!$M),"""")"
Range(sDS1BuildRange).Locked = True
Else
Range(sDS1BuildRange).Locked = False
Range(sDS1BuildRange).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
sValue = Range(sDS1BuildRange).Value
Range(sDS1BuildRange).Formula = ""
Range(sDS1BuildRange).Value = sValue
End If
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End Sub
我创建了一个全新的复选框,并用它替换了其他一个复选框。我之前复制了我创建的第一个复选框的所有复选框,然后将它们重命名。我认为这在某种程度上把他们搞砸了。创建一个新的似乎 运行 代码没有重新 checking/unchecking 复选框。