2015-10-05 144 views
0

我在重新排列多个Excel文件中的列时遇到问题。我有312个文件,每个文件200列。我想按特定顺序排列这些列,以便将所有文件合并为一个主文件。我有VBA代码重新排列在一个Excel文件中的列。我希望代码遍历所有文件,重新排列列,保存并关闭工作簿。使用VBA重排多个Excel文件中的列

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 
+0

您可能会考虑用工作表上的查找表替换这些If语句。 –

+0

@TimI将学习如何做到这一点。谢谢 – Jonah

回答

2

这是一个非常简化的方法,用于重新排列312文件中的列。

用名为“Template”的工作表创建一个新的工作簿。在模板的第一行中,按照你想要的顺序输入你想要的列标题。例如,在单元格A1中输入:Source。在单元格B1中输入:业务名称。在单元格C1中输入:法定名称。然后继续填写第1行,并填写所需列的顺序。

现在把下面的程序在一个标准的代码模块中的模板工作簿:

Public Sub Jonah() 

    Dim c&, sPath$, sFile$, v, rOut As Range, wbReport As Workbook 

    On Error Resume Next 
    sPath = "D:\List\List\" 
    sFile = Dir(sPath & "*.csv") 

    Application.DisplayAlerts = False 
    Application.ScreenUpdating = False 
    With Sheets("template") 
     c = .Cells(1, .Columns.Count).End(xlToLeft).Column 
     v = .[a1].Resize(, c) 
     .Copy 
    End With 
    Set wbReport = ActiveWorkbook 
    wbReport.Sheets(1).Name = "Final Report" 
    With wbReport.Worksheets.Add 
     .Name = "Tmp" 
     Set rOut = .[a1].Resize(, c) 
     rOut = v 
    End With 
    Do 
     With Workbooks.Open(sPath & sFile) 
      With .Worksheets(1) 
       With .[a1].CurrentRegion.Resize(, c + 1) 
        .AdvancedFilter xlFilterCopy, , rOut 
        rOut.CurrentRegion.Resize(, 1).Offset(, c) = .Parent.Name 
        v = rOut.CurrentRegion.Resize(, c + 1).Offset(1) 
        With wbReport.Sheets("Final Report") 
         .Cells(.Rows.Count, "A").End(xlUp)(2).Resize(UBound(v, 1), UBound(v, 2)) = v 
        End With 
        rOut.CurrentRegion.Resize(, c + 1).Offset(1).ClearContents 
       End With 
      End With 
      .Close 0 
     End With 
     sFile = Dir 
    Loop Until sFile = "" 
    With wbReport.Sheets("Final Report") 
     .[a1].Offset(, c) = "CSV" 
     .Cells.EntireColumn.AutoFit 
    End With 
    wbReport.Sheets("Tmp").Delete 
    Application.DisplayAlerts = False 
    Application.ScreenUpdating = True 

End Sub 

编辑SPATH在例行的顶部,这样它知道你的312个CSV文件。

运行例程。

就是这样。

它将使用名为“最终报告”的工作表从所有文件创建主文件。 但列将以您建立的模板订购。

原始CSV文件将保持不变。

+0

@Jonah你可以做到这一点吗? –

+0

谢谢你的解决方案。我已经尝试过,但不幸的是,当我运行例程时没有任何反应。我已经相应地更改了文件路径。我还希望例程跳过中间文件并将所有数据合并到一个“最终报告”工作表中。 – Jonah

+0

我在这里测试了一些csv文件。你可以寄给我几个文件吗?也许他们有些不寻常的事情。我确信我可以很快把它整理出来。我的电子邮件地址是:[email protected] –

0

以上给出了什么问题?它只是没有循环通过每个文件?

我认为这应该起作用(你可能想复制或许只有3个文件到一个新的测试文件夹,以确保它在100个以上的文件上运行这个文件)。

我想你只需编辑/修改你的循环...尝试这条线,而不是`难道尽管则strName <> vbNullString

Do While Len(strName) > 0 

我可以帮助多一点,但什么是怎么active_workbook幸福界定?

+0

我的代码的问题是它打开每个文件。我希望它在不打开它们的情况下遍历文件,重新排列列并保存文件。 – Jonah