I want to copy paste some columns of a closed workbook into my active workbook. My columns are separeted, so I want to do that with a loop over a Collection variable.
here my code (EDIT)
Option Explicit
Sub CopyDataFromClosedWbk()
'copy data from closed workbook to active workbook
Dim colonne As Collection
Dim col As Variant
Dim xlApp As Application
Dim xlBook As Workbook
Dim Sh As Object
Set colonne = New Collection
colonne.Add "A:B", "A1"
colonne.Add "E:F", "G1"
colonne.Add "N", "I1"
colonne.Add "P", "F1"
Set xlApp = CreateObject("Excel.Application")
'Path source workbook
Set xlBook = xlApp.Workbooks.Open("C:\Users\Amira AYADI\Desktop\Stage\Automatisation\Base Case_BDD CAPACITAIRE_ENVOYE_V2 2017_2023_24042017.xlsx")
xlBook.Sheets("DATA").Range("A1:CJ374810").AutoFilter
xlBook.Sheets("DATA").Range("BD1").Select
xlBook.Sheets("DATA").Range("$A$1:$CJ$374810").AutoFilter Field:=56, Criteria1:="CMR"
For Each col In colonne
xlBook.Sheets("DATA").Range(col).Copy
xlApp.DisplayAlerts = False
Debug.Print col
Set xlBook = Nothing
Set xlApp = Nothing
Set xlBook = ActiveWorkbook
Set Sh = xlBook.Sheets("Calcul")
'Sh.Activate
Range(colonne.Item(col)).Select
Sh.paste
Next col
xlBook.Close
xlApp.Quit
End Sub
But that don't work and there are several mistakes:
First the
Range(colonne.Item(col)).Select
don't work whith "col" but with index I guess. So how can iterate over Key's value ?
Second when I remplace col by 1 like that (for a try) :
Range(colonne.Item(1)).Select
I have a 91 error : Object variable or With block not set.
In addition to that, I have a pop up window saying that the workbook "source.xlsx" is ready to modifications. How can I desable this?
Do you have any ideas ?