I have a coding to calculate No. of x and y dies (it's actually column and row). How do I insert the coding into the UserForm to make it works? Then, after I key in the data for each x and y dies and press "Ok", I want the output to appear on my worksheet. What coding do I use for the "Ok" part? And for the "Cancel" part as well. I'm still new to VBA code.
My code:
Sub InsertShapeRange()
Dim my_row As Integer
Dim my_col As Integer
Dim Rng As Range
Dim shp As Shape
Dim ws As Worksheet
my_col = Application.InputBox("No. of x dies?", "Wafer Map", Default:=0)
my_row = Application.InputBox("No. of y dies?", "Wafer Map", Default:=0)
Set Rng = Selection
Set Rng = Rng.Resize(my_row, my_col)
Set ws = Rng.Parent
Set shp = ws.Shapes.AddShape(1, 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
I have a UserForm as shown below.
You could add parameters to your subroutine.
Sub InsertShapeRange(my_col As Integer, my_row As Integer)
You would then need to delete these lines, as the variables are now defined in the Sub definition :
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)
Now add code for your Ok button when clicked by right clicking on the button and selecting View Code.
Private Sub cmdOK_Click()
InsertShapeRange txtXDies, txtYDies
Me.Hide
End Sub
You might also want to set the Default property of the OK Button to True so that pressing the Enter Key is the same as clicking it. Ensure that the Cancel property is set to False.
OK Button Default property = True
For the Cancel Button, enter the following code to hide the form.
Private Sub cmdCancel_Click()
Me.Hide
End Sub
Finally set the Cancel property of the Cancel button to True like so. This makes pressing the Escape key equivalent to clicking it. Also set Default to False.
Cancel Property of Cancel Button = True
The completed Form Code module will look like this:
Option Explicit
Private Sub cmdCancel_Click()
Me.Hide
End Sub
Private Sub cmdOK_Click()
InsertShapeRange txtXDies, txtYDies
Me.Hide
End Sub
The completed InsertShapeRange subroutine will now look like this.
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
In my example the Text Boxes are named "txtXDies" and "txtYDies", the OK Button "cmdOK", and Cancel Button "cmdCancel". You can check the names or rename them in the Properties Window - press F4 and click on the object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.