worksheet object not defined, code works in one sheet but not another.

huangapple go评论113阅读模式
英文:

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:

  1. renaming the worksheet Dim
  2. heading straight to an activesheet without a Dim statement
  3. 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

worksheet object not defined, code works in one sheet but not another.

  • 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 = &quot;Item Return&quot;
Const COLUMN_OFFSET As Long = -1
Const COLOR_INDEX As Long = 22
If ActiveSheet Is Nothing Then
MsgBox &quot;No visible workbooks open.&quot;, vbCritical, PROC_TITLE
Exit Sub
End If
If Not TypeOf ActiveSheet Is Worksheet Then
MsgBox &quot;&quot;&quot;&quot; &amp; ActiveSheet.Name &amp; &quot;&quot;&quot; is not a worksheet.&quot;, _
vbCritical, PROC_TITLE
Exit Sub
End If
Dim ws As Worksheet: Set ws = ActiveSheet
Dim CheckBoxName: CheckBoxName = Application.Caller
If IsError(CheckBoxName) Then
MsgBox &quot;This code is designed to run only when ticking a check box.&quot;, _
vbCritical, PROC_TITLE
Exit Sub
End If
Debug.Print &quot;Worksheet Name: &quot; &amp; ws.Name
Debug.Print &quot;Check Box Name: &quot; &amp; CheckBoxName
Dim chk As CheckBox
On Error Resume Next
Set chk = ws.CheckBoxes(CheckBoxName)
On Error GoTo 0
If chk Is Nothing Then
MsgBox &quot;There is no check box named &quot;&quot;&quot; &amp; CStr(CheckBoxName) &amp; &quot;&quot;&quot;.&quot;, _
vbCritical, PROC_TITLE
Exit Sub
End If
Debug.Print &quot;Check Box Value: &quot; &amp; chk.Value
Dim tCell As Range: Set tCell = chk.TopLeftCell
Debug.Print &quot;Top Left Cell Address: &quot; &amp; tCell.Address(0, 0)
Dim c As Long: c = tCell.Column + COLUMN_OFFSET
If c &lt; 0 Or c &gt; ws.Columns.Count Then
MsgBox &quot;There is no column &#39;&quot; &amp; c &amp; &quot;&#39;.&quot;, _
vbCritical, PROC_TITLE
Exit Sub
End If
Dim cCell As Range: Set cCell = tCell.Offset(, COLUMN_OFFSET)
&#39; the same as:
&#39;Set cCell = ws.Cells(tCell.Row, c)
Debug.Print &quot;Highlight Cell Address: &quot; &amp; cCell.Address(0, 0)
Dim DoHighlight As Boolean
If Len(CStr(cCell.Value)) &gt; 0 Then
If chk.Value &lt;&gt; 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

huangapple
  • 本文由 发表于 2023年6月8日 15:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定