Check to see if Worksheet Exists

  • Thread starter Thread starter Zeroman
  • Start date Start date
Z

Zeroman

Hi all,

I want to see if a worksheet called "Surplus" exists and if it doesn'
I want to create it.

Thanks for your help in advance.

Zeroma
 
dim oSheet as worksheet
set oSheet =Nothing
on error resume next
set oSheet = sheets("Surplus")
on error goto 0
if oSheet is Nothing then
Sheets.add
activesheet.name ="Surplus"
end if

Robert Flanagan
Macro Systems
Delaware, U.S. 302-234-9857
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel
 
try this


Public Sub t()

Dim Wkbk As Workbook
Dim wksht As Worksheet
Set Wkbk = ActiveWorkbook

Dim e As Integer
e = 0
For Each wksht In Wkbk.Worksheets
If wksht.Name = "Surplus" Then
e = 1
End If
Next
If e = 0 Then
Sheets.Add.Name = "Surplus"
End If

End Su
 
Hi Zeroman,

Try:

Sub Tester02()

On Error Resume Next
ActiveWorkbook.Sheets.Add.Name = "Sheet4"
On Error GoTo 0

End Sub

If the sheet does not exist, the obove creates it; if the sheet already
exists, the procedure errors silently.
 
The following code will do what you asked.

Sub TestExists()

Dim FoundIt As Boolean
Dim NS As Object

For Each c In ThisWorkbook.Sheets
If c.Name = "Surplus" Then FoundIt = True
Next

If FoundIt = False Then
Set NS = Sheets.Add
NS.Name = "Surplus"
End If

End Su
 
one way:


Dim wsSheet As Worksheet
On Error Resume Next
Set wsSheet = Worksheets("Surplus")
On Error GoTo 0
If wsSheet is Nothing Then _
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "Surplus"
 
Here is one way

Dim wb As String
Dim sFilename As String

sFilename = "C:\myTest\BobsFile.xls"
On Error Resume Next
wb = Dir(sFilename)
If wb = "" Then
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=sFilename
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
sorry, I gave code for a workbook. But you have plenty of other replies.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Bob Phillips said:
Here is one way

Dim wb As String
Dim sFilename As String

sFilename = "C:\myTest\BobsFile.xls"
On Error Resume Next
wb = Dir(sFilename)
If wb = "" Then
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=sFilename
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Zeroman said:
Hi all,

I want to see if a worksheet called "Surplus" exists and if it doesn't
I want to create it.

Thanks for your help in advance.

Zeroman
If your workbook has a reference to Microsoft Scripting Runtime

Sub qwerty1()
Dim x As Dictionary, ws As Worksheet
Set x = New Dictionary
For Each ws In Sheets
x.Add CStr(ws.Name), ws.Name
Next
If Not x.Exists("Surplus") Then _
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Surplus"
End Sub

Alan Beban
 
Wow 11 replies.

Thanks for all the help, I will get back to you and tell you if i
works
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top