Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / November 2007

Tip: Looking for answers? Try searching our database.

rearrange Columns according to predefined order

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
stratis - 21 Nov 2007 08:56 GMT
I have a file lets name it file One Xl Sheet that has  10 Colums and Another
file two one that has 50
I would liketo rearrange  the file one colums  in such an order to match the
order in file B. So for example input
File One
A       B      C                   D
SSn Name  last name      Age
File Two
A                   B       C         D        E                F
record num    Age    Name   City   Last Name    SSn

File  One  Should be converted
A                               B       C         D                     E    
           F
record num (blank)    Age    Name   City(blank)   Last Name    SSn

I need to compare the 2 files and unless they get the same order this is not
possible.
Bernie Deitrick - 21 Nov 2007 15:15 GMT
stratis,

Put the code below into an otherwise blank workbook, then run the macro. It will prompt you to
select the two workbooks to open (so they should not be open already): File One first, then File
Two.

HTH,
Bernie
MS Excel MVP

Sub SortColumnsToMatchOrder()
Dim File1 As Workbook
Dim File2 As Workbook
Dim i As Integer
Dim myR As Range

Set File1 = Workbooks.Open(Application.GetOpenFilename( _
  , , "Select File 1 (To be sorted)"))
Set File2 = Workbooks.Open(Application.GetOpenFilename( _
  , , "Select File 2 (with sort order)"))

With File1.Sheets(1)
  .Range("A1").EntireRow.Insert
  .Range(.Range("A1"), .Cells(2, Columns.Count).End(xlToLeft)(0)).Formula = _
  "=MATCH(A2,'[" & File2.Name & "]" & File2.Sheets(1).Name & "'!1:1,FALSE)"

On Error GoTo NoMatchError
Set myR = .Range("1:1").SpecialCells(xlCellTypeFormulas, 16)
GoTo MatchErrors
NoMatchError:

For i = 1 To File2.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column
  If IsError(Application.Match(i, File1.Sheets(1).Range("1:1"), False)) Then
     With .Cells(1, Columns.Count).End(xlToLeft)
        .Cells(1, 2).Value = i
        .Cells(2, 2).Value = File2.Sheets(1).Cells(1, i).Value
     End With
  End If
Next i

.Range("1:1").Value = .Range("1:1").Value

.Range("A1").CurrentRegion.Sort Key1:=.Range("A1"), _
        Order1:=xlAscending, _
        Header:=xlNo, _
       Orientation:=xlLeftToRight, _
       DataOption1:=xlSortNormal

GoTo SkipMessage
MatchErrors:
MsgBox "Not all headers were matched! - check errors in row 1 of " & .Parent.Name
Exit Sub

SkipMessage:
.Range("1:1").EntireRow.Delete
End With

End Sub

>I have a file lets name it file One Xl Sheet that has  10 Colums and Another
> file two one that has 50
[quoted text clipped - 14 lines]
> I need to compare the 2 files and unless they get the same order this is not
> possible.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.