Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-06-2018, 10:28 PM
Josh1012 Josh1012 is offline How to Loop through all open Workbboks Windows 10 How to Loop through all open Workbboks Office 2010 64bit
Novice
How to Loop through all open Workbboks
 
Join Date: Sep 2018
Posts: 12
Josh1012 is on a distinguished road
Default How to Loop through all open Workbboks

Hi,

I'm trying to loop through all open workbooks excluding the one I'm working in as the master workbook and want to copy the first two cells (A1:A2) in each workbook in each sheet, back to the workbook that I'm working in.

Here is the code I have so far, but I keep getting a runtime error '438':


Sub Macro1()
'
' Macro1 Macro
'
Dim wb As Workbook
Dim ws As Worksheet
Dim myrange As Range


Dim i As Long

For Each wb In Application.Workbooks
If wb.Name <> "Book1.xlsx" Then
For Each ws In wb.Sheets()
For i = 3 To i + 2
ws.Range("A1:A2").Copy Destination:=Windows("Book1").Sheets("Sheet1").Ran ge(Cells(i, 1), Cells((i + 1), 1)).String
Next i
Next ws
End If
Next wb
End Sub
Reply With Quote
  #2  
Old 09-07-2018, 02:11 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,771
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

Please ALWAYS wrap code with code tags ( Edit- select code - click #).Thx
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
  #3  
Old 09-07-2018, 08:28 AM
NoSparks NoSparks is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Where is your code located ?
Are you sure "Book1.xlsx" is right ?


Does something like this work
Code:
Sub Macro1()

Dim wb As Workbook, ws As Worksheet, i As Long

'cycle through all open workbooks
For Each wb In Application.Workbooks
    'check if it's thisworkbook <~~ ie: the one holding this code
    If wb.Name <> ThisWorkbook.Name Then
        'when it's not thisworkbook
        With wb
            'cycle through the sheets in wb
            For Each ws In wb.Sheets
                'determine where to write data in thisworkbook
                i = ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
                'first write is to start at row 3
                If i < 3 Then i = 3
                'copy values to thisworkbook from ws
                ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Resize(2).Value = ws.Range("A1:A2").Value
            Next ws
        End With
    End If
Next wb

End Sub
Reply With Quote
  #4  
Old 09-08-2018, 03:55 AM
Josh1012 Josh1012 is offline How to Loop through all open Workbboks Windows 10 How to Loop through all open Workbboks Office 2010 64bit
Novice
How to Loop through all open Workbboks
 
Join Date: Sep 2018
Posts: 12
Josh1012 is on a distinguished road
Default

Hi,

@Pecoflyer, yeah sorry I didn't know I had to wrap the code. I will do that next time.

@No Sparks, thanks for your reply. Yes my code is located in Book.xlsx.

I haven't tried your code yet but I will let you know if it works as soon as I try and run it.

Also, just a quick question. I'm new to VBA and want to know when to use the "With" command.
Reply With Quote
  #5  
Old 09-08-2018, 06:41 AM
NoSparks NoSparks is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Quote:
Yes my code is located in Book.xlsx
No it isn't... it can't be.
Code can only be saved in an .xlsm (macro enabled) file.

Quote:
Also, just a quick question. I'm new to VBA and want to know when to use the "With" command.
While in the VBA environment, if you put the cursor in a word and hit F1 Excel will bring up the help page for that word.

A quick Google search for "VBA when to use with" returned this as one of the top items.

Last edited by NoSparks; 09-09-2018 at 06:05 AM. Reason: spelling
Reply With Quote
  #6  
Old 09-08-2018, 08:00 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,771
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

Quote:
Originally Posted by Josh1012 View Post
Hi,

@Pecoflyer, yeah sorry I didn't know I had to wrap the code. I will do that next time.

Now that you do know, could you please add them in your first post ?
Thanks
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
  #7  
Old 09-09-2018, 02:11 AM
Josh1012 Josh1012 is offline How to Loop through all open Workbboks Windows 10 How to Loop through all open Workbboks Office 2010 64bit
Novice
How to Loop through all open Workbboks
 
Join Date: Sep 2018
Posts: 12
Josh1012 is on a distinguished road
Default

Hey,



Thanks so much, the code worked brilliantly. Also I hate to ask but how do I edit my original post to include code formatting?


I appreciate your help and the tips.
Reply With Quote
  #8  
Old 09-09-2018, 03:48 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,771
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

Click the "Edit button then " Go advanced". You will find the # button there.
It also appears when you create a new post. Thanks
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
  #9  
Old 09-09-2018, 06:29 AM
NoSparks NoSparks is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Quote:
Also I hate to ask but how do I edit my original post to include code formatting?
I'm not sure that you can.

I just tried following the instructions Pecoflyer is giving you and while I could edit my post at #5 (posted yesterday) there is no Edit button at post #3 (posted two days ago) so I can't edit that one.

I've never started a topic so don't know if the situation is different for you.

Perhaps one of the moderators will elaborate on what the parameters are that allow editing.
Reply With Quote
  #10  
Old 09-09-2018, 07:11 AM
Josh1012 Josh1012 is offline How to Loop through all open Workbboks Windows 10 How to Loop through all open Workbboks Office 2010 64bit
Novice
How to Loop through all open Workbboks
 
Join Date: Sep 2018
Posts: 12
Josh1012 is on a distinguished road
Default

@NoSparks


Hey, no worries. I actually tried looking up the FAQ's to see if I could edit it, but I think you only have a limited amount of time to edit it until it becomes permanent.



Again, thanks for all your help.
Reply With Quote
  #11  
Old 09-09-2018, 08:51 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,771
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

To be honest I never tried it as a normal member. Perhaps as Mod I have more options.
I'll check this with admin.
Thanks for letting me know
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
  #12  
Old 09-10-2018, 12:19 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline How to Loop through all open Workbboks Windows 7 64bit How to Loop through all open Workbboks Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,771
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

After checking, Admin confirmed the Edit possibility only lasts 24hr to prevent spamming
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
  #13  
Old 09-11-2018, 02:37 AM
Josh1012 Josh1012 is offline How to Loop through all open Workbboks Windows 10 How to Loop through all open Workbboks Office 2010 64bit
Novice
How to Loop through all open Workbboks
 
Join Date: Sep 2018
Posts: 12
Josh1012 is on a distinguished road
Default

Quote:
Originally Posted by Pecoflyer View Post
After checking, Admin confirmed the Edit possibility only lasts 24hr to prevent spamming

No worries, thanks for checking that up. I'll make sure to remember that in the future.
Reply With Quote
Reply

Tags
copy, for each loop, for loop



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Loop through all open Workbboks Do Loop bug donlincolnmsof Word VBA 11 09-07-2017 10:07 PM
How to Loop through all open Workbboks Macro Loop Help Twizzle008 Word VBA 15 09-18-2015 03:20 PM
What's wrong with my loop? Irrma Word VBA 2 06-17-2014 06:25 AM
How to Loop through all open Workbboks How to a For loop in VBA Jennifer Murphy Word VBA 1 01-29-2013 03:30 AM
How to Loop through all open Workbboks Continuous Loop cksm4 Word 6 01-06-2011 09:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:08 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft