View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rob van Gelder[_4_] Rob van Gelder[_4_] is offline
external usenet poster
 
Posts: 1,236
Default if statement for sheet not found in workbook

Sub test()
Dim ws As Worksheet, tosheet As String

tosheet = "this is a test"

With ThisWorkbook.Worksheets
On Error Resume Next
Set ws = .Item(tosheet)
On Error GoTo 0
If ws Is Nothing Then
Set ws = .Add(After:=.Item(.Count))
ws.Name = tosheet
End If
End With
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"tango" wrote in message
om...
dear all,
i want to add new worksheet if the worksheet name is not found in the
workbook when user input in the inputbox.

tosheet is the variable input by user.

** this if statement is incorrect. how should i write the statement?
If Worksheets(tosheet).Name not found in the workbook then

** this line is correct.
Set ws =
ThisWorkbook.Worksheets.Add(after:=ThisWorkbook.Sh eets(ThisWorkbook.Sheets.Count))

End If


thanks alot