I have a form which has 3 text boxes and a button. Button has a "Click" event handler with the following code:
Edit: following the advice by Darren Bartrup-Cook, I have removed all Select and Active calls, but that didn't help.
Sheets("__TEMPLATE").Copy After:=Sheets(Sheets.Count)
Dim ws As Worksheet
Dim size As Integer
Set ws = Sheets(Sheets.Count)
ws.Range("B1:B1").FormulaR1C1 = Me.tbName
size = Me.tbSize
Dim cSize As Range
Set cSize = ws.Range("B2:B2")
cSize.Formula = Me.tbSize
cSize.NumberFormat = "General"
cSize.Value = cSize.Value
If Me.tbIndex.TextLength > 0 Then
ws.Range("B3:B3").FormulaR1C1 = Me.tbIndex
End If
ActiveWindow.ScrollRow = 1
ws.Name = Me.tbName
ws.Range("A" & (size + 6) & ":G1005").Delete
Unload Me
The intended action is to copy a template sheet and adjust the table size using the number from the second text box (there's no real table, just a range of table-like formatted rows). Also it copies the text box values to the cells B2 and B3.
The issue is that it does not work every time; sometimes it just copies the template and stops; other time it may throw an error message pointing out the Worksheets().Select method;
Sheets().Select and Workbook().Worksheets().Select notations yield the same result.
Could anyone let me know what is the right way to reference a sheet from a form method's code?
Thank you!