Option Explicit
Const MastBookName = "MasterFile.xlsb" 'Edit to suit"
Const MastShtName = "MasterSheet" 'Edit to suit"
Dim OldSht As Object
Dim NewSht As Object
Public Sub ReArrange_and_Clear_Columns()
'This assumes that you have opened the Master Book
'And have Opened the CSV file as new Excel book
'AND the CSV Book is the Active book
Set OldSht = ActiveSheet
'Run Subs:
BlankColumns
AddNewSheet
MoveColumns
End Sub
Sub BlankColumns()
Dim Headers As Variant
Headers = Array("DS New/Reuse", "ComponentATA", "PART_STATUS", "Part NEW/REUSED", "Industrialisierung", "K_DISPO_", _
"STATION_PDA", "LaborCode", "ProdType", "ReportingCenter Team", "Requirement PoE")
Dim Found As Range
Dim i As Long
Application.ScreenUpdating = False
For i = 1 To UBound(Headers)
With Workbooks(MastBookName).Sheets(MastShtName).Rows(1)
Set Found = .Find(Headers(i))
If Not Found Is Nothing Then Intersect(Found.EntireColumn, .UsedRange).Offset(1).Clear
End With
Next
Application.ScreenUpdating = True
End Sub
Sub AddNewSheet()
Dim Sht As Worksheet
Dim Headers As Variant
Headers = Array("MSN", "Section_Short", "Used_ATA", "ADAP_CI", "ADAP_DS", "DS_State", "", "", "Component", "Component_NAME", _
"", "", "Component_Type", "Quantity_per_DS", "", "", "Station", "", "", "", "CA_Code", "Requirement_Name", _
"Requirement_State", "TechnoCode", "ATA_ZONE", "DS_Title", "DS_Issue", "DS_IPT", "DS_Team", "", _
"NSPI Standardisation Status", "NSPI Qualification comments", "NSPI Best Qual P-Status", "NSPI Best Qual E-Status", _
"Component_Release_Date", "DS_PoE", "DS_Version", "CI_Title", "", "Component_STATE", "Component_level", _
"Component_Lead_Time", "DS_Domain", "MERCode", "DORCode", "Natco Supply Code", "Natco Supplier Name")
Application.ScreenUpdating = False
With ActiveWorkbook
.Sheets.Add
Set NewSht = .ActiveSheet
NewSht.Range("A1").Resize(1, 47).Value = Headers
End With
Application.ScreenUpdating = True
End Sub
Sub MoveColumns()
Dim Cel As Range
Dim Found As Range
Dim Headers As Range
Application.ScreenUpdating = False
With OldSht
Set Headers = .Intersect(.Rows(1), .UsedRange)
End With
With NewSht
For Each Cel In .Intersect(.Rows(1), .UsedRange)
If Not Cel = "" Then
Set Found = Headers.Find(Cel)
If Not Found Is Nothing Then _
Found.EntireColumn.Copy Destination:=Cel
End If
Next
End With
Application.DisplayAlerts = False
'OldSht.Delete ''''Uncomment this line after testing
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub