vba test if sheet exsists

bensonsearch

Well-known Member
Joined
May 26, 2011
Messages
844
Hi all,

i have the below code which runs a report. but its based on months, the sheets are named by the months and are added automatically if the month doesnt exsist. the problem i need to capture is if they select a month that isnt in the workbook it tells then so and exits sub. but i cant figure it out. below is the code in which I need this

Code:
Dim myWorksheet As Worksheet
    Dim myWorksheetName As String
    Dim count As Long
    Dim temp As String
    Dim count2 As Long
    
    If ComboBox1.value = "" Or ComboBox2.value = "" Then
    MsgBox ("Please select Month or Branch")
    Exit Sub
    End If
    
       temp = ComboBox2.value
    myWorksheetName = ComboBox1.value
    
        'need to test that myworksheetname exsists <--------
 

    count = Sheets(myWorksheetName).Range("A1").CurrentRegion.Rows.count
    
    Worksheets(myWorksheetName).Range("A1:P" & count).AutoFilter _
    field:=4, Criteria1:=temp
    
  
     
     Worksheets("Sheet1").Range("A1:P2").Copy _
    Destination:=Worksheets("Sheet3").Range("A1")
    
     Worksheets(myWorksheetName).Range("A3:M" & count).Copy _
    Destination:=Worksheets("Sheet3").Range("A3")
    
    count2 = Sheets("Sheet3").Range("A1").CurrentRegion.Rows.count
    count23 = count2 + 1
     Sheets("Sheet3").Range("K" & count23).value = "Total"
     Sheets("Sheet3").Range("L" & count23).value = "=SUM(L3:L" & count2 & ")"
    Sheets("Sheet3").Range("M" & count23).value = "=SUM(M3:M" & count2 & ")"
    
    With Sheets("Sheet3").PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
.PrintArea = ""
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0.92)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = True
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
   
   
    Sheets("Sheet3").Range("A1:M" & count23).PrintOut
    
     Worksheets(myWorksheetName).AutoFilterMode = False
    Sheets("Sheet3").Range("A1:P" & count23).value = ""
     ThisWorkbook.Save

thanx in advance
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
Code:
Function SheetExists(WSName As String) As Boolean
Dim WS As Worksheet
    
On Error Resume Next
Set WS = Worksheets(WSName)
    If Not WS Is Nothing Then SheetExists = True
End Function
 
Upvote 0
The function works just like any function and you'll have to pass it the sheetname:
Code:
If SheetExists(ComboBox1.Value) = True Then
    'The sheet exists
Else
    'The sheet doesn't exist
End If
 
Upvote 0

Forum statistics

Threads
1,225,678
Messages
6,186,401
Members
453,352
Latest member
OrionF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top