Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy cells between workbooks

Tags:

excel

vba

Could someone please help me with some VBA code.

I am trying to copy 2 ranges of cells between workbooks (both workbooks should be created beforehand as i don't want the code to create a new workbook on the fly).

Firstly I need to copy these ranges- From 'Sheet 3' of booka.xls, Range: Cell H5 to the last row in column H with data copy this to 'Sheet 1' of bookb.xls, starting in Cell B2 for as many cells down in the B column

Secondly I need to copy these ranges- From 'Sheet 3' of booka.xls, Range: Cell K5 to the last row in column K with data copy this to 'Sheet 1' of bookb.xls, starting in Cell D2 for as many cells down in the D column

Here is what I have so far:

 Sub CopyDataBetweenBooks()

Dim iRow        As Long
    Dim wksFr       As Worksheet
    Dim wksTo       As Worksheet

    wksFr = "C:\booka.xls"
    wksTo = "C:\bookb.xls"

    Set wksFrom = Workbooks(wksFr).Worksheets("Sheet 3")
    Set wksTo = Workbooks(wksTo).Worksheets("Sheet 1")

    With wksFrom
        For iRow = 1 To 100
            .Range(.Cells(iRow, 8), .Cells(iRow, 9)).Copy wksTo.Cells(iRow, 8)
        Next iRow
    End With

End Sub
like image 529
trunks Avatar asked Jun 09 '11 02:06

trunks


People also ask

How do I pull the same cell from multiple workbooks?

Click the tab for the first worksheet that you want to reference. Hold down the Shift key then click the tab for the last worksheet that you want to reference. Select the cell or range of cells that you want to reference. Complete the formula, and then press Enter.

How do I copy data from multiple Excel workbooks to one workbook?

On the Data tab, under Tools, click Consolidate. In the Function box, click the function that you want Excel to use to consolidate the data. In each source sheet, select your data, and then click Add. The file path is entered in All references.


2 Answers

Assuming you have the reference to wksFrom and wksTo, here is what the code should be

wksFrom.Range(wksFrom.Range("H5"), wksFrom.Range("H5").End(xlDown)).Copy wksTo.Range("B2")
wksFrom.Range(wksFrom.Range("K5"), wksFrom.Range("K5").End(xlDown)).Copy wksTo.Range("D2")
like image 78
shahkalpesh Avatar answered Oct 20 '22 17:10

shahkalpesh


Here's an example of how to do one of the columns:

Option Explicit
Sub CopyCells()
    Dim wkbkorigin As Workbook
    Dim wkbkdestination As Workbook
    Dim originsheet As Worksheet
    Dim destsheet As Worksheet
    Dim lastrow As Integer
    Set wkbkorigin = Workbooks.Open("booka.xlsm")
    Set wkbkdestination = Workbooks.Open("bookb.xlsm")
    Set originsheet = wkbkorigin.Worksheets("Sheet3")
    Set destsheet = wkbkdestination.Worksheets("Sheet1")
    lastrow = originsheet.Range("H5").End(xlDown).Row
    originsheet.Range("H5:H" & lastrow).Copy  'I corrected the ranges, as I had the src
    destsheet.Range("B2:B" & (2 + lastrow)).PasteSpecial 'and destination ranges reversed
End Sub

As you have stated in the comments, this code above will not work for ranges with spaces, so substitute in the code below for the lastrow line:

lastrow = originsheet.range("H65536").End(xlUp).Row

Now ideally, you could make this into a subroutine that took in an origin workbook name, worksheet name/number, and range, as well as a destination workbook name, worksheet name/number, and range. Then you wouldn't have to repeat some of the code.

like image 32
jonsca Avatar answered Oct 20 '22 19:10

jonsca