I am having problems rearranging columns in multiple Excel files. I have 312 files with 200 columns each. I would like to arrange the columns in a particular order so that I combine all the files to create a master file. I have the VBA code that rearranges the columns in one Excel file. I would like the code to loop through all the files, rearrange the columns, save and close the workbook.
Sub MoveColumns()
strName = Dir(File_Path & "\" & "*.csv") 'CHANGE FILE EXTENSION TO OTHER EXCEL BASED EXTENSION IF YOU WANT
Dim iRow As Long
Dim iCol As Long
Do While strName <> vbNullString
If active_workbook.Name <> strName And strName <> "" Then
Workbooks.Open Filename:=File_Path & "\" & strName
Set dataset_workbook = ActiveWorkbook
'Constant values
ActiveWorksheet = Sheets(1)
target_sheet = "Final Report" 'Specify the sheet to store the results
iRow = Sheets(ActiveWorksheet).UsedRange.Rows.Count
Worksheets.Add.Name = "Final Report"
For iCol = 1 To Sheets(ActiveWorksheet).UsedRange.Columns.Count
'Sets the TargetCol to zero in order to prevent overwriting existing targetcolumns
TargetCol = 0
'Read the header of the original sheet to determine the column order
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Source" Then TargetCol = 1
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Business Name" Then TargetCol = 2
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Legal Name" Then TargetCol = 3
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical Address" Then TargetCol = 4
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical Address Number" Then TargetCol = 5
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical Pre Direction" Then TargetCol = 6
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical Address Name" Then TargetCol = 7
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical Address Suffix" Then TargetCol = 8
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical Post Direction" Then TargetCol = 9
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical City" Then TargetCol = 10
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical State" Then TargetCol = 11
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical ZIP" Then TargetCol = 12
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Physical ZIP 4" Then TargetCol = 13
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Key Executive Name" Then TargetCol = 14
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "First Name" Then TargetCol = 15
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Middle Initial" Then TargetCol = 16
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Last Name" Then TargetCol = 17
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Title" Then TargetCol = 18
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Gender" Then TargetCol = 19
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Location Employee Size" Then TargetCol = 20
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Corporate Employee Size" Then TargetCol = 21
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Revenue / Yr" Then TargetCol = 22
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing Address" Then TargetCol = 23
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing Address Number" Then TargetCol = 24
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing Pre Direction" Then TargetCol = 25
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing Address Name" Then TargetCol = 26
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing Address Suffix" Then TargetCol = 27
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing Post Direction" Then TargetCol = 28
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing City" Then TargetCol = 29
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing State" Then TargetCol = 30
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing ZIP" Then TargetCol = 31
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Mailing ZIP 4" Then TargetCol = 32
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Phone" Then TargetCol = 33
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Fax" Then TargetCol = 34
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Toll-Free" Then TargetCol = 35
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "County Name" Then TargetCol = 36
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "County Population" Then TargetCol = 37
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Metro Area" Then TargetCol = 38
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Latitude" Then TargetCol = 39
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Longitude" Then TargetCol = 40
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "EIN" Then TargetCol = 41
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Main Line of Business" Then TargetCol = 42
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Franchise" Then TargetCol = 43
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Location Type" Then TargetCol = 44
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Importer or Exporter" Then TargetCol = 45
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Manufacturer" Then TargetCol = 46
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Primary SIC" Then TargetCol = 47
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Primary SIC Description" Then TargetCol = 48
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC02" Then TargetCol = 49
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC02.Description" Then TargetCol = 50
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC03" Then TargetCol = 51
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC03.Description" Then TargetCol = 52
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC04" Then TargetCol = 53
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC04.Description" Then TargetCol = 54
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC05" Then TargetCol = 55
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC05.Description" Then TargetCol = 56
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC06" Then TargetCol = 57
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC06.Description" Then TargetCol = 58
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC07" Then TargetCol = 59
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC07.Description" Then TargetCol = 60
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC08" Then TargetCol = 61
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC08.Description" Then TargetCol = 62
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC09" Then TargetCol = 63
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC09.Description" Then TargetCol = 64
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC10" Then TargetCol = 65
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "SIC10.Description" Then TargetCol = 66
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 1" Then TargetCol = 67
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 1 Description" Then TargetCol = 68
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 2" Then TargetCol = 69
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 2 Description" Then TargetCol = 70
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 3" Then TargetCol = 71
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 3 Description" Then TargetCol = 72
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 4" Then TargetCol = 73
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 4 Description" Then TargetCol = 74
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 5" Then TargetCol = 75
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 5 Description" Then TargetCol = 76
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 6" Then TargetCol = 78
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 6 Description" Then TargetCol = 79
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 7" Then TargetCol = 80
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 7 Description" Then TargetCol = 81
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 8" Then TargetCol = 82
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 8 Description" Then TargetCol = 83
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 9" Then TargetCol = 84
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 9 Description" Then TargetCol = 85
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 10" Then TargetCol = 86
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 10 Description" Then TargetCol = 87
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Non-Profit" Then TargetCol = 88
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 2 Description" Then TargetCol = 70
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 3" Then TargetCol = 71
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 3 Description" Then TargetCol = 72
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 4" Then TargetCol = 73
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 4 Description" Then TargetCol = 74
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 5" Then TargetCol = 75
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 5 Description" Then TargetCol = 76
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 6" Then TargetCol = 78
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 6 Description" Then TargetCol = 79
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 7" Then TargetCol = 80
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 7 Description" Then TargetCol = 81
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 8" Then TargetCol = 82
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 8 Description" Then TargetCol = 83
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 9" Then TargetCol = 84
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 9 Description" Then TargetCol = 85
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 10" Then TargetCol = 86
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "NAICS 10 Description" Then TargetCol = 87
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Non-Profit" Then TargetCol = 88
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Number of PCs" Then TargetCol = 89
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Public / Private" Then TargetCol = 90
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Home Based Business" Then TargetCol = 91
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Small Business" Then TargetCol = 92
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Square Footage" Then TargetCol = 93
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Website" Then TargetCol = 94
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Women Owned" Then TargetCol = 95
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Year of Incorporation" Then TargetCol = 96
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Ticker Symbol" Then TargetCol = 97
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Stock Exchange" Then TargetCol = 98
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Fortune 1000 Ranking" Then TargetCol = 99
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Credit Score" Then TargetCol = 100
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "2014 Revenue/Yr" Then TargetCol = 101
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "2012 Revenue/Yr" Then TargetCol = 102
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "2010 Revenue/Yr" Then TargetCol = 103
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "3 Year % Sales Growth" Then TargetCol = 104
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "5 Year % Sales Growth" Then TargetCol = 105
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "2014 Employees" Then TargetCol = 106
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "2012 Employees" Then TargetCol = 107
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "2010 Employees" Then TargetCol = 108
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "3 Year % Employee Growth" Then TargetCol = 109
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "5 Year % Employee Growth" Then TargetCol = 110
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 1" Then TargetCol = 111
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 1" Then TargetCol = 112
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 1" Then TargetCol = 113
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 1" Then TargetCol = 114
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 2" Then TargetCol = 115
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 2" Then TargetCol = 116
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 2" Then TargetCol = 117
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 2" Then TargetCol = 118
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 3" Then TargetCol = 119
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 3" Then TargetCol = 120
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 3" Then TargetCol = 121
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 3" Then TargetCol = 122
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 4" Then TargetCol = 123
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 4" Then TargetCol = 124
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 4" Then TargetCol = 125
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 4" Then TargetCol = 126
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 5" Then TargetCol = 127
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 5" Then TargetCol = 128
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 5" Then TargetCol = 129
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 5" Then TargetCol = 130
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 6" Then TargetCol = 131
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 6" Then TargetCol = 132
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 6" Then TargetCol = 133
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 6" Then TargetCol = 134
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 7" Then TargetCol = 135
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 7" Then TargetCol = 136
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 7" Then TargetCol = 137
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 7" Then TargetCol = 138
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 8" Then TargetCol = 139
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 8" Then TargetCol = 140
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 8" Then TargetCol = 141
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 8" Then TargetCol = 142
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 9" Then TargetCol = 143
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 9" Then TargetCol = 144
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 9" Then TargetCol = 145
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 9" Then TargetCol = 146
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 10" Then TargetCol = 147
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 10" Then TargetCol = 148
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 10" Then TargetCol = 149
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 10" Then TargetCol = 150
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 11" Then TargetCol = 151
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 11" Then TargetCol = 152
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 11" Then TargetCol = 153
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 11" Then TargetCol = 154
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 12" Then TargetCol = 155
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 12" Then TargetCol = 156
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 12" Then TargetCol = 157
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 12" Then TargetCol = 158
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 13" Then TargetCol = 159
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 13" Then TargetCol = 160
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 13" Then TargetCol = 161
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 13" Then TargetCol = 162
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 14" Then TargetCol = 163
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 14" Then TargetCol = 164
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 14" Then TargetCol = 165
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 14" Then TargetCol = 166
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 15" Then TargetCol = 167
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 15" Then TargetCol = 168
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 15" Then TargetCol = 169
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 15" Then TargetCol = 170
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 16" Then TargetCol = 171
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 16" Then TargetCol = 172
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 16" Then TargetCol = 173
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 16" Then TargetCol = 174
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 17" Then TargetCol = 175
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 17" Then TargetCol = 176
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 17" Then TargetCol = 177
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 17" Then TargetCol = 178
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 18" Then TargetCol = 179
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 18" Then TargetCol = 180
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 18" Then TargetCol = 181
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 18" Then TargetCol = 182
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 19" Then TargetCol = 182
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 19" Then TargetCol = 183
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 19" Then TargetCol = 184
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 19" Then TargetCol = 185
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive First Name 20" Then TargetCol = 186
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Last Name 20" Then TargetCol = 187
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Title 20" Then TargetCol = 188
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Executive Gender 20" Then TargetCol = 189
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Accounting Annual Expense" Then TargetCol = 190
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Advertising Annual Expense" Then TargetCol = 191
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Business Insurance Annual Expense" Then TargetCol = 192
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Legal Annual Expense" Then TargetCol = 193
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Office Equipment Annual Expense" Then TargetCol = 194
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Rent Annual Expense" Then TargetCol = 195
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Technology Annual Expense" Then TargetCol = 196
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Telecom Annual Expense" Then TargetCol = 197
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Est. Utilities Annual Expense" Then TargetCol = 198
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "Date" Then TargetCol = 199
If Sheets(ActiveWorksheet).Cells(1, iCol).Value = "AtoZ ID" Then TargetCol = 200
'If a TargetColumn was determined (based upon the header information) then copy the column to the right spot
If TargetCol <> 0 Then
'Select the column and copy it
Sheets(ActiveWorksheet).Range(Sheets(ActiveWorksheet).Cells(1, iCol), Sheets(ActiveWorksheet).Cells(iRow, iCol)).Copy Destination:=Sheets(target_sheet).Cells(1, TargetCol)
End If
Next iCol 'Move to the next column until all columns are read
End If
strName = Dir
Loop
Application.DisplayAlerts = True
End Sub
Here is a very streamlined method for rearranging the columns in your 312 files.
Create a new workbook with a worksheet called "Template". In the first row of the template enter the column headers you want... in the order you want. For example, in cell A1 enter: Source. In cell B1 enter: Business Name. In cell C1 enter: Legal Name. And continue filling row 1 with the order of your columns you want.
Now place the following routine in a standard code module in the template workbook.:
Edit the sPath at the top of the routine so that it knows where your 312 CSV files are.
Run the routine.
That's it.
It will create a master file from all the files with a worksheet named "Final Report". BUT THE COLUMNS WILL BE IN THE ORDER YOU ESTABLISHED WITH THE TEMPLATE.
The original CSV files will remain unaltered.
What issue is the above giving? Is it just not looping through each file?
I think this should work (you may want to copy perhaps only 3 files to a new test folder, to make sure it works before running this on 100+ files).
I think you just need to edit/revise your Loop...try this line instead of `Do While strName <> vbNullString
I could help a little more, but what how is
active_workbook
being defined?