nguyenanhdung8111982
Thành viên hoạt động



- Tham gia
- 1/11/19
- Bài viết
- 120
- Được thích
- 33
- Giới tính
- Nam
tôi có file gốc như hình dưới

và sau khi copy ra file mới

Đây là code:
Nhờ mọi người giúp đỡ chỉnh sửa code để
cột "ID" là số thứ tự tăng dần theo dòng cột A
,cột "trksegID" giá trị luôn =1
link file:
drive.google.com

và sau khi copy ra file mới

Đây là code:
Mã:
Sub ProcessMultipleFiles()
Dim NewFileName As String
Dim FileList As Variant, FilePath As Variant
Dim FolderPath As String
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
FolderPath = "D:\file_csv\"
FileList = Array("20190928.csv", "20190927.csv")
For Each FilePath In FileList
FilePath = FolderPath & FilePath
If FSO.FileExists(FilePath) Then
NewFileName = FSO.GetBaseName(FilePath)
NewFileName = NewFileName & "_N.csv"
FSO.CopyFile FilePath, FolderPath & NewFileName, True
CSVAmend2 FolderPath, NewFileName
Else
MsgBox FilePath & " not found"
End If
Next FilePath
End Sub
Sub CSVAmend2(FolderPath As String, FileName As String)
Dim wb As Workbook, ws As Worksheet, rng As Range, headers As Variant
headers = Array("ID", "trksegID", "lat", "lon", "ele", "time", "time_N", "Heading")
'open file (immediate save not needed)
'Set wb = Workbooks.Open("D:\test_file\test\20200310_07_002_QTB_GS023662-gps.csv")
Set wb = Workbooks.Open(FolderPath & FileName)
'wb.SaveAs ("D:\test_file\test\20200310_07_002_QTB_GS023662.csv")
Set ws = wb.Sheets(1)
Set rng = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp))
'add time columns
With rng.Offset(, 8)
.Formula = "=F2"
.Value = .Value
.Offset(, 8).Value = .Value
.Formula = "=A2"
.Value = .Value
.Offset(, 6).Value = .Value
.Offset(, 6).NumberFormat = "yyyy/mm/dd hh:mm:ss"
.Formula = "=A2+ TIME(7,0,0)"
.Offset(, 7).Value = .Value
.Offset(, 7).NumberFormat = "yyyy/mm/dd hh:mm:ss"
End With
'add ID columns
ws.Range("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
rng.Formula = "=row()-1"
rng.Offset(, 1).Value = 1
'delete columns not required and insert headers
ws.Range("F:O").Delete Shift:=xlToLeft
ws.Range("A1:H1").Value = headers
ActiveSheet.Range("A2:H50000").RemoveDuplicates Columns:=6, Header:=xlYes
'save and close
'wb.Save
wb.Close SaveChanges:=True 'False
End Sub
Nhờ mọi người giúp đỡ chỉnh sửa code để
cột "ID" là số thứ tự tăng dần theo dòng cột A
,cột "trksegID" giá trị luôn =1
link file:
file_csv.rar
