VBA - Import from closed workbook and match columns to named ranges

trux101

New Member
Joined
Feb 10, 2016
Messages
19
I am very new to VBA but have an ambitious goal that you may be able to help with.

I have the code below which is a small part of a larger code from Ron De Bruin importing using ADO method. The code enables opening of a closed workbook and importing data from that workbook into current workbook. This works perfectly only if the columns are set up to match in both workbooks which in my case is not always possible to achieve on all 40 columns of data each time. My import data files always have the first line as a header but the titles are not always consistent for example:

Employee Number
Start Date
Grade
Current Salary




Sometimes is
Sometimes is
Sometimes is
Sometimes is
Staff ID
Company Start Date
Company Grade
Salary
Employee ID
Group Start Date
Corporate Grade
Current Year Salary
Personnel ID
Date Started
Current Year Grade
CY Salary

<tbody>
</tbody>


1. Is there a method of tweaking the code below so that when the import begins the code finds the different titles of employee number and when a match is found – puts the data into a worksheet named range called Employee_Number in my receiving workbook and then moves on to Start Date, Grade etc. until all the defined columns are completed.

2. Where a match is not found for a column header a message box informing the user to pop up then continuing with the rest of the import

Sub GetData_Example4()
Dim SaveDriveDir As String, MyPath As String
Dim FName As Variant

SaveDriveDir = CurDir
MyPath = Application.DefaultFilePath
ChDrive MyPath
ChDir MyPath
FName = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*")

If FName = False Then
'do nothing
Else
GetData FName, "", "A1:C1", Sheets("Sheet1").Range("A1"), False, False
End If

ChDrive SaveDriveDir
ChDir SaveDriveDir
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,216,434
Messages
6,130,611
Members
449,584
Latest member
c_clark

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