title: Macro Excel: Assign macro to Userform
body: <p>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 &quot;Ok&quot;, I want the output to appear on my worksheet. What coding do I use for the &quot;Ok&quot; part? And for the &quot;Cancel&quot; part as well. I'm still new to VBA code.</p>
  <p>My code:</p>
  <pre><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(&quot;No. of x dies?&quot;, &quot;Wafer Map&quot;, Default:=0)
  my_row = Application.InputBox(&quot;No. of y dies?&quot;, &quot;Wafer Map&quot;, 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
  </code></pre>
  <p>I have a UserForm as shown below.</p>
  <p><a href="https://i.stack.imgur.com/Un9Wd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Un9Wd.jpg" alt="UserForm" /></a></p>