VBA代码链接checkbox到某些列

我有三列E(数量不足)F(太慢)和G(未列出)他们都有checkbox。 我需要链接E到H F到I G到J

下面的代码很好地工作,如果只有1列的checkbox,但我不知道如何改善代码运行在某一列的checkbox。 现在它只是search整个工作表的checkbox,并将其链接到所需的列。

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 

编辑

好的…让我们再试一次:

由于Check Box对象没有它所在单元格的单元格信息,我们将不得不更加创造性地使用Offset属性。

由于我们知道每行有3个checkbox,我们可以findcheckbox的总数并除以3来确定有多less行。

然后通过将Range设置为列“E”顶部的单个单元格,可以使用该单元格的offset属性。

假设您将checkbox依次放在工作表下方的列“E”,然后下一栏“F”,然后下“G”,一旦到达每列的最后一行,我们就可以重置偏移量。 (如果按行顺序将checkbox放在表单上,​​则必须反转循环逻辑。)(如果将checkbox随机放置,那么运气不好,将不得不手动设置链接的单元格。 )

 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 

只要你的checkbox放在每一列的表单上,上面的程序应该为每个框find正确的链接单元格。

如果他们被放置在一个不同的顺序,那么至less这个代码会告诉你如何设置一个初始的Range单元格,以及如何引用具有偏移量的其他单元格。

希望这个代码或这些想法的组合将帮助你解决你的问题。 🙂