Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Help with some code please

I have the following code:

With Worksheets("mysheet")
If Not IsEmpty(.Range("A2").Value) Then
'do stuff
End If
End With

My problem is that mysheet is not always a sheet in the file. How can I get
Excel to miss out the code above if mysheet does not exist in the file?

Thanks in advance.

Gareth


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 96
Default Help with some code please

Hi try this

Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name = "mysheet" Then
With Worksheets("mysheet")
If Not IsEmpty(.Range("A2").Value) Then
'do stuff
End If
End With
End If
Next sh

libby
-----Original Message-----
I have the following code:

With Worksheets("mysheet")
If Not IsEmpty(.Range("A2").Value) Then
'do stuff
End If
End With

My problem is that mysheet is not always a sheet in the

file. How can I get
Excel to miss out the code above if mysheet does not

exist in the file?

Thanks in advance.

Gareth


.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Help with some code please

dim testWks as worksheet

set testWks = nothing
on error resume next
set testwks = worksheets("mysheet")
on error goto 0

if testwks is nothing then
'not there
else
if not isempt(....


would be one way. If you do it lots, instead of putting all that inline code in
your macro, you could call a function:

This is one that I've saved from Chip Pearson:


Function WorksheetExists(SheetName As String, _
Optional WhichBook As Workbook) As Boolean
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = Len(WB.Worksheets(SheetName).Name) 0
End Function

You can then call this function in code as follows:

If WorksheetExists("Sheet123") = True Then
' sheet exists
Else
' sheet does not exist
End If



Gareth wrote:

I have the following code:

With Worksheets("mysheet")
If Not IsEmpty(.Range("A2").Value) Then
'do stuff
End If
End With

My problem is that mysheet is not always a sheet in the file. How can I get
Excel to miss out the code above if mysheet does not exist in the file?

Thanks in advance.

Gareth


--

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
split post code (zip code) out of cell that includes full address Concord Excel Discussion (Misc queries) 4 October 15th 09 06:59 PM
Code to conditional format all black after date specified in code? wx4usa Excel Discussion (Misc queries) 3 December 26th 08 07:06 PM
Drop Down/List w/Code and Definition, only code entered when selec Spiritdancer Excel Worksheet Functions 2 November 2nd 07 03:57 AM
Convert a Number Code to a Text Code Traye Excel Discussion (Misc queries) 3 April 6th 07 09:54 PM
copying vba code to a standard code module 1vagrowr Excel Discussion (Misc queries) 2 November 23rd 05 04:00 PM


All times are GMT +1. The time now is 03:07 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"