Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default For Each Loop

Could someone tell me why the following sub will not paset each book name
and sheet to its own workbook and sheet. I am obvously doing something
wrong but don't know what.


Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet


For Each wk In Workbooks
For Each ws In Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk



End Sub




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default For Each Loop

Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet


For Each wk In Workbooks
For Each ws In wk.Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk

End Sub

works for me

Best regards

Wolf


-----Original Message-----
Could someone tell me why the following sub will not

paset each book name
and sheet to its own workbook and sheet. I am obvously

doing something
wrong but don't know what.


Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet


For Each wk In Workbooks
For Each ws In Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk



End Sub




.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default For Each Loop

Excellent, Thanks



"Wolf" wrote in message
...
Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet


For Each wk In Workbooks
For Each ws In wk.Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk

End Sub

works for me

Best regards

Wolf


-----Original Message-----
Could someone tell me why the following sub will not

paset each book name
and sheet to its own workbook and sheet. I am obvously

doing something
wrong but don't know what.


Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet


For Each wk In Workbooks
For Each ws In Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk



End Sub




.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default For Each Loop

Matt,

The problem is caused by not referencing the correct workbook when iterating
through the worksheets. SO you need to add the wk. object qualifier to it


Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet

For Each wk In Workbooks
For Each ws In wk.Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk

End Sub

Incidentally, you can do this with worksheet function s

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,100)

=MID(CELL("filename",A1),FIND("[",CELL("filename",A1))+1,FIND("]",CELL("file
name",A1))-FIND("[",CELL("filename",A1))-1)

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Matt" wrote in message
...
Could someone tell me why the following sub will not paset each book name
and sheet to its own workbook and sheet. I am obvously doing something
wrong but don't know what.


Sub TestSub()

Dim wk As Workbook
Dim ws As Worksheet


For Each wk In Workbooks
For Each ws In Worksheets
ws.Cells(1, 1) = ws.Name
ws.Cells(3, 1) = wk.Name
Next ws
Next wk



End Sub






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
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
if & Loop steven.holloway Excel Discussion (Misc queries) 5 July 20th 07 09:50 AM
do..loop Anna Excel Discussion (Misc queries) 6 June 20th 07 01:10 PM
while loop Arun Kumar Saha Excel Worksheet Functions 2 June 19th 07 01:31 PM
Using a for loop Jeff Excel Discussion (Misc queries) 1 November 8th 06 09:27 PM


All times are GMT +1. The time now is 10:35 AM.

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"