scott_n_phnx
Active Member
- Joined
- Sep 28, 2006
- Messages
- 445
I am extracting raw data from another program and importing into Excel 2007. An issue that I have run into is that the ordering is not always correct. I have found a code that will reorder the columns, but I have discovered that sometimes not all of the headers are included in the extract. The data in the columns are not always needed, but it throws off my other formulas and macros when this column is missing. Can anyone suggest a way to search through the header array and when one of the headers is missing, it will insert a blank/empty column? This should ensure that all of the data is aligned properly (I hope).
Here is the code that I am using to reorder, including the array.
Here is the code that I am using to reorder, including the array.
Code:
Sub Reorder_Columns()
Dim arrColOrder As Variant, ndx As Integer
Dim Found As Range, counter As Integer
arrColOrder = Array("EventTime(dt)", "Severity(sev)", "EventName(evt)", "Message(msg)", "InitHostDomain(rv42)", "InitHostName(shn)", "InitIP(sip)", "InitUserDomain(rv35)", _
"InitUserName(sun)", "TargetHostDomain(rv41)", "TargetHostName(dhn)", "TargetIP(dip)", "TargetUserDomain(rv45)", "TargetUserName(dun)", "InitServiceName(sp)", "ExtendedInformation(ei)", "DeviceEventTimeString(et)", "Tags(rv145)")
counter = 1
Application.ScreenUpdating = False
For ndx = LBound(arrColOrder) To UBound(arrColOrder)
Set Found = Rows("1:1").Find(arrColOrder(ndx), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not Found Is Nothing Then
If Found.Column <> counter Then
Found.EntireColumn.Cut
Columns(counter).Insert Shift:=xlToRight
Application.CutCopyMode = False
End If
counter = counter + 1
End If
Next ndx
Application.ScreenUpdating = True
End Sub