workbooks

jompy

Board Regular
Joined
Mar 2, 2009
Messages
189
Hi guys

<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--> Can anybody help out? Looking to run a loop (For I = 1 to 50) sort of thing. No problem with that, but I want to loop through two different workbooks.

So active workbook (lets call it book insect, sheet beetle) needs to be populated with data from source workbook (lets call it book animals, sheet lion)

Both workbooks are on C drive, and in a file named “Lite Software”

Just need to know how to address both workbooks so loop is performed.

Many thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi

Just set object references to each file and then use these:

Code:
Dim wbInsect As Workbook, wbAnimals as Workbook
Dim wsBeetle As Worksheet, wsLion As Worksheet

Set wbInsect = Workbooks.Open("C:\Lite Software\Insect.xls")
Set wbAnimals = Workbooks.Open("C:\Lite Software\Animals.xls")

Set wsBeetle = wbInsect.Worksheets("Beetle")
Set wsLion = wbAnimals.Worksheets("Lion")


'now use them:
For i = 1 to 50
   wsBeetle.Cells(i,1) = wsLion.Cells(i+10,1)
Next i
 
Upvote 0
Hi Richard

Thanks a lot for sharing works a treat. Had to do it from a third workbook but not a problem really, not sure why?

Thanks again

Jompy
 
Upvote 0
You have a reference available to the workbook in which any code resides which is ThisWorkbook. Hence, if you have the code in either of the Insects or Animals workbook, change the way this is referenced to:

Code:
'assume code stored in Animals.xls

Set wsLion = ThisWorkbook.Sheets("Lion")   'ThisWorkbook is an object reference that points at the workbook in which the code resides
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,158
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top