Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default undoing the for next procedure


Guys,

My macro creates 66 new wsheets and names them. How do I undo the sub?
I mean if i wanted to modify code and include other commands withing
the For Next Procedure, but dont want to create another 66 new sheets
and name them all over again but would like to undo it and repeat the
procedure with my revised code.

Thanks so much!!!!

Regards,
Maria


--
mariasa
------------------------------------------------------------------------
mariasa's Profile: http://www.excelforum.com/member.php...o&userid=31726
View this thread: http://www.excelforum.com/showthread...hreadid=521666

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default undoing the for next procedure

Hi Mariasa,

Try:

'=============
Public Sub Tester()

Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long

Set WB = ThisWorkbook

On Error GoTo XIT
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With

For Each SH In WB.Sheets
If SH.Index 3 Then
SH.Delete
End If
Next SH

XIT:
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With

End Sub
'<<=============

---
Regards,
Norman



"mariasa" wrote in
message ...

Guys,

My macro creates 66 new wsheets and names them. How do I undo the sub?
I mean if i wanted to modify code and include other commands withing
the For Next Procedure, but dont want to create another 66 new sheets
and name them all over again but would like to undo it and repeat the
procedure with my revised code.

Thanks so much!!!!

Regards,
Maria


--
mariasa
------------------------------------------------------------------------
mariasa's Profile:
http://www.excelforum.com/member.php...o&userid=31726
View this thread: http://www.excelforum.com/showthread...hreadid=521666



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default undoing the for next procedure


Wow long code but worked like magic when applied after using ur code to
create the 66 worksheets and name them. Thanks Norman!!!!

But when I use the shorter version of OverAC to create the 66 sheets
and name them first, namely

Sub CommandButton1_Click()

Dim counter As Integer

For counter = 2 To 67
Sheets.Add
ActiveSheet.Name = Sheets("Sheet1").Range("A" & counter).Value
Next counter
End Sub

and then use ur code to undo it, i am left with sheet 67, 68, 69 named
according to the funds list 947, 949 and 953. So the sheet1 which
contains all the code is then eliminated. Why does that happen and is
there any way to fix that?

Thanks a bunch :-)

Sincerely,
Maria


--
mariasa
------------------------------------------------------------------------
mariasa's Profile: http://www.excelforum.com/member.php...o&userid=31726
View this thread: http://www.excelforum.com/showthread...hreadid=521666

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default undoing the for next procedure

Hi Maria,

Wow long code but worked like magic when applied after using ur code to
create the 66 worksheets and name them. Thanks Norman!!!!


The length of code is not necessarily an indication of efficiency; indeed an
inverse relationship may exist.

My code could be shortened substantially by, for exaample, deleting the (non
contiguous) sections:

On Error GoTo XIT
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With




XIT:
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With


This would shorten the code but would also increase the code execution time.

But when I use the shorter version of OverAC to create the 66 sheets
and name them first, namely

Sub CommandButton1_Click()

Dim counter As Integer

For counter = 2 To 67
Sheets.Add
ActiveSheet.Name = Sheets("Sheet1").Range("A" & counter).Value
Next counter
End Sub

and then use ur code to undo it, i am left with sheet 67, 68, 69 named
according to the funds list 947, 949 and 953. So the sheet1 which
contains all the code is then eliminated. Why does that happen and is
there any way to fix that?


My original sheet insertion code specified that each new sheet should be
added to the end of the workbook. In consequence, I am able to delete the
(now) unwanted sheets by deleting all sheets after the third sheet.

The shorter code which you have used does not specify the insertion position
for the new sheets and, thus, problems may be experienced if you use my
suggested deletion code.

In the present situation, you could delete the remaining three unwanted
sheets with a one-off code:

'=============
Public Sub Tester04()
Dim arr As Variant

arr = Array("947", "949", "953")
Sheets(arr).Delete
End Sub
'<<=============


---
Regards,
Norman


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default undoing the for next procedure


Hi Norman,

i tried deleting the 2 parts of the code as u suggested and i got the
msg "Data may exist in the sheets selected for deletion. To permanently
delete the data press delete" and I had to press delete 66 times to get
down to my starting 3 sheets.

Also with ur last suggestion - insert the code to delete the remaining
3 unwanted sheets - my workbook will be completely empty, since the
sheet 1, 2 and 3 have already been deleted.

Thanks for the explanation. I will use ur code for creation and naming
of the sheets then since it works with ur undo method and seems to be
more flexible because of all the dims

thanks again!!!


--
mariasa
------------------------------------------------------------------------
mariasa's Profile: http://www.excelforum.com/member.php...o&userid=31726
View this thread: http://www.excelforum.com/showthread...hreadid=521666

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
Undoing formula phil2006 Excel Discussion (Misc queries) 1 July 13th 06 11:33 AM
Undoing a macro MissyLovesExcel Excel Programming 7 December 7th 05 04:12 PM
undoing conditional formatting lil'bit Excel Discussion (Misc queries) 3 November 10th 05 08:55 PM
Undoing LINKS in Excel 2000 jayceejay New Users to Excel 3 January 4th 05 05:58 PM
undoing a VB action RICH Excel Programming 2 November 14th 03 12:42 PM


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