Help copy column in excel with VBA for multiple file!!!

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
180
Office Version
  1. 2016
Platform
  1. Windows
I have file test.csv with format as 1.jpg
1585059064026.png

i want copy file new test_N.csv with format as 2.jpg
1585059101748.png

column ID : Fill in the order automatically
trksegID default with value: 1
copy column Latitude->lat
copy column Longitude->lon
copy column Alevation->ele
copy column Heading->Heading
Date/Time ->Time with format "yyyy-mm-dd hh:mm:ss"
Time_N=time(7,0,0)+column time
link file: example: test.csv->test_N.csv
test1.csv->test1_n.csv
 

Attachments

  • 1585059077412.png
    1585059077412.png
    106 KB · Views: 4

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this. It will open a dialog in order to pick your csv files to be converted. Multiple files can be selected by pressing CTRL. Each file will be imported in a separate workbook.
VBA Code:
Option Explicit

Public Sub Reform_CSV()
    Const cMyFolder     As String = "E:\MyExtraDrive\User\Folder\SubFolder"    ' <<<< change as desired where your files are located
    Dim oWb             As Workbook
    Dim vDlgResult      As Variant
    Dim bCSVResult      As Boolean
    Dim lLastRow        As Long
    Dim i               As Long
    vDlgResult = FilePicker_CSV(cMyFolder)
    If Not vDlgResult(0) = vbCancel Then
        For i = LBound(vDlgResult) To UBound(vDlgResult)
            Set oWb = Workbooks.Add
            ActiveSheet.Range("A2").Select
            ActiveWindow.FreezePanes = True
            bCSVResult = ImportCSV(oWb.Sheets(1), CStr(vDlgResult(i)))
            If bCSVResult Then
                With oWb.Sheets(1)
                    lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                    .Columns("A:F").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    .Columns("H:H").Copy Destination:=.Columns("C:C")
                    .Columns("I:I").Copy Destination:=.Columns("D:D")
                    .Columns("J:J").Copy Destination:=.Columns("E:E")
                    .Columns("L:L").Copy Destination:=.Columns("F:F")
                    .Columns("H:W").Delete Shift:=xlToLeft
                    .Range("A1").Value = "ID"
                    .Range("B1").Value = "trksegID"
                    .Range("C1").Value = "lat"
                    .Range("D1").Value = "lon"
                    .Range("E1").Value = "ele"
                    .Range("G1").Value = "time"
                    .Range("H1").Value = "time_N"
                    .Range("A2").Value = "1"
                    .Range("B2").Value = "1"
                    .Range("H2").Formula = "= G2 + TIME(7,0,0)"
                    .Range("A2").AutoFill Destination:=.Range("A2:A" & lLastRow), Type:=xlFillSeries
                    .Range("B2").AutoFill Destination:=.Range("B2:B" & lLastRow), Type:=xlFillDefault
                    .Range("H2").AutoFill Destination:=.Range("H2:H" & lLastRow), Type:=xlFillDefault
                    .Columns("C:D").NumberFormat = "0.0000000"
                    .Columns("E:E").NumberFormat = "0.0"
                    .Columns("E:E").ColumnWidth = 7
                    .Columns("G:H").NumberFormat = "yyyy/mm/dd hh:mm:ss"
                End With
            Else
                MsgBox "Something unexpected went wrong", vbExclamation, "Reform_CSV"
        End If
        Next i
        Set oWb = Nothing
    Else
        MsgBox "User has canceled", vbExclamation, "Reform_CSV"
    End If
End Sub


Public Function FilePicker_CSV(ByRef argFolderPath As String) As Variant()
    Dim vFiles()    As Variant
    Dim oFSO        As Object
    Dim sPath       As String
    Dim fd          As FileDialog
    Dim i           As Long
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If Not oFSO.FolderExists(argFolderPath) Then
        sPath = Environ("USERPROFILE")
    End If
    Set oFSO = Nothing
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .InitialFileName = IIf(Right(argFolderPath, 1) = "\", argFolderPath, argFolderPath & "\")
        .AllowMultiSelect = True
        .Title = "Import CSV file"
        .ButtonName = "Import File(s)"
        .InitialView = msoFileDialogViewDetails
        .Filters.Clear
        .Filters.Add Description:="CSV (Comma-Separated Values)", Extensions:="*.csv", Position:=1
        .Filters.Add Description:="All Files", Extensions:="*.*", Position:=2
        .FilterIndex = 1
        If .Show = True Then
            ReDim vFiles(.SelectedItems.Count - 1)
            For i = 0 To .SelectedItems.Count - 1
                vFiles(i) = .SelectedItems.Item(i + 1)
            Next i
        Else
            ReDim vFiles(0)
            vFiles(0) = vbCancel
        End If
    End With
    Set fd = Nothing
    FilePicker_CSV = vFiles
End Function


Public Function FileStripExt(ByRef argFileName As String) As String
    Dim tLen    As Long
    FileStripExt = argFileName
    tLen = InStrRev(argFileName, ".", -1, vbTextCompare)
    If tLen <> 0 Then
        tLen = 1 + Len(argFileName) - tLen
        FileStripExt = Left(argFileName, Len(argFileName) - tLen)
    End If
End Function


Public Function ImportCSV(ByVal argSht As Worksheet, ByRef argCSV_FullName As String) As Boolean
    Dim oFSO        As Object
    Dim sIDName     As String
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    If oFSO.FileExists(argCSV_FullName) Then
        sIDName = FileStripExt(oFSO.GetFileName(argCSV_FullName))
        On Error GoTo SUB_ERROR
        With argSht.QueryTables.Add(Connection:="TEXT;" & argCSV_FullName, Destination:=Range("$A$1"))
            .Name = sIDName
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileDecimalSeparator = "."
            .TextFileThousandsSeparator = ","
            .Refresh BackgroundQuery:=False
        End With
        ImportCSV = True
    Else
        ImportCSV = False
    End If
    GoTo SUB_QUIT
SUB_ERROR:
    ImportCSV = False
    MsgBox "An error occured." & vbCrLf & _
           "Number: " & Err.Number & vbCrLf & _
           "Description: " & Err.Description & vbCrLf & _
           "Source: " & Err.Source & " (procedure ImportCSV)", vbCritical, "ImportCSV"
    Err.Clear
SUB_QUIT:
    Set oFSO = Nothing
End Function
 
Upvote 0
Hi GWteB!!!
Help me i changes columin as picture:
Column F: time
Column G: time_N
Column H: Heading
and remove duplicate conform column time
 

Attachments

  • change.jpg
    change.jpg
    188.5 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top