提问人:StillLearningThisStuff 提问时间:10/18/2023 最后编辑:StillLearningThisStuff 更新时间:10/19/2023 访问量:63
VBA - 删除列/复选框/行期间出现未知变量错误(上一个工作代码)
VBA - Unknown variable error during column/checkbox/row removal (prev working code)
问:
我得到了一个过去的问题的慷慨帮助(请参阅:将标题添加到最后一列一次,并在最后一列的某些行中复选框),现在我正在添加它,我遇到了变量错误,无法弄清楚如何解决它。
代码的当前目标:
将数组与隐藏管理 ws 中的范围进行比较(范围是列表框选择 - 最多允许 4 个选择)
如果适用,则进行选择(并记录在隐藏的 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
编辑 2
答:
编辑 - 问题在这里:
For Each c In colD.EntireRow.Columns(mHdr).Cells
colD
,但您的复选框位于不同的工作表上。targetWS
aWS
与其使用,不如考虑添加一个 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
评论
下一个:VBA 到目标查找与循环
评论
Nothing
On Error Resume Next