Sub Macro()
Dim MyObj As Object, MySource As Object, file As Variant
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim Sheetname As String
Dim path_file As String
Dim folder As String
Dim filename As String
folder = ThisWorkbook.Worksheets("ControlPanel").Range("D10").Value
file = Dir(folder & "*.csv")
numfiles_total = 0
numfiles_i = 0
'Application.ScreenUpdating = False
Do While file <> ""
numfiles_total = numfiles_total + 1
file = Dir()
Loop
file = Dir(folder & "*.csv")
valor = 1
With ThisWorkbook
Do While file <> ""
MsgBox valor
ThisWorkbook.Worksheets("ControlPanel").Range("A14") = "Files processed: " & numfiles_i & "/" & numfiles_total
ThisWorkbook.Worksheets("ControlPanel").Range("F15").Value = file
ThisWorkbook.Worksheets("data").Select
Cells.Select
Selection.ClearContents
path_file = folder & file
ThisWorkbook.Sheets.Add(after:=.Sheets(.Sheets.Count)).Name = "data_temp"
With ThisWorkbook.Worksheets("data_temp").QueryTables.Add(Connection:= _
"TEXT;" & path_file _
, Destination:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = ","
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ThisWorkbook.Worksheets("data_temp").Range("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), _
TrailingMinusNumbers:=True
ThisWorkbook.Worksheets("data_temp").Range("B:B").Select
Selection.Replace What:="000", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ThisWorkbook.Worksheets("data").Range("A:F").Clear
ThisWorkbook.Worksheets("data_temp").Range("A:F").Copy Destination:=ThisWorkbook.Worksheets("data").Range("A:F")
Application.DisplayAlerts = False
ThisWorkbook.Worksheets("data_temp").Delete
Application.DisplayAlerts = True
ThisWorkbook.Worksheets("data").Columns("A").ColumnWidth = 12
ThisWorkbook.Worksheets("data").Columns("B:B").NumberFormat = "0"
totalrows = WorksheetFunction.Count(.Worksheets("data").Range("A:A"))
For i = 1 To totalrows
ThisWorkbook.Worksheets("Calculation").Range("A" & i).Value = ThisWorkbook.Worksheets("data").Range("A" & i).Value & "," & ThisWorkbook.Worksheets("data").Range("B" & i).Value & "," & Replace(ThisWorkbook.Worksheets("data").Range("D" & i).Value, ",", ".") & "," & Replace(ThisWorkbook.Worksheets("data").Range("C" & i).Value, ",", ".") & "," & ThisWorkbook.Worksheets("data").Range("E" & i).Value
Next i
newfiletxt = folder & Replace(file, ".csv", ".txt")
newfilecsv = folder & file
Open newfiletxt For Output As #1
For i = 1 To totalrows
Print #1, ThisWorkbook.Worksheets("Calculation").Range("A" & i)
Next i
Close #1
If Not Dir(newfilecsv) = "" Then
Kill newfilecsv
End If
Name newfiletxt As newfilecsv
Call ClearTextToColumns
numfiles_i = numfiles_i + 1
valor = valor + 1
file = Dir
Loop
End With
MsgBox "All files were processed correctly!", vbInformation
' Application.ScreenUpdating = True
End Sub