宏Excel:将宏指定给Userform

时间:2017-03-23 03:28:03

标签: excel vba excel-vba

我有一个编码来计算x和y的数量(它实际上是列和行)。如何将编码插入UserForm以使其工作?然后,在我键入每个x和y的数据后,按“确定”,我希望输出显示在我的工作表上。 “Ok”部分使用什么编码?而对于“取消”部分也是如此。我还是VBA代码的新手。

我的代码:

Date/Time:       2017-03-23T01:58:34Z
Launch Time:     2017-03-23T01:55:34Z
OS Version:      Mac OS X 10.2 (16D32)
Report Version:  104

Exception Type:  SIGABRT
Exception Codes: #0 at 0x11a944dd6
Crashed Thread:  0

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'

Last Exception Backtrace:
0   CoreFoundation                       0x0000000118af9d33 0x118a03000 + 1010995
1   libobjc.A.dylib                      0x0000000119a4421e 0x119a3f000 + 21022
2   CoreFoundation                       0x0000000118b632b5 0x118a03000 + 1442485
3   Foundation                           0x00000001105a2624 0x11058c000 + 91684
4   MyApp.iOS                         0x000000010dcd29a0 0x10d9f1000 + 3021216
5   MyApp.iOS                         0x000000010dcdc73a 0x10d9f1000 + 3061562
6   MyApp.iOS                         0x000000010dcd568a 0x10d9f1000 + 3032714
7   MyApp.iOS                         0x000000010dcdbdbd 0x10d9f1000 + 3059133
8   MyApp.iOS                         0x000000010dcdd171 0x10d9f1000 + 3064177
9   UIKit                                0x000000010eacff71 0x10e96e000 + 1449841
10  MyApp.iOS                         0x000000010dcdd2f9 0x10d9f1000 + 3064569
11  ???                                  0x00000001321d76ae 0x0 + 0

我有一个UserForm,如下所示。

UserForm

1 个答案:

答案 0 :(得分:0)

您可以在子程序中添加参数。

Sub InsertShapeRange(my_col As Integer, my_row As Integer)

然后您需要删除这些行,因为变量现在在Sub定义中定义:

Dim my_row As Integer
Dim my_col As Integer

my_col = Application.InputBox("No. of x dies?", "Wafer Map", Default:=0)
my_row = Application.InputBox("No. of y dies?", "Wafer Map", Default:=0)

现在,通过右键单击按钮并选择“查看代码”,单击“确定”按钮的代码。

Private Sub cmdOK_Click()
    InsertShapeRange txtXDies, txtYDies
    Me.Hide
End Sub

您可能还想将OK按钮的Default属性设置为True,以便按Enter键与单击它相同。确保Cancel属性设置为False。

OK Button Default property = True

对于“取消”按钮,请输入以下代码以隐藏表单。

Private Sub cmdCancel_Click()
    Me.Hide
End Sub

最后将Cancel按钮的Cancel属性设置为True。这使得按Escape键相当于单击它。同时将Default设置为False。

Cancel Property of Cancel Button = True

完成的表格代码模块如下所示:

Option Explicit

Private Sub cmdCancel_Click()
    Me.Hide
End Sub

Private Sub cmdOK_Click()
    InsertShapeRange txtXDies, txtYDies
    Me.Hide
End Sub

完成的InsertShapeRange子例程现在看起来像这样。

Sub InsertShapeRange(my_col As Integer, my_row As Integer)

    Dim Rng As Range
    Dim shp As Shape
    Dim ws As Worksheet

    Set Rng = Selection
    Set Rng = Rng.Resize(my_row, my_col)
    Set ws = Rng.Parent
    Set shp = ws.Shapes.AddShape(msoShapeRectangle, Rng.Left, Rng.Top, Rng.Width, Rng.Height)
    With shp
        .Fill.Visible = msoFalse
        With .Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(0, 0, 0)
            .Transparency = 0
        End With
    End With
    With Rng
        .Borders(xlInsideHorizontal).LineStyle = xlContinuous
        .Borders(xlInsideVertical).LineStyle = xlContinuous
    End With

End Sub

在我的例子中,文本框被命名为" txtXDies"和" txtYDies",OK按钮" cmdOK"和取消按钮" cmdCancel"。您可以在“属性”窗口中检查名称或重命名它们 - 按F4并单击对象。