Hey everybody!
I'm trying to write a macro that copies values from one workbook (which I select when running the macro) to my main workbook (Sheet "Data"). This file has several columns but I only need the values in the column with the header "Name" and "Volume". The data in these two columns should then be copied into the column of the same name in the main workbook ("Data"). The columns "Name" and "Volume" are not always in the same column depending on the file I import data from. How can I do this? As of now my macro copies everything (see below).
Thanks in advance!
I'm trying to write a macro that copies values from one workbook (which I select when running the macro) to my main workbook (Sheet "Data"). This file has several columns but I only need the values in the column with the header "Name" and "Volume". The data in these two columns should then be copied into the column of the same name in the main workbook ("Data"). The columns "Name" and "Volume" are not always in the same column depending on the file I import data from. How can I do this? As of now my macro copies everything (see below).
Thanks in advance!
Code:
Sub ImportData()
Sheets("Data").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
ChDrive "C:"ChDir "C:"
Filename = Application.GetOpenFilename("Excel files (*.xls*), *.xls*")
Set TargetWorkbook = ActiveWorkbook
Set TargetSheet = ActiveWorkbook.Sheets("Data")
If Filename <> False Then
Workbooks.Open (Filename)
Range("A2").Select
Set ImportWorkbook = ActiveWorkbook
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
TargetWorkbook.Activate
TargetSheet.Range("A2").Select
ActiveSheet.Paste
ImportWorkbook.Activate
Application.CutCopyMode = False
ActiveWorkbook.Close 0
TargetWorkbook.Activate
Last edited: