【发布时间】:2018-07-03 13:36:32
【问题描述】:
我设法创建了一个表单,用户可以在其中扩展数据透视表的字段,一旦他们完全扩展了一个字段/分支,一个按钮将出现在 E 列中,并且该数据透视字段数据连接在J 列(有一些隐藏列)。
我想要用户单击 E 列中的自动生成按钮,该按钮将 J 列中的相应数据导出到工作簿某处的列表中。
我下面的代码会自动为完全展开的字段生成按钮,但我不知道如何编写代码以将每个按钮链接到 J 列中的相应单元格 - 这可能不是很困难,但我们将不胜感激。
Sub buttonGenerator()
Dim btn As Button
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete
Dim t As Range
Dim size As Integer
size = ActiveSheet.PivotTables("Pivottable1").TableRange2.Rows.Count
For i = 2 To size Step 1
If Not IsEmpty(ActiveSheet.Range(Cells(i, 4), Cells(i, 4))) Then
Set t = ActiveSheet.Range(Cells(i, 5), Cells(i, 5))
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
.OnAction = "btnS"
.Caption = "Add to summary" '& i
.Name = "Btn" & i
End With
End If
Next i
Application.ScreenUpdating = False
End Sub
Sub buttonAppCaller()
MsgBox Application.Caller
End Sub
所以这是我的代码 .. 它抛出运行时错误 1004“无法获取工作表类的 Buttons 属性”。不知道我做错了什么,但我需要从按钮旁边的单元格中获取数据,以便在单击该特定按钮时复制到工作表 2 中列表的底部。请帮忙!
Sub btnS()
Dim dest As Range
Dim origin As Range
origin = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(0, 1) 'input data from cell next to button click
dest = Worksheets("Form Output").Range("A1") 'output data to list in sheet 2 - "Form output"
Set dest = origin
End Sub
【问题讨论】: