Excel 如何将属性指定给复选框
嗨,我有一个复选框和代码,如果我运行它会显示错误。请更正此错误Excel 如何将属性指定给复选框,excel,vba,Excel,Vba,嗨,我有一个复选框和代码,如果我运行它会显示错误。请更正此错误 Private Sub CheckBox1_Click() With Sheets("bom") If .CheckBox1.Value = True Then .Range("show_all_level") = "Yes" Else .Range("show_all_level") = "No" End If End
Private Sub CheckBox1_Click()
With Sheets("bom")
If .CheckBox1.Value = True Then
.Range("show_all_level") = "Yes"
Else
.Range("show_all_level") = "No"
End If
End With
End Sub
错误类型:
试试下面的代码,它应该可以处理您的“显示所有级别”
名称范围的不同场景
Option Explicit
Private Sub CheckBox1_Click()
Dim Nm As Name
Dim NmExist As Boolean
Dim NameRng As Range
' loop through all Name Ranges in ThisWorkbook
For Each Nm In ThisWorkbook.Names
If Nm.Parent.Name Like "bom" Then '<-- check if name range parent (Sheet.Name) is "bom"
MsgBox Nm.Name & " Name Range exists is sheet " & Chr(34) & Nm.Parent.Name & Chr(34)
NmExist = True ' raise the flag >> Name exist in "bom" sheet
Set NameRng = Nm.RefersToRange ' set the Range to the Name Range
Exit For
ElseIf Nm.Parent.CodeName Like "ThisWorkbook" Then '<-- check if name scope is "ThisWorkbook"
MsgBox Nm.Name & " Name Range exists as WorkBook scope"
NmExist = True ' raise the flag >> Name exist in Workbook scope
Set NameRng = Nm.RefersToRange ' set the Range to the Name Range
Exit For
End If
Next Nm
' verify that "show_all_level" name exist in "bom" sheet (or Workbook scope)
If Not NmExist Then
MsgBox Chr(34) & "show_all_level" & Chr(34) & "Name Range, doesn't exist in the desired sheet", vbCritical
Exit Sub
End If
With Sheets("bom")
If .CheckBox1.Value = True Then
NameRng.Value = "Yes"
Else
NameRng.Value = "No"
End If
End With
End Sub
选项显式
专用子复选框1_单击()
名称为Dim Nm
作为布尔存在
Dim NameRng As范围
'循环浏览此工作簿中的所有名称范围
用于此工作簿中的每个Nm。名称
如果Nm.Parent.Name类似于“bom”,则工作簿范围中存在该名称
Set NameRng=Nm.refrestorange'将范围设置为名称范围
退出
如果结束
下一纳米
'验证“bom”表(或工作簿范围)中是否存在“显示所有级别”名称
如果不存在,那么
MsgBox Chr(34)和“显示所有级别”&Chr(34)和“名称范围,不存在于所需的工作表中”,vbCritical
出口接头
如果结束
带图纸(“bom”)
如果.CheckBox1.Value=True,则
NameRng.Value=“是”
其他的
NameRng.Value=“否”
如果结束
以
端接头
您可能没有定义一个称为“显示所有级别”的范围。检查“公式”选项卡>“名称管理器”,查看是否定义了名为“显示所有级别”的范围?定义的命名范围显示所有级别是否与复选框位于同一工作表上?i、 e.工作表(“bom”)更重要的是,检查框1是否在工作表bom中?是的,它在工作表(“bom”)中,当错误发生时,哪一行高亮显示?@dineshkumarpalanisame请参见@0m3r感谢此链接;)过去我需要它很多次;)