Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default if statement for sheet not found in workbook

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default if statement for sheet not found in workbook

One way:

Option Explicit
Sub testme()

Dim ToSheet As Worksheet
Dim ToSheetName As String

ToSheetName = InputBox(prompt:="what's the name")
If Trim(ToSheetName) = "" Then
Exit Sub '?
End If

Set ToSheet = Nothing
On Error Resume Next
Set ToSheet = Worksheets(ToSheetName)
On Error GoTo 0

If ToSheet Is Nothing Then
With ThisWorkbook
.Worksheets.Add after:=.Sheets(.Sheets.Count)
End With
Set ToSheet = ActiveSheet
On Error Resume Next
ToSheet.Name = ToSheetName
If Err.Number < 0 Then
MsgBox "Please rename " & ToSheet.Name & " manually!"
Err.Clear
End If
End If
End Sub


tango wrote:

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


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete values in sheet 2 that arre found in sheet 1 np Excel Discussion (Misc queries) 1 December 10th 09 07:21 PM
If ID # on one sheet isn't found on another... Steve Excel Worksheet Functions 4 October 4th 08 02:57 PM
how can I format sheet 1 if the value is found in sheet 2? Samad New Users to Excel 2 July 30th 06 12:05 AM
Do same thing to each workbook found in a folder KENNY Excel Programming 4 October 8th 04 03:03 PM
Jump to next sheet if not found Zurn[_8_] Excel Programming 1 September 28th 04 12:15 PM


All times are GMT +1. The time now is 08:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"