Solved

Check to see if worksheet exists

Posted on 2011-09-28
4
213 Views
Last Modified: 2012-05-12
Sub CopyFunction()
    Dim lEndRow As Long, lStartRow As Long
    Dim num1 As Integer, num2 As Integer
    Dim wkb As Workbook
    Dim pivotSht As Worksheet, Finals As Worksheet, srcWks As Worksheet, wssheet As Worksheet
    Dim copyRng As Range, pasteRng As Range
    
    Set wssheet = Sheets("Summary Data")
    
    If Not wssheet Is Nothing Then
        Set wssheet = Sheets("Summary Data")
        Application.DisplayAlerts = False
        Worksheets("Summary Data").Delete
        Application.DisplayAlerts = True
    End If
      
    Set wkb = ThisWorkbook
    Set pivotSht = wkb.Sheets("Pivots")
         
    'put error checking in, to ensure sheet "Summary Data" doesn't already exist, or you'll get an error when you set the name to the new sheet
    Set Finals = wkb.Worksheets.Add(after:=wkb.Sheets(wkb.Sheets.Count))
    Finals.Name = "Summary Data"
      
    Finals.Range("A1:C1") = Split("ID,Sum of Debt,Sum of Credit", ",")
      
    '1st row starts from 3
    lStartRow = 3
      
    'last row = grand total and its not included
    lEndRow = (pivotSht.Range("A" & pivotSht.Rows.Count).End(xlUp).Row) - 1
      
    Set copyRng = pivotSht.Range("A" & lStartRow, pivotSht.Range("C" & lEndRow))
    Set pasteRng = Finals.Range("A2")
      
    'copy values only, not formats
    pasteRng.Resize(copyRng.Rows.Count, copyRng.Columns.Count).Value = copyRng.Value
      
    Finals.Columns.AutoFit
      
End Sub

Open in new window



Hi,

I have the above function which performs copying.

Now, before it performs this function, I have an error handler (failing one) which deletes if the tab exists already

    Set wssheet = Sheets("Summary Data")
    
    If Not wssheet Is Nothing Then
        Set wssheet = Sheets("Summary Data")
        Application.DisplayAlerts = False
        Worksheets("Summary Data").Delete
        Application.DisplayAlerts = True
    End If

Open in new window


But the problem is, I am alreadying setting the 'wssheet' parameter to the sheet. It crashes if the worksheet does not exist. It performs good, if the worksheet (Summary Data) exists.

Could you please help me to check 'if worksheet exists' on the if statement itself?

Thanks!
0
Comment
Question by:Shanan212
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36719554
On Error Resume Next
Set wssheet = Sheets("Summary Data")
On Error Goto 0

Open in new window


in its simplest form.
0
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 36719715
Thanks!

I can understand what 'On error resume next' means (it skips the error and goes to next line)

But what does 'On error goto 0' does? (goes nowhere?)
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36719790
It just turns off the On Error Resume Next so that errors are reported again.
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36719915
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question