Copy cells from another open workbook, little lost.

Willow123

New Member
Joined
Dec 29, 2018
Messages
17
Hi All,

So I would like, if possible, to be able to copy 2 columns of text (whose length will vary) from another open workbook into the my active workbook. I believe I have to use the index method of identifying the workbook that i would like to copy the column of text from as the name of that workbook will change. I cannot have anycode in the workbook i am copying from. I've been playing around with variations of the code below just trying to get one column to copy, to no avail. Any thoughts would be greatly appreciated.

Code:
[FONT=Verdana]Sub getinfo()[/FONT]
[FONT=Verdana]'Workbooks(2).Activate

Dim r() As Variant
Dim r1 As Range[/FONT]
[FONT=Verdana]
With Workbooks(2)
r1 = Range("H1", Range("H1").End(xlDown)).Select
End With[/FONT]
[FONT=Verdana]
Range("H1", Range("H1").End(xlDown)).Offset(0, 1).Select[/FONT]
[FONT=Verdana]
End Sub
[/FONT]
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
If you are going to use the index to identify your workbooks then you will need to make sure of the order in which the workbooks are opened, because Excel determines what the index number is, not the code you write. If the workbook containing the code is opened first and no other Excel files are open, then it will be Workbooks(1), the next to open will be Workbooks(2) and the index will follow in sequence as the workbooks are opened. If you close Workbooks(2), all the folloing indexes automatically decrease by 1. So working with indexes means you have to keep track of which workbooks are opened in which order and if any close during runtime of a procedure, the index of some of the open workbooks will change. The best way to handle that is to Set your workbooks to object variables at the beginning of the code and use that variable throughout the code so it will always refer to the correct workbook.
Code:
Subt()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet
Set wb1 = ThisWorkbook 'assues opened first
Set wb2 = Workbooks(2)
Set sh1 = wb1.Sheets(1) 'variable now contains workbook and sheet reference
Set sh2 = wb2.Sheets(1) 'same here
sh1.Range("H2", sh1.Cells(Rows.Count, "H").End(xlUp)).Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
End Sub
 
Last edited:
Upvote 0
If you are going to use the index to identify your workbooks then you will need to make sure of the order in which the workbooks are opened, because Excel determines what the index number is, not the code you write. If the workbook containing the code is opened first and no other Excel files are open, then it will be Workbooks(1), the next to open will be Workbooks(2) and the index will follow in sequence as the workbooks are opened. If you close Workbooks(2), all the folloing indexes automatically decrease by 1. So working with indexes means you have to keep track of which workbooks are opened in which order and if any close during runtime of a procedure, the index of some of the open workbooks will change. The best way to handle that is to Set your workbooks to object variables at the beginning of the code and use that variable throughout the code so it will always refer to the correct workbook.
Code:
Subt()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet
Set wb1 = ThisWorkbook 'assues opened first
Set wb2 = Workbooks(2)
Set sh1 = wb1.Sheets(1) 'variable now contains workbook and sheet reference
Set sh2 = wb2.Sheets(1) 'same here
sh1.Range("H2", sh1.Cells(Rows.Count, "H").End(xlUp)).Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2)
End Sub

Thanks for the help JLG that works great.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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