VBA - 删除列/复选框/行期间出现未知变量错误(上一个工作代码)

VBA - Unknown variable error during column/checkbox/row removal (prev working code)

提问人:StillLearningThisStuff 提问时间:10/18/2023 最后编辑:StillLearningThisStuff 更新时间:10/19/2023 访问量:63

问:

我得到了一个过去的问题的慷慨帮助(请参阅:将标题添加到最后一列一次,并在最后一列的某些行中复选框),现在我正在添加它,我遇到了变量错误,无法弄清楚如何解决它。


代码的当前目标:

  1. 将数组与隐藏管理 ws 中的范围进行比较(范围是列表框选择 - 最多允许 4 个选择)

  2. 如果适用,则进行选择(并记录在隐藏的 ws 中),将比较

    表 1) 表的标题,以查看是否需要在表 1 的表末尾添加新的列标题(& bordering & 复选框)

    表 2) 单列中另一个表的(不同的 ws)行,以查看是否需要将新的行/行项目添加到表 2 的表 Img 中,并添加行

此外,如果用户返回源列表框并取消选择某个项目,则会影响 2 个表:

  Table 1) applicable table header and assoc. column & checkboxes removed 
  Table 2) applicable row/line item removed from table

问题:

添加到表格中效果很好,但是在“删除”过程中遇到了麻烦。自从得到帮助(添加到其中)以来,我已经更新了代码,但仍然使用推荐的结构。

我从 c 变量作为 Range 开始,但尝试了 Long/Variant,只是为了看看我会得到什么,看看我是否能理解正在发生的事情,但此时我什么都不理解。

各种变量错误:

错误 1) 对象变量或未设置 With Block 变量 - 当变量“c”为 Range 时

运行时错误位置

错误 2) 控制变量必须是 Variant 或 Object - 当变量“c”为 Long 时

错误 3) ByRef 参数类型不匹配 - 当变量“c”为 Variant 时


法典:

Sub IP_AO_Update()

    Const AO_COL As Long = 4
    Const HEADERS_ROW As Long = 6
    
    Dim srcWS As Worksheet
    Dim aWS As Worksheet
    Dim targetWS As Worksheet
    Dim SelTerm As Variant
    Dim mSel As Variant
    Dim c As Variant 'Long 'Range
    Dim bLR As Long
    Dim dLR As Long
    Dim arrSel As Variant
    Dim colD As Range
    Dim targetLR As Long
    Dim arrAddOns As Variant
    Dim term As Variant
    Dim hdr As Variant
    Dim mHdr As Variant
    Dim rngCB As Range
    
    Set wb = ThisWorkbook
    Set aWS = wb.ActiveSheet
    Set targetWS = wb.Sheets(aWS.Index + 1)
    Set admin = wb.Worksheets("Admin")
    Set SelRng = admin.Range("AF2:AF5")
    Set colD = targetWS.Range("D7:D10")
      
    With Application
        .ScreenUpdating = False
    End With
    
    arrAddOns = Array("Implant Add On", "High Cost Drug Add On", "Postpartum LARC Add On", "Renal Dialysis Add On")
    
    For Each term In arrAddOns
       
        'Apply [AO]:'
        hdr = HeaderText(term)
        mHdr = Application.Match(hdr, aWS.Rows(HEADERS_ROW), 0)
        
        If Not IsError(Application.Match(term, SelRng, 0)) Then
        
            If IsError(mHdr) Then
            
                ' + AO hdr          
                mHdr = aWS.Cells(HEADERS_ROW, Columns.Count).End(xlToLeft).Column + 1
                
                ' + AO col bordering
                With aWS.Cells(HEADERS_ROW, mHdr)
                    .Value = hdr
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlTop
                    .Borders(xlEdgeTop).LineStyle = xlContinuous
                    .Borders(xlEdgeTop).Weight = xlThin
                    .Borders(xlEdgeTop).ColorIndex = 15
                    .Borders(xlEdgeBottom).LineStyle = xlContinuous
                    .Borders(xlEdgeBottom).Weight = xlThin
                    .Borders(xlEdgeBottom).ColorIndex = 15
                    .Borders(xlEdgeRight).LineStyle = xlContinuous
                    .Borders(xlEdgeRight).Weight = xlThin
                    .Borders(xlEdgeRight).ColorIndex = 15
                    With Range(.Offset(1, 0), .Offset(22, 0))
                        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
                        .Borders(xlInsideHorizontal).Weight = xlThin
                        .Borders(xlInsideHorizontal).ColorIndex = 15
                        .Borders(xlEdgeRight).LineStyle = xlContinuous
                        .Borders(xlEdgeRight).Weight = xlThin
                        .Borders(xlEdgeRight).ColorIndex = 15
                        .Borders(xlEdgeBottom).LineStyle = xlContinuous
                        .Borders(xlEdgeBottom).Weight = xlThin
                        .Borders(xlEdgeBottom).ColorIndex = 15
                    End With
                End With
                
                ' + AO terms in AO ws
                mSel = targetWS.Cells(Rows.Count, AO_COL).End(xlUp).Row + 1
                                             
                With targetWS
                    .Cells(mSel, AO_COL).Value = term
                                    
                    If .Cells(mSel, AO_COL).Value <> "" Then
                        .Cells(mSel, 2).Value = "ADD ON"
                    End If
                End With
            End If
                  
            ' + cb
            For Each c In aWS.Range("B7:B" & aWS.Cells(Rows.Count, "B").End(xlUp).Row).Cells
                Set rngCB = c.EntireRow.Columns(mHdr)
                Set cb = CellCheckbox(rngCB)
                Debug.Print rngCB.Address, Not cb Is Nothing
                
                If cb Is Nothing Then AddCheckbox rngCB
            Next c
            
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Else
            If Not IsError(mHdr) Then
                For Each c In colD.EntireRow.Columns(mHdr).Cells 
                    On Error Resume Next
                    
                    ' - tbl AO hdr, cols, cb
                    CellCheckbox(c).Delete  'The c variable starts to error here when c = Range
                    c.ClearContents
                    c.Borders(xlEdgeTop).LineStyle = xlNone
                    c.Borders(xlEdgeBottom).LineStyle = xlNone
                    c.Borders(xlEdgeRight).LineStyle = xlNone
    
                    With Range(c.Offset(1, 0), c.Offset(22, 0))
                        c.Borders(xlEdgeRight).LineStyle = xlNone
                        c.Borders(xlInsideHorizontal).LineStyle = xlNone
                    End With
                    
                    On Error GoTo 0
                Next c
                
                aWS.Columns(mHdr).Delete
                
                ' - rows in AO ws
                With targetWS
                    For Each c In colD.EntireColumn.Rows(mSel).Cells
                        c.ClearContents
                    Next c
                    
                    .Rows(mSel).Delete
                    
                End With
            End If
        
        End If
    Next term
End Sub

编辑 1

误差 Img

编辑 2

结果的图片 - 剩余复选框

Excel VBA for 循环 复选框 variable-assignment

评论

0赞 GSerg 10/18/2023
收到带有正确声明变量的运行时错误后,您开始随机更改其类型,希望这是修复它的方法,可以理解地收到编译时错误?
0赞 StillLearningThisStuff 10/18/2023
我应该提到这一点,但这正是发生的事情!
0赞 GSerg 10/18/2023
因此,使变量保持原样,并开始寻找为什么在它(可能是正确的)时访问它。您的屏幕截图甚至没有显示运行时错误的位置。Nothing
0赞 StillLearningThisStuff 10/18/2023
我更新了帖子以包含错误点的 tan img。
0赞 Tim Williams 10/18/2023
你大概是想忽略该部分中的任何错误。因此,如果您在那里遇到运行时错误,可能是因为您将 VBA 错误处理选项设置为“在所有错误时中断”。On Error Resume Next

答:

0赞 Tim Williams 10/18/2023 #1

编辑 - 问题在这里:

For Each c In colD.EntireRow.Columns(mHdr).Cells

colD,但您的复选框位于不同的工作表上。targetWSaWS


与其使用,不如考虑添加一个 Sub,如下所示:On Error resume Next

'Delete any checkbox linked to cell `c`
Function DeleteLinkedCheckbox(c As Range)
    Dim cb As Object
    Set cb = CellCheckbox(c)
    If Not cb Is Nothing Then cb.Delete
End Function

然后代替这个:

On Error Resume Next
CellCheckbox(c).Delete  
c.ClearContents
'...
'...
On Error Got 0

您可以致电

DeleteLinkedCheckbox c
c.ClearContents
'etc

评论

0赞 StillLearningThisStuff 10/18/2023
实现了上面的代码,看起来它跳过了复选框删除部分?就像我按原样运行它一样(复选框删除 + ),并且只使用 .ClearContents/Delete(不包括复选框删除代码),结果相同:
0赞 StillLearningThisStuff 10/18/2023
我已经在帖子中包含了结果的图片!
0赞 Tim Williams 10/18/2023
如果它跳过了,那么你需要逐步浏览代码并找出哪一行是问题所在。如果没有工作簿,我很难进行故障排除,因此我可以在上下文中运行它。如果您能够分享它,请告诉我,我会在这里发送我的电子邮件。
0赞 StillLearningThisStuff 10/18/2023
如果你可以的话,我应该可以看!
0赞 StillLearningThisStuff 10/18/2023
知道了,谢谢!你一会儿应该会看到味精