我有三列E(QTY不足(F(太慢(和G(未列出(它们都有复选框。我需要链接e到hF到我g至j
如果只有1列的复选框,则以下代码可以很好地工作,但我不知道如何改进特定列中通过复选框运行的代码。现在,它只是搜索整个表格以获取复选框并将其链接到所需的列。
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
edit
好吧...让我们再试一次:
由于复选框对象没有其所在的单元格的单元格信息,因此我们将不得不更具创造性地使用Offset
属性。
由于我们知道每行有3个复选框,因此我们可以找到复选框的总数,然后除以3,以找出有多少行。
然后,通过将范围设置为" E"列顶部的单个单元格,您可以使用该单元格上的偏移属性。
假设您将复选框依次放置在表向下列" 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
只要您的复选框放在每列的纸板上,上面程序应为每个框找到正确的链接单元格。
如果它们以不同的顺序放置,则至少此代码向您展示了如何设置初始范围单元格以及如何用偏移引用其他单元格。
希望此代码或这些想法的组合将帮助您解决问题。:(