I am getting an error Subscript out of range (Error 9) in vba. I cant find the problem and i searched it everywhere. My code is asking user for a workbook name then asking for select a worksheet, after user select the sheet. It will copy and paste between two sheets. Can someone please help me and here is my code and if you run it then you will see the error thanks
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function
Sub Button1_Click()
Dim shname As String
Dim wb As String
wb = Application.GetOpenFilename
If wb <> "False" Then Workbooks.Open wb
Do Until WorksheetExists(shname)
shname = InputBox("Enter sheet name")
If Not WorksheetExists(shname) Then
MsgBox shname & " doesn't exist!", vbExclamation
Else
WSName = shname
Sheets(shname).Select
ActiveWorkbook.Worksheets("Sheet1").Cells(1, 1) = Workbooks(Dir(wb)).Worksheets(shname).Cells(1, 1)
End If
Loop
End Sub