Prashant1211
New Member
- Joined
- Jun 9, 2020
- Messages
- 33
- Office Version
- 2016
- Platform
- Windows
Hello VBA Experts,
In the following code, I open a file and copy the data from that file to my master file. Before I copy the data, I want to sort the columns in the open file according to my requirements. I have tried the following code but it does not work and have no idea what to change. Can anyone please help me to get the code corrected. Thanks
Dim search As Range
Dim cnt As Integer
Dim colOrdr As Variant
Dim indx As Integer
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim LastRow As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
If FileToOpen = False Then Exit Sub
Set OpenBook = Application.Workbooks.Open(FileToOpen)
LastRow = ActiveSheet.UsedRange.Rows.Count
OpenBook.Sheets(1).colOrdr = Array("Colli no list", "Item Number", "Material", "required Qty", "Base Unit of Measure", "Material Description", "Basic material", "Column name", "Document", "Drawing No.List") 'define column order with header names here
For indx = LBound(colOrdr) To UBound(colOrdr)
Set search = Rows("1:1").Find(colOrdr(indx), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not search Is Nothing Then
If search.Column <> cnt Then
search.EntireColumn.Cut
Columns(cnt).Insert Shift:=xlToRight
Application.CutCopyMode = False
End If
cnt = cnt + 1
End If
Next indx
In the following code, I open a file and copy the data from that file to my master file. Before I copy the data, I want to sort the columns in the open file according to my requirements. I have tried the following code but it does not work and have no idea what to change. Can anyone please help me to get the code corrected. Thanks
Dim search As Range
Dim cnt As Integer
Dim colOrdr As Variant
Dim indx As Integer
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim LastRow As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
If FileToOpen = False Then Exit Sub
Set OpenBook = Application.Workbooks.Open(FileToOpen)
LastRow = ActiveSheet.UsedRange.Rows.Count
OpenBook.Sheets(1).colOrdr = Array("Colli no list", "Item Number", "Material", "required Qty", "Base Unit of Measure", "Material Description", "Basic material", "Column name", "Document", "Drawing No.List") 'define column order with header names here
For indx = LBound(colOrdr) To UBound(colOrdr)
Set search = Rows("1:1").Find(colOrdr(indx), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If Not search Is Nothing Then
If search.Column <> cnt Then
search.EntireColumn.Cut
Columns(cnt).Insert Shift:=xlToRight
Application.CutCopyMode = False
End If
cnt = cnt + 1
End If
Next indx