Checking for existing worksheet name

  • Thread starter Thread starter KtM
  • Start date Start date
K

KtM

Morning!
I am doing a little program that would fetch information to a set of
sheets, which works OK now.
Problem is with checking if the data has been previously fetched, then
it should overwrite existing data instead of creating a new set.
How do you check if exists stuff in excel.

Basic idea.
If exists (name) then
write to name
else
new sheet with (name)
 
I found this UDF in the forum, I forgot the name of the author, sorry!

Public Function WSExist(wsname As String) As Boolean
'returns true if worksheet exists in the active workbook
Dim objWorksheet As Object
On Error Resume Next
WSExist = False
Set objWorksheet = ActiveWorkbook.Sheets(wsname )
If Err = 0 Then WSExist = True
End Function


Regards,
Stefi

„KtM†ezt írta:
 
Here is a sheet check function that you can incorporate

'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

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