我有三列E(QTY不足)F(太慢)和G(未列出)它们都有复选框。我需要链接 E到H. F到我 G到J
如果只有一列复选框,但我不知道如何通过某列中的复选框改进代码来运行以下代码。现在,它只搜索整个工作表中的复选框,并将它们链接到所需的列。
Sub LinkChecks()
'Update 20150310
i = 2
For Each cb In ActiveSheet.CheckBoxes
cb.LinkedCell = Cells(i, "I").Address
i = i + 1
Next cb
End Sub
答案 0 :(得分:0)
修改强>
好的......让我们再试一次:
由于Check Box对象没有其所在单元格的单元格信息,因此我们必须更具创造性地使用Offset
属性。
由于我们知道每行有3个复选框,我们可以找到复选框的总数并除以3以找出有多少行。
然后通过将Range设置为“E”列顶部的单个单元格,可以在该单元格上使用offset属性。
假设您将复选框按顺序放在列表“E”上,然后向下放置“F”列,然后按下“G”,我们可以在到达每列的最后一行后重置偏移。 (如果按行顺序放置工作表上的复选框,则必须反转循环逻辑。)(如果随机放置复选框,则运气不佳,必须手动设置链接的单元格。 )
Sub LinkChecks()
Dim rng As Range
Dim strColumn As String
Dim i As Integer
Dim intCount As Integer
Dim intRowCnt As Integer
Dim intRowOffset As Integer
Dim intColumnOffset As Integer
Dim dCnt As Double
i = 1 ' Your initial row offset
intCount = 0 ' A counter for total number of check boxes
intRowCnt = 0 ' A Row counter to find last row
intRowOffset = i ' Current Row offset from initial rng cell
intColumnOffset = 3 ' Current Column Offset (3 over from first check box column)
strColumn = "E" ' Set a starting Column of your first check box
Set rng = ActiveSheet.Cells(1, strColumn) ' Set initial rng cell
' Count how many check boxes are on the active sheet
For Each cb In ActiveSheet.CheckBoxes
intCount = intCount + 1
Next cb
' Since you know you have 3 check boxes per row,
' you can divide by 3 to get your row count
dCnt = intCount / 3
' *** Put test for remainder problems here ***
For Each cb In ActiveSheet.CheckBoxes
cb.LinkedCell = rng.Offset(intRowOffset, intColumnOffset).Address
intRowOffset = intRowOffset + 1
' Increment your row count until you get to last row
intRowCnt = intRowCnt + 1
If intRowCnt >= dCnt Then
intRowCnt = 0 ' Reset you row counter
intColumnOffset = intColumnOffset + 1 ' Increment Offset to the next column
intRowOffset = i ' Reset Row offset back to top row
End If
Next cb
End Sub
只要您的复选框放在每列的工作表上,上面的程序就会为每个框找到正确的链接单元格。
如果它们以不同的顺序放置,那么至少此代码将向您展示如何设置初始Range单元格以及如何引用具有偏移量的其他单元格。
希望此代码或这些想法的组合可以帮助您解决问题。 :)