Home

Multiple files in different column order

%3CLINGO-SUB%20id%3D%22lingo-sub-646028%22%20slang%3D%22en-US%22%3EMultiple%20files%20in%20different%20column%20order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-646028%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%20I%20have%20over%201000%20CSV%20files%20with%20different%20column%20order.%20For%20example%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efile1%3A%20name%2C%20phone%2C%20date%2C%20question%3C%2FP%3E%3CP%3Efile2%3A%20phone%2C%20date%2C%20question%2C%20name%3C%2FP%3E%3CP%3Efile3%3A%20question%2C%20name%2C%20phone%2C%20date%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehow%20can%20I%20reorder%20the%20columns%20of%20them%20in%20a%20quick%20way%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-646028%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel.%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESorting%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-646075%22%20slang%3D%22en-US%22%3ERe%3A%20Multiple%20files%20in%20different%20column%20order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-646075%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F348754%22%20target%3D%22_blank%22%3E%40PPDD129056%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20the%20following%20vba%20macro%20read%20all%20csv%20and%20change%20the%20columns.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EFunction%20GetColumn(strTitle%20As%20String)%20As%20Integer%3CBR%20%2F%3EDim%20rngFind%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rngFind%20%3D%20ActiveSheet.Rows(1).Find(what%3A%3DstrTitle%2C%20lookat%3A%3DxlWhole)%3CBR%20%2F%3EIf%20rngFind%20Is%20Nothing%20Then%3CBR%20%2F%3EGetColumn%20%3D%200%3CBR%20%2F%3EElse%3CBR%20%2F%3EGetColumn%20%3D%20rngFind.Column%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Function%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESub%20Readall()%3CBR%20%2F%3EDim%20strPfad%20As%20String%3CBR%20%2F%3EDim%20strDatei%20As%20String%3CBR%20%2F%3EDim%20Quelle%20As%20Workbook%3CBR%20%2F%3EDim%20strName%20As%20String%3CBR%20%2F%3EDim%20lngRowFree%20As%20Long%3CBR%20%2F%3EDim%20lngRowMax%20As%20Long%3CBR%20%2F%3EDim%20lngCol%20As%20Long%3CBR%20%2F%3E%3CBR%20%2F%3EstrPfad%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%3CBR%20%2F%3EstrDatei%20%3D%20Dir(strPfad%20%26amp%3B%20%22*.csv%22)%3CBR%20%2F%3E%3CBR%20%2F%3EApplication.DisplayAlerts%20%3D%20False%3CBR%20%2F%3E%3CBR%20%2F%3EDo%20While%20strDatei%20%26lt%3B%26gt%3B%20%22%22%3CBR%20%2F%3EWorkbooks.OpenText%20Filename%3A%3DstrPfad%20%26amp%3B%20strDatei%2C%20Semicolon%3A%3DTrue%2C%20local%3A%3DTrue%3CBR%20%2F%3ESet%20Quelle%20%3D%20ActiveWorkbook%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20Quelle.Worksheets(1)%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ElngRowMax%20%3D%20Quelle.Worksheets(1).UsedRange.Rows.Count%3CBR%20%2F%3ElngCol%20%3D%20GetColumn(%22name%22)%3CBR%20%2F%3E.Range(.Cells(1%2C%20lngCol)%2C%20.Cells(lngRowMax%2C%20lngCol)).Copy%20Destination%3A%3D.Cells(1%2C%204)%3CBR%20%2F%3ElngCol%20%3D%20GetColumn(%22phone%22)%3CBR%20%2F%3E.Range(.Cells(1%2C%20lngCol)%2C%20.Cells(lngRowMax%2C%20lngCol)).Copy%20Destination%3A%3D.Cells(1%2C%205)%3CBR%20%2F%3ElngCol%20%3D%20GetColumn(%22question%22)%3CBR%20%2F%3E.Range(.Cells(1%2C%20lngCol)%2C%20.Cells(lngRowMax%2C%20lngCol)).Copy%20Destination%3A%3D.Cells(1%2C%206)%3CBR%20%2F%3E%3CBR%20%2F%3E.Range(%22A%3AC%22).Delete%3CBR%20%2F%3E%3CBR%20%2F%3EQuelle.SaveAs%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20strDatei%3CBR%20%2F%3EQuelle.Close%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3E%3CBR%20%2F%3EstrDatei%20%3D%20Dir()%3CBR%20%2F%3E%3CBR%20%2F%3ELoop%3CBR%20%2F%3EApplication.DisplayAlerts%20%3D%20True%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eregards%3C%2FP%3E%3CP%3EBernd%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fwww.vba-tanker.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ewww.vba-tanker.com%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-646587%22%20slang%3D%22en-US%22%3ERe%3A%20Multiple%20files%20in%20different%20column%20order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-646587%22%20slang%3D%22en-US%22%3EThanks%20for%20your%20reply.%20But%20my%20CSV%20files%20don't%20have%20the%20heading%20and%20the%20content%20of%20the%20data%20is%20in%20Chinese.%20FIle1%3A%20%E8%92%99AJ6140%202008%E5%B9%B409%E6%9C%8826%E6%97%A5%2015272419841224001X%20%E5%86%85%E8%92%99%E5%8F%A4%E5%91%BC%E5%92%8C%E6%B5%A9%E7%89%B9%E5%B8%82%E5%9C%9F%E5%8B%98%E9%99%A22%E5%8F%B7%E6%A5%BC1%E5%8D%95%E5%85%834%E5%8F%B7%20%E6%9E%97%E5%87%AF%2013654885004%20%E8%92%99AJ7961%202008%E5%B9%B409%E6%9C%8827%E6%97%A5%20150104197108073611%20%E5%86%85%E8%92%99%E5%8F%A4%E5%91%BC%E5%92%8C%E6%B5%A9%E7%89%B9%E5%B8%82%E7%8E%89%E6%B3%89%E5%8C%BA%E6%A1%83%E8%8A%B1%E4%B9%A1%E5%91%BC%E6%B8%85%E8%B7%AF%E5%91%BC%E5%B8%82%E7%AC%AC%E5%8D%81%E4%BA%8C%E4%B8%AD%E5%AD%A644%E5%8F%B7%20%E5%88%98%E6%96%87%E4%B9%89%2013947139489%20%E8%92%99AJ9546%202008%E5%B9%B409%E6%9C%8827%E6%97%A5%20150102198110063043%20%E5%86%85%E8%92%99%E5%8F%A4%E5%91%BC%E5%92%8C%E6%B5%A9%E7%89%B9%E5%B8%82%E6%96%B0%E5%9F%8E%E5%8C%BA%E5%81%A5%E5%BA%B7%E8%A1%9719%E5%8F%B7%E9%99%A22%E5%8F%B7%E6%A5%BC2%E5%8D%95%E5%85%8315%E5%8F%B7%20%E5%88%98%E5%AE%A3%2015047888294%20File2%3A%20%E7%AB%A5%E5%86%B2%E5%B1%B1%20310107530103505%2013901701395%20%E7%94%98%E6%B3%89%E4%BA%8C%E6%9D%9153%E5%8F%B79%E5%AE%A4%2020040514%20%E9%87%91%E4%BA%9A%E6%96%B0%20310222196203061452%2013501929685%20%E8%BF%8E%E5%9C%86%E8%B7%AF300%E5%BC%8465%E6%94%AF%E5%BC%844%E5%8F%B74%2020040514%20%E5%8D%A2%E6%B5%B7%E6%B3%89%20310107560617121%2013818176240%20%E4%B8%8A%E6%B5%B7%E5%B8%82%E6%99%AE%E9%99%80%E5%8C%BA%E9%87%91%E6%B2%99%E6%B1%9F%E8%B7%AF%E9%95%BF%E9%A3%8E%E4%B8%80%E6%9D%91%2020040514%20FIle3%3A%20%E8%92%99DD6909%20%E8%A2%81%E5%B9%B8%E7%A6%8F%2013694773726%20150425198511090019%20%E5%86%85%E8%92%99%E5%8F%A4%E8%87%AA%E6%B2%BB%E5%8C%BA%E8%B5%A4%E5%B3%B0%E5%B8%82%E5%85%8B%E4%BB%80%E5%85%8B%E8%85%BE%E6%97%97%202008%E5%B9%B410%E6%9C%8822%E6%97%A5%20%E8%92%99DD6910%20%E9%95%BF%E9%9D%92%2013847631979%20150426197904145377%20%E5%86%85%E8%92%99%E5%8F%A4%E8%87%AA%E6%B2%BB%E5%8C%BA%E8%B5%A4%E5%B3%B0%E5%B8%82%E7%BF%81%E7%89%9B%E7%89%B9%E6%97%97%E4%B9%8C%E4%B8%B9%E9%95%87%202008%E5%B9%B410%E6%9C%8828%E6%97%A5%20%E8%92%99DD6911%20%E6%B1%A4%E6%B5%B7%E6%B5%A9%2013848872416%20150423197409100017%20%E5%86%85%E8%92%99%E5%8F%A4%E8%87%AA%E6%B2%BB%E5%8C%BA%E8%B5%A4%E5%B3%B0%E5%B8%82%E5%B7%B4%E6%9E%97%E5%8F%B3%E6%97%97%202008%E5%B9%B410%E6%9C%8822%E6%97%A5%20What%20I%20hope%20is%20let%20my%20files%20reorder%20like%20Name%20(Usually%20composed%20of%202%20to%203%20Chinese%20characters)%20ID%20(Usually%20composed%20of%2015%20or%2018%20digits%2C%20sometimes%20the%2018th%20will%20be%20X)%20Phone%20(Usually%20composed%20of%2011%20digits)%20License%20plate%20Address%20and%20other%20moves%20to%20the%20back%20column%3C%2FLINGO-BODY%3E
PPDD129056
New Contributor

Hello, I have over 1000 CSV files with different column order. For example,

 

file1: name, phone, date, question

file2: phone, date, question, name

file3: question, name, phone, date

 

how can I reorder the columns of them in a quick way?

 

 

2 Replies

@PPDD129056 

Hi, the following vba macro read all csv and change the columns.

 

Function GetColumn(strTitle As String) As Integer
Dim rngFind As Range

Set rngFind = ActiveSheet.Rows(1).Find(what:=strTitle, lookat:=xlWhole)
If rngFind Is Nothing Then
GetColumn = 0
Else
GetColumn = rngFind.Column
End If

End Function


Sub Readall()
Dim strPfad As String
Dim strDatei As String
Dim Quelle As Workbook
Dim strName As String
Dim lngRowFree As Long
Dim lngRowMax As Long
Dim lngCol As Long

strPfad = ThisWorkbook.Path & "\"
strDatei = Dir(strPfad & "*.csv")

Application.DisplayAlerts = False

Do While strDatei <> ""
Workbooks.OpenText Filename:=strPfad & strDatei, Semicolon:=True, local:=True
Set Quelle = ActiveWorkbook

With Quelle.Worksheets(1)


lngRowMax = Quelle.Worksheets(1).UsedRange.Rows.Count
lngCol = GetColumn("name")
.Range(.Cells(1, lngCol), .Cells(lngRowMax, lngCol)).Copy Destination:=.Cells(1, 4)
lngCol = GetColumn("phone")
.Range(.Cells(1, lngCol), .Cells(lngRowMax, lngCol)).Copy Destination:=.Cells(1, 5)
lngCol = GetColumn("question")
.Range(.Cells(1, lngCol), .Cells(lngRowMax, lngCol)).Copy Destination:=.Cells(1, 6)

.Range("A:C").Delete

Quelle.SaveAs ThisWorkbook.Path & "\" & strDatei
Quelle.Close
End With

strDatei = Dir()

Loop
Application.DisplayAlerts = True
End Sub

 

regards

Bernd

www.vba-tanker.com

Highlighted
Thanks for your reply. But my CSV files don't have the heading and the content of the data is in Chinese. FIle1: 蒙AJ6140 2008年09月26日 15272419841224001X 内蒙古呼和浩特市土勘院2号楼1单元4号 林凯 13654885004 蒙AJ7961 2008年09月27日 150104197108073611 内蒙古呼和浩特市玉泉区桃花乡呼清路呼市第十二中学44号 刘文义 13947139489 蒙AJ9546 2008年09月27日 150102198110063043 内蒙古呼和浩特市新城区健康街19号院2号楼2单元15号 刘宣 15047888294 File2: 童冲山 310107530103505 13901701395 甘泉二村53号9室 20040514 金亚新 310222196203061452 13501929685 迎圆路300弄65支弄4号4 20040514 卢海泉 310107560617121 13818176240 上海市普陀区金沙江路长风一村 20040514 FIle3: 蒙DD6909 袁幸福 13694773726 150425198511090019 内蒙古自治区赤峰市克什克腾旗 2008年10月22日 蒙DD6910 长青 13847631979 150426197904145377 内蒙古自治区赤峰市翁牛特旗乌丹镇 2008年10月28日 蒙DD6911 汤海浩 13848872416 150423197409100017 内蒙古自治区赤峰市巴林右旗 2008年10月22日 What I hope is let my files reorder like Name (Usually composed of 2 to 3 Chinese characters) ID (Usually composed of 15 or 18 digits, sometimes the 18th will be X) Phone (Usually composed of 11 digits) License plate Address and other moves to the back column
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies