JugglerJAF
Active Member
- Joined
- Feb 17, 2002
- Messages
- 297
- Office Version
- 365
- Platform
- Windows
I receive a number of data files each month from a number of different sources, and while they are all supposed to be in a standard layout (column headers and column order), they often aren't.
I've written some code that checks to see if the values on the sheet match to either...
- the standard layout (in which case no changes are required)
- a known incorrect layout (in which case, I know what actions are required to get the file back to standard layout - moving/deleting/adding/renaming columns)
- a new unknown layout (in which case I display an error message)
The code I have (simplified of course, the actual files have almost 60 column headers) is shown below, but it's a bit of a pain to populate all of the new values whenever a new data file layout turns up (roughly one new layout every 3 or 4 months on average).
Is there an easier/more efficient/quicker to add new variations/better way of doing this?
I've written some code that checks to see if the values on the sheet match to either...
- the standard layout (in which case no changes are required)
- a known incorrect layout (in which case, I know what actions are required to get the file back to standard layout - moving/deleting/adding/renaming columns)
- a new unknown layout (in which case I display an error message)
The code I have (simplified of course, the actual files have almost 60 column headers) is shown below, but it's a bit of a pain to populate all of the new values whenever a new data file layout turns up (roughly one new layout every 3 or 4 months on average).
Is there an easier/more efficient/quicker to add new variations/better way of doing this?
Code:
Sub data_layout_checks()
'v0: standard layout
If UCase([A1]) = "NAME" And UCase([B1]) = "REGION" And UCase([C1]) = "AGE" And UCase([D1]) = "SALES" Then
MsgBox "Standard layout. No changes required."
Exit Sub
End If
'v1: known incorrect version 1
If UCase([A1]) = "NAME" And UCase([B1]) = "REGION" And UCase([C1]) = "SALES" And UCase([D1]) = "AGE" Then
Columns("D:D").Cut
Columns("C:C").Insert Shift:=xlToRight
MsgBox "Converted to standard layout (v1)."
Exit Sub
End If
'v2: known incorrect version 2
If UCase([A1]) = "SALES" And UCase([B1]) = "DEPARTMENT" And UCase([C1]) = "REGION" And UCase([D1]) = "EMPLOYEE NAME" And UCase([E1]) = "AGE(Y)" Then
Columns("B:B").Delete Shift:=xlToLeft
Columns("A:A").Cut
Columns("E:E").Insert Shift:=xlToRight
Columns("A:A").Cut
Columns("C:C").Insert Shift:=xlToRight
[A1].Value = "Name"
[C1].Value = "Age"
MsgBox "Converted to standard layout (v2)."
Exit Sub
End If
'error if unknown layout
MsgBox "Unknown layout. Unable to fix automatically."
End Sub