Copy Range from a workbook to another

JohannF

New Member
Joined
Apr 17, 2013
Messages
9
Hi there,
I need to copy a range from hundreds workbooks to one spreadsheet. In each workbook, there are 3 columns that I need to copy.
I wrote this :

Code:
    For i = 1 To 13
        Workbooks.Open Filename:=ThisWorkbook.Path & "\Meter" & j & "-" & i & ".csv", ReadOnly:=True
        Workbooks("Book1.xlsm").Sheets("Meter-1-2-3-4").Range("A" & incrow, "C" & incrow + 95) = Workbooks("Meter" & j & "-" & i & ".csv").Sheets("Meter" & j & "-" & i).Range("A2:C97").Value
        incrow = incrow + 96
        Workbooks("Meter" & j & "-" & i & ".csv").Close SaveChanges:=False
    Next i

It works well,

The problem is that every 13 workbooks I need to move about 5 columns to the right.

So the way I found is :

Code:
j = 1
    For i = 1 To 13
        Workbooks.Open Filename:=ThisWorkbook.Path & "\Meter" & j & "-" & i & ".csv", ReadOnly:=True
        Workbooks("Book1.xlsm").Sheets("Meter-1-2-3-4").Range([B]"A" & incrow, "C" & incrow + 95[/B]) = Workbooks("Meter" & j & "-" & i & ".csv").Sheets("Meter" & j & "-" & i).Range("A2:C97").Value
        incrow = incrow + 96
        Workbooks("Meter" & j & "-" & i & ".csv").Close SaveChanges:=False
    Next i
j = j + 1
incrow = 2


  For i = 1 To 13
        Workbooks.Open Filename:=ThisWorkbook.Path & "\Meter" & j & "-" & i & ".csv", ReadOnly:=True
        Workbooks("Book1.xlsm").Sheets("Meter-1-2-3-4").Range([B]"F" & incrow, "H" & incrow + 95[/B]) = Workbooks("Meter" & j & "-" & i & ".csv").Sheets("Meter" & j & "-" & i).Range("A2:C97").Value
        incrow = incrow + 96
        Workbooks("Meter" & j & "-" & i & ".csv").Close SaveChanges:=False
  Next i

I have to copy this several times and to change manually the column. It works but it's not elegant (effective).
I tried : Range(cells(a,b),cells(c,d)) but it doesn't work in this case.

Do you have any idea ?

Thanks in advance,

Johann
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
So I found a way (it was very easy but I didn't think about this). It's just an offset.

Code:
For j = 1 To 4
    For i = 1 To 13
        Workbooks.Open Filename:=ThisWorkbook.Path & "\Meter" & j & "-" & i & ".csv", ReadOnly:=True
        myrange.[U][B]Offset(0, 5 * (j - 1))[/B][/U] = Workbooks("Meter" & j & "-" & i & ".csv").Sheets("Meter" & j & "-" & i).Range("A2:C97").Value
        incrow = incrow + 96
        Workbooks("Meter" & j & "-" & i & ".csv").Close SaveChanges:=False
    Next i
incrow = 2
Next j
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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