I'm new to Excel-VBA so please excuse my ignorance. ;)
I'm actually trying to run a program on the selected sheet from a UserForm. The program would then get a value from the selected sheet which would import it to a newly created sheet. Sheet18 is a sheet template it will be copied to the new sheet.
This are the codes:
Private Sub CommandButton1_Click()
UserForm1.Hide
Dim wsUserSel As Worksheet
Dim ws As Worksheet
Dim ExFund As Variant
On Error Resume Next
Set wsUserSel = ListBox1.Value
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
On Error GoTo 0
With wsUserSel
ExFund = .Cells(107, 4).Value
End With
Sheet18.Range("A1:K126").Copy
ws.Name = InputBox("Please input the name of new worksheet.", ThisWorkbook.Name)
ws.Activate
Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Cells(29, 2) = ExFund
ActiveSheet.Cells.EntireColumn.AutoFit
Application.CutCopyMode = False
End Sub
This code returns an error
Run Time Error "91": Object Variable or With block variable not set
because of this part of the code:
With wsUserSel
ExFund = .Cells(107, 4).Value
End With
I have tried putting it inside With or using wsUserSel directly but I can't seem to get it working.
What am I doing wrong?