简体   繁体   English

设置生成表单控制按钮的宏的按钮位置

[英]Setting Button Location for Macro that Generates Form Control Buttons

I currently have data that I am splitting into multiple sheets and allowing the user to select which sheet they run the macro on. 我目前有将数据拆分为多个工作表的数据,并允许用户选择要在其上运行宏的数据。 The work sheet is used as a filtration between multiple systems 该工作表用作多个系统之间的过滤

I have a named range (dayList) that groups the data into days, and creates a new sheet for each day (day1, day2, etc). 我有一个命名范围(dayList),将数据分组为几天,并为每一天(day1,day2等)创建一个新工作表。 Each sheet must then have another macro applied (screen data) that will filter the data in each of these sheets in exactly the same way. 然后,每个工作表都必须应用另一个宏(屏幕数据),该宏将以完全相同的方式过滤这些工作表中的每个数据。

I am trying to create a UI that will approximate the user being able to click on one of the cells in the named range to run the macro for that particular day. 我正在尝试创建一个UI,该UI将使用户能够单击指定范围内的某个单元格以在特定日期运行宏。 The code snippet im currently working with is below. 我目前正在使用的代码段如下。

 Sheets("LaunchScreen").Activate
    Cells(rowCounter, 6).Value = "Day" & dayCounter
    ActiveSheet.Buttons.Add(538.5, 56.25, 48.75, 13.5).Name = "Day" & dayCounter
    ActiveSheet.Buttons("Day" & dayCounter).Select
    Selection.OnAction = "JoinTransactionAndFMMS"

Im looping this to create a new button for each day then pass the button name to another macro as a parameter to find the worksheet in this workbook that shares the same name. 我将其循环以每天创建一个新按钮,然后将按钮名称作为参数传递给另一个宏,以在此工作簿中找到共享相同名称的工作表。

TLDR: I need to: TLDR:我需要:

  1. Set the location of a button using VBA, preferably matching location to a cell reference eg Range("A1").Button.Insert 使用VBA设置按钮的位置,最好将位置与单元格引用匹配,例如Range("A1").Button.Insert
  2. Pass a cell reference from a named range into a macro 将单元格引用从命名范围传递到宏

An excel noob in way over his head here. 一个出色的菜鸟在这里头顶。 Any help would be greatly appreciated! 任何帮助将不胜感激!

1) The .Add method expects 4 parameters. 1).Add方法需要4个参数。 The first two are TOP and LEFT positions to get the UPPER LEFT corner of the button, the 3rd and 4th are height and width settings for the button. 前两个是TOP和LEFT位置,用于获取按钮的UPPER LEFT角,第3个和第4个是按钮的高度和宽度设置。

So, if you know you want to add a button and the reference is to position it at cell C10 (a better example than A1), then this would do it: 因此,如果您知道要添加一个按钮,并且引用是将其放置在单元格C10(比A1更好的示例)中,则可以这样做:

ActiveSheet.Buttons.Add(Range("C10").Top, Range("C10").Left, 48.75, 13.5).Name = "Day" & dayCounter     

2) Your macro has to first be designed to accept a parameter being "passed" into it. 2)您的宏必须首先被设计为接受“传递”给它的参数。 So, something like 所以,像

Sub MyMacro(MyRange As Range)
    MsgBox MyRange.Address
End Sub

Now, your other macro can call MyMacro and you must pass in the parameter as it is called: 现在,您的另一个宏可以调用MyMacro,并且您必须传递被称为的参数:

Sub test2()
   Call MyMacro(Range("Animals"))
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM