英文:
worksheet object not defined, code works in one sheet but not another
问题
This sub is designed to control the colour of the cell adjacent to a checkbox when it is ticked without the checkbox object being linked to a cell. In the old version of the sheet, the identical code works exactly as intended. When applied to a new workbook, however, it fails on the line in bold with the error code below:
Run time error '1004'
Method 'Checkboxes' of object '_Worksheet' failed
Do I need to define the worksheet elsewhere in my code or is this an issue with the application caller? Entirely possible it's something else but I don't know how the same code that works in one book fails in another.
英文:
This sub is designed to control the colour of the cell adjacent to a checkbox when it is ticked without the checkbox object being linked to a cell. In the old version of the sheet the identical code works exactly as intended. When applied to a new workbook however it fails on the line in bold with the error code below:
> Run time error '1004'
> Method 'Checkboxes' of object '_Worksheet' failed
Do I need to define the worksheet elsewhere in my code or is this an issue with the application caller? Entirely possible its something else but I don't know how the same code that works in one book fails in another.
Sub ItemReturn()
Dim ws As Worksheet
Dim chk As CheckBox
Dim lColD As Long
Dim lColChk As Long
Dim lRow As Long
Dim rngD As Range
Dim lRowChk As Long
lColD = -1 'number of columns
lRow = 0
Set ws = ActiveSheet
**Set chk = ws.CheckBoxes(Application.Caller)**
lRow = chk.TopLeftCell.Row
lColChk = chk.TopLeftCell.Column
Set rngD = ws.Cells(lRow, lColChk + lColD)
Select Case chk.Value
Case 1 'box is checked
rngD.Interior.ColorIndex = 0
Case Else
If IsEmpty(rngD) = False Then rngD.Interior.ColorIndex = 22
End Select
End Sub
I've tried:
- renaming the worksheet Dim
- heading straight to an activesheet without a Dim statement
- removing the application caller from the line of code
答案1
得分: 1
以下是您要翻译的部分:
在工作表中通过选中任何复选框(表单控件)运行代码
- 正如我在评论中所述,宏需要分配给每个复选框,以使
Application.Caller
返回复选框的名称并使代码正常工作。 - 在尝试过程中,我提出了以下内容。它涵盖了您可能会遇到的一些意外情况。
- 完成后,请删除或注释掉
Debug.Print
行。
Sub ItemReturn()
Const PROC_TITLE As String = "Item Return"
Const COLUMN_OFFSET As Long = -1
Const COLOR_INDEX As Long = 22
If ActiveSheet Is Nothing Then
MsgBox "没有可见工作簿打开。", vbCritical, PROC_TITLE
Exit Sub
End If
If Not TypeOf ActiveSheet Is Worksheet Then
MsgBox """" & ActiveSheet.Name & """" & " 不是工作表。", _
vbCritical, PROC_TITLE
Exit Sub
End If
Dim ws As Worksheet: Set ws = ActiveSheet
Dim CheckBoxName: CheckBoxName = Application.Caller
If IsError(CheckBoxName) Then
MsgBox "此代码仅设计用于在选中复选框时运行。", _
vbCritical, PROC_TITLE
Exit Sub
End If
Debug.Print "工作表名称: " & ws.Name
Debug.Print "复选框名称: " & CheckBoxName
Dim chk As CheckBox
On Error Resume Next
Set chk = ws.CheckBoxes(CheckBoxName)
On Error GoTo 0
If chk Is Nothing Then
MsgBox "没有名为" & """" & CStr(CheckBoxName) & """" & "的复选框。", _
vbCritical, PROC_TITLE
Exit Sub
End If
Debug.Print "复选框值: " & chk.Value
Dim tCell As Range: Set tCell = chk.TopLeftCell
Debug.Print "左上角单元格地址: " & tCell.Address(0, 0)
Dim c As Long: c = tCell.Column + COLUMN_OFFSET
If c < 0 Or c > ws.Columns.Count Then
MsgBox "没有列'" & c & "'。", _
vbCritical, PROC_TITLE
Exit Sub
End If
Dim cCell As Range: Set cCell = tCell.Offset(, COLUMN_OFFSET)
' 与以下相同:
'Set cCell = ws.Cells(tCell.Row, c)
Debug.Print "高亮单元格地址: " & cCell.Address(0, 0)
Dim DoHighlight As Boolean
If Len(CStr(cCell.Value)) > 0 Then
If chk.Value <> 1 Then
DoHighlight = True
End If
End If
If DoHighlight Then
If Not cCell.Interior.ColorIndex = COLOR_INDEX Then
cCell.Interior.ColorIndex = COLOR_INDEX
End If
Else
If Not cCell.Interior.ColorIndex = xlNone Then
cCell.Interior.ColorIndex = xlNone
End If
End If
End Sub
复选框1的结果
工作表名称: Sheet1
复选框名称: 复选框 1
复选框值: 1
左上角单元格地址: B1
高亮单元格地址: A1
工作表名称: Sheet1
复选框名称: 复选框 1
复选框值: -4146
左上角单元格地址: B1
高亮单元格地址: A1
英文:
Run Code By Ticking Any Check Box (Forms Control) in a Worksheet
- As I stated in the comments, the macro needs to be assigned to each check box for
Application.Caller
to return the name of the check box and the code to work. - While playing around with it, I came up with the following. It covers some of the surprises you may encounter.
- Remove or out-comment the
Debug.Print
lines when done playing.
<!-- language: lang-vb -->
Sub ItemReturn()
Const PROC_TITLE As String = "Item Return"
Const COLUMN_OFFSET As Long = -1
Const COLOR_INDEX As Long = 22
If ActiveSheet Is Nothing Then
MsgBox "No visible workbooks open.", vbCritical, PROC_TITLE
Exit Sub
End If
If Not TypeOf ActiveSheet Is Worksheet Then
MsgBox """" & ActiveSheet.Name & """ is not a worksheet.", _
vbCritical, PROC_TITLE
Exit Sub
End If
Dim ws As Worksheet: Set ws = ActiveSheet
Dim CheckBoxName: CheckBoxName = Application.Caller
If IsError(CheckBoxName) Then
MsgBox "This code is designed to run only when ticking a check box.", _
vbCritical, PROC_TITLE
Exit Sub
End If
Debug.Print "Worksheet Name: " & ws.Name
Debug.Print "Check Box Name: " & CheckBoxName
Dim chk As CheckBox
On Error Resume Next
Set chk = ws.CheckBoxes(CheckBoxName)
On Error GoTo 0
If chk Is Nothing Then
MsgBox "There is no check box named """ & CStr(CheckBoxName) & """.", _
vbCritical, PROC_TITLE
Exit Sub
End If
Debug.Print "Check Box Value: " & chk.Value
Dim tCell As Range: Set tCell = chk.TopLeftCell
Debug.Print "Top Left Cell Address: " & tCell.Address(0, 0)
Dim c As Long: c = tCell.Column + COLUMN_OFFSET
If c < 0 Or c > ws.Columns.Count Then
MsgBox "There is no column '" & c & "'.", _
vbCritical, PROC_TITLE
Exit Sub
End If
Dim cCell As Range: Set cCell = tCell.Offset(, COLUMN_OFFSET)
' the same as:
'Set cCell = ws.Cells(tCell.Row, c)
Debug.Print "Highlight Cell Address: " & cCell.Address(0, 0)
Dim DoHighlight As Boolean
If Len(CStr(cCell.Value)) > 0 Then
If chk.Value <> 1 Then
DoHighlight = True
End If
End If
If DoHighlight Then
If Not cCell.Interior.ColorIndex = COLOR_INDEX Then
cCell.Interior.ColorIndex = COLOR_INDEX
End If
Else
If Not cCell.Interior.ColorIndex = xlNone Then
cCell.Interior.ColorIndex = xlNone
End If
End If
End Sub
Results for Check Box 1
Worksheet Name: Sheet1
Check Box Name: Check Box 1
Check Box Value: 1
Top Left Cell Address: B1
Highlight Cell Address: A1
Worksheet Name: Sheet1
Check Box Name: Check Box 1
Check Box Value: -4146
Top Left Cell Address: B1
Highlight Cell Address: A1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论