I have 2 values in sheet1 which I want to migrate to sheet2. As of now my my CommandButton works fine till the point I create a newsheet. But as soon as I select my newsheet it gives me a subscript out of Range error. On commenting my last code of line the program runs fine.
Private Sub CommandButton1_Click()
Dim CustomerName As String, CustomerProblem As Integer, newSheet As Worksheet, newName As String
Do
newName = Application.InputBox("What do you want to name the new sheet?", Type:=2)
If newName = "False" Then Exit Sub: Rem cancel pressed
Set newSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(1))
On Error Resume Next
newSheet.Name = newName
newName = Error
On Error GoTo 0
If newName <> vbNullString Then
Application.DisplayAlerts = False
newSheet.Delete
Application.DisplayAlerts = True
MsgBox newName
End If
Loop Until newName = vbNullString
Worksheets("sheet1").Select
CustomerName = Range("C4")
CustomerProblem = Range("C5")
This line gives me the error.
Worksheets("newName").Select