有没有办法将一个表单button同步到一个单元格中,并为其分配另一个子表单

问题已经解决了如何使多个button引用不同的相邻偏移单元格

Sub Tester()Dim btn As Object Dim rng As Range Dim i As Long Dim lastRow As Long

lastRow = 99999 'Modify as needed this will be the last possible row to add a button For i = 2 To lastRow Step 4 Set rng = ActiveSheet.Cells(i, 21) 'Column 2, row i '## Create the button object and assign a variable to represent it Set btn = ActiveSheet.Buttons.Add(1, 1, 1, 1) '## use the btn variable to manipulate the button: With btn .Top = rng.Top .Left = rng.Left .width = rng.width .height = rng.RowHeight .OnAction = "offsetRelative" .Caption = "Close" End With Next End Sub Sub offsetrelative() Dim rowNumber As Long '## Get the row number of the button: rowNumber = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row Application.ScreenUpdating = False Sheets("Sheet6").Range("AD" & CStr(rowNumber)).Value = 1 Application.ScreenUpdating = True End Sub 

上面的解决scheme效果很好,但是每当我点击一个button时,计算起来会有点慢。 我需要它是瞬间的,但它挂了5秒钟…有什么办法可以避免这种情况?

你在正确的轨道上。 我将使用您logging的macros中的第一行,并与您所做的Tester整合。 然后把它放在一个循环中,以创build尽可能多的button。

 Sub Tester() Dim btn as Object Dim rng As Range Dim i as Long Dim lastRow As Long lastRow = 100 'Modify as needed this will be the last possible row to add a button For i = 3 to lastRow Step 4 Set rng = ActiveSheet.Cells(i, 2) 'Column 2, row i '## Create the button object and assign a variable to represent it Set btn = ActiveSheet.Buttons.Add(1, 1, 1, 1) '## use the btn variable to manipulate the button: With btn .Top = rng.Top .Left = rng.Left .Width = rng.Width .Height = rng.RowHeight .onAction = "offsetRelative" End With Next End Sub 

我修改你的offsetRelative函数来使用Application.Caller来确定button所在的单元格/范围,并从中得到行号。 然后,而不是像“AE3”范围内的硬编码,我们可以结合列“AE”和行号:

 Sub offsetrelative() Dim rowNumber as Long '## Get the row number of the button: rowNumber = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row Application.ScreenUpdating = False Sheets("sheet2").Range("AE" & Cstr(rowNumber)).Value = 1 Application.Wait (Now + 0.000001) Sheets("sheet2").Range("AE" & Cstr(rowNumber)).Value = 0 Application.ScreenUpdating = True End Sub 

你似乎已经知道Buttons.Add()返回新的button,可以存储在一个variables,并使用它获得Shapes("Button 24") ,但我想我应该提及它的完整性。

为了对每个button执行不同的操作,可以为它们分配所有相同的子例程,并编写一个使用ActiveSheet.Buttons(Application.Caller).TopLeftCell子例程来确定button所在的单元格,并决定要执行什么操作基于单元格的地址。

您可以使用.Offset(x, y)属性相对于已知单元格相对地处理单元格,或者使用例如.Row而不是使用Sheet.Range来避免必须dynamic计算范围,如果每个button所在的区域布局相同办法。

 Sub offsetrelative() Dim myCell as Range Dim myRow as Long myCell = ActiveSheet.Buttons(Application.Caller).TopLeftCell myRow = myCell.Row - 2 ' D9 = 7 Application.ScreenUpdating = False Sheets("sheet2").Range("AE" & CStr(row)).Value = 1 Application.Wait (Now + 0.000001) Sheets("sheet2").Range("AE" & CStr(row)).Value = 0 Application.ScreenUpdating = True End Sub 

你可以尝试的另一件事是编码button名称中的行号。 所以你可以在创buildbutton时设置.Name = "Button_Row" & CStr(i) ,然后根本不使用myCell ,在offsetrelative函数myRow = CLng(Mid(Application.Caller, 11))使用myRow = CLng(Mid(Application.Caller, 11)) 。 11是数字的第一个字符位置,这取决于你的前缀的长度,在这个例子中是“Button_Row”[10个字符长]。