LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA won't copy Worksheets after sheet Deletion

So, I've written some code that reads through a list of sheets, and in
the event a given sheet can NOT be found in the workbook, the code
copies a "Template" sheet and names it after the missing sheet.

The code runs beautifully the first time. Any time there is a missing
sheet on the list, the code copies the "Template" sheet and renames
it, accordingly.

Where the code runs into problems is upon sheet deletion. If I select
a range of sheets (manually) and delete them from the workbook, the
code is unable to access the "Copy Worksheets" method upon re-run.

I have seen others post about this bug in Excel and am wondering if
anyone knows/has a work-around.

Here is the code. It is a bit complicated as it uses Global
Variables, but hopefully the names make sense. Please ask questions
or offer advice. Thanks, Thaddeus

--------------------


Option Explicit
Option Base 1 ''set the first array address to 1 instead
of zero.

Sub ADD_TEMPLATE_SHEETS()
'
' Written 5/25/2007 by tagar
'

Dim cntsheet As Long '''The count of sheets in the book at the
start of sub.
Dim cntsheetplus As Variant '''A counter on the increment of
sheets, used to add new sheets to the end of the book.
Dim nmSheet As Variant '''used to iterate through cntsheets
Dim nmAddSheet As Variant '''the name of the sheet to check and add
Dim addTarget As Variant '''target boolean to determine if sheet
exists
Dim i As Long '''Page counter for lngRowMax
Dim j As Long '''Page counter for lngColMax
Dim lngRowCount As Long '''the counter column number
Dim lngColCount As Long '''the actual column number
Dim sheetArray() As Variant '''
Dim arrayIndex As Long


'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
'''
'''Get Global Variables!!
'''
PROJECT_GLOBALS.GET_GLOBALS

Workbooks(nmWorkbook).Activate

cntsheet = Workbooks(nmWorkbook).Worksheets.Count '''Get the count of
worksheets in the workbook
cntsheetplus = cntsheet '''assign the initial
value to cntsheetsplus


''''enter all sheet names into an array
ReDim sheetArray(1 To cntsheet + 10)
For nmSheet = 1 To cntsheet
sheetArray(nmSheet) = Sheets(nmSheet).Name
Next nmSheet

lngColCount = Worksheets(nmConsoleSheet).Cells(24,
6).End(xlToRight).Column - 5
lngRowCount = Worksheets(nmConsoleSheet).Cells(24, 5).End(xlDown).Row
- 24


For i = 1 To lngRowCount '''for each sheet to add (row)
For j = 1 To lngColCount '''for each data type (column)
addTarget = 0
'''Display Work Update Status
Sheets(nmConsoleSheet).Activate
Sheets(nmConsoleSheet).Cells(19, 4).Value = "Checking " & nmAddSheet
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.DisplayAlerts = False
Application.ScreenUpdating = False

For arrayIndex = 1 To cntsheet '''check to see if sheet already exists
in workbook.


'''Assemble the name of the sheet to check/add if missing
If arrayIndex = 1 Then
nmAddSheet =
Workbooks(nmWorkbook).Sheets(nmConsoleSheet).Cells (i + 24, 5).Value &
"-" & Sheets(nmConsoleSheet).Cells(24, j + 5).Value
End If

If sheetArray(arrayIndex) = nmAddSheet Then
addTarget = 1 ''''Get the name if sheet exists
arrayIndex = cntsheet
'MsgBox ("Do Nothing")
End If


Next arrayIndex '''check all sheets to see if nmAddSheet
already exists. If yes, do nothing.


If addTarget = 0 Then
Workbooks(nmWorkbook).Sheets(nmTemplateSheet).Copy
After:=Workbooks(nmWorkbook).Sheets(Sheets.Count) '(cntsheetplus)
Workbooks(nmWorkbook).Sheets(nmTemplateSheet &
" (2)").Name = nmAddSheet
cntsheetplus = cntsheetplus + 1 '''since the sheet
was added, increment cntsheetsplus so the next sheet will still be
added to the end of the workbook.
End If

Next j '''continue for each Data Type (columns)
Next i '''continue for all sheets to be checked/added (rows)



'''Display Work Update Status
Sheets(nmConsoleSheet).Activate
Sheets(nmConsoleSheet).Cells(19, 4).Value = "Complete"
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

 
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
Urgent:Find match between two worksheets and copy to another sheet guru Excel Worksheet Functions 1 September 21st 09 07:41 PM
Protect individual worksheets from deletion ejm3 Excel Programming 3 May 8th 07 10:16 AM
How do I link cells, sheet to sheet, to recognize row deletion Max Excel Programming 5 February 27th 06 10:27 PM
Prevent Sheet Deletion T-®ex[_60_] Excel Programming 7 September 7th 05 05:17 AM
Sheet deletion mitch Excel Discussion (Misc queries) 4 August 19th 05 12:10 AM


All times are GMT +1. The time now is 04:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"