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 For Each ... Next loop - need to reference the loop variable

I have a for-each loop which loops through worksheets. Due to an error
in Excel, and because I am copying a lot of sheets, I need to save,
close, and open the workbook several times from within the for-each
loop to prevent Excel from crashing (I'm basically using the workaround
proposed he
http://support.microsoft.com/default...4&Product=xlw).
I'm using Excel 2000.

This resets the reference to my worksheet in the for-each loop, and I'm
not sure how to save that information so I can 'reassign' the reference
after re-opening the workbook (within the loop). I tried saving the
name of the worksheet and then using a Set statement on the loop
variable (shtLL) to retrieve that particular worksheet, but that didn't
work. When it got to the "next" statement it acted as though it had
completed the for-each loop.

Here's some demonstrative code:

Dim shtLL As Worksheet
Dim wkbkLL As Workbook
Dim tempName as String

For Each shtLL In wkbkLL.Worksheets
tempName = shtLL.name

wkbkLL.Close savechanges:=True
Set wkbkLL = Nothing
Set wkbkLL = Application.Workbooks.Open(wkbkLLPath)

set shtLL = wkbkLL.Worksheets(tempName)
Next


Thanks for any help you can give!

 
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
Loop with variable name? Mike Excel Discussion (Misc queries) 6 April 25th 09 05:12 AM
use a variable to name an object in a loop m davidson Excel Programming 7 April 19th 06 10:19 AM
Loop with variable Knut Excel Programming 2 November 19th 05 02:48 PM
Advancing outer Loop Based on criteria of inner loop ExcelMonkey Excel Programming 1 August 15th 05 05:23 PM
Excel VBA Loop & Variable Reference Carolyn[_4_] Excel Programming 2 October 15th 04 08:30 PM


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