Hello
I have a macro that help me to extract some data from text files and put on a table in Excel.
The macro is working, adapted by me from old one.
Sometimes I have hundreds of txt files and since the macro is not the fastest on my laptop I don't know how many files are processed/remains.
I want to add a progress bar on screen to know how many files are processes/remains from all files.
I try to do it myself, but no success.
See below the code for my macro:
I have a macro that help me to extract some data from text files and put on a table in Excel.
The macro is working, adapted by me from old one.
Sometimes I have hundreds of txt files and since the macro is not the fastest on my laptop I don't know how many files are processed/remains.
I want to add a progress bar on screen to know how many files are processes/remains from all files.
I try to do it myself, but no success.
See below the code for my macro:
Code:
Private Sub Database_generateur()
Application.ScreenUpdating = False
Dim fichier, repert, Fname As String
Dim data2 As Worksheet
Dim indexrow, indexrow2 As Integer
indexrow = Application.WorksheetFunction.CountA(ActiveSheet.Range("A:A")) + 1
indexrow2 = 1
indexrow = indexrow + 1
'declare active document
Set data2 = ActiveWorkbook.Worksheets("data")
' ask filename to load
fichier = Application.GetOpenFilename("All Files (*.*), *.*")
If fichier = "Faux" Then Exit Sub 'test if cancel
'take the name of file to remove to path to have only directory
Fname = Dir(fichier)
repert = Left(fichier, Len(fichier) - Len(Fname))
Fname = Dir(repert)
Do While (Fname <> "") 'read all xls file
'test column to fill
Nb1 = ActiveCell.Column
'create file to find
fichier = repert & Fname
'open xls file with data
Workbooks.OpenText Filename:=fichier _
, Origin:=932, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _
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), Array(7, 1), Array(8, 1), _
Array(9, 1)), TrailingMinusNumbers:=True
Set xlbook = Workbooks(Fname)
Set Cell1 = xlbook.ActiveSheet.Range("A" & indexrow2)
Do While Cell1 <> "CODIGO"
Cell1 = xlbook.ActiveSheet.Range("A" & indexrow2)
'Time recording
If Cell1 = "TIEMPO" Then data2.Range("H" & indexrow).Value = xlbook.ActiveSheet.Range("B" & indexrow2).Value
'Data extraction
If Cell1 = "101" Then data2.Range("I" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "901" Then data2.Range("J" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "1" Then data2.Range("K" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "102" Then data2.Range("L" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "902" Then data2.Range("M" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "2" Then data2.Range("N" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "202" Then data2.Range("O" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "3" Then data2.Range("P" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "5" Then data2.Range("Q" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "4" Then data2.Range("R" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "6" Then data2.Range("S" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "19" Then data2.Range("T" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "14" Then data2.Range("U" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "107" Then data2.Range("V" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "115" Then data2.Range("W" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "915" Then data2.Range("X" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "15" Then data2.Range("Y" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "215" Then data2.Range("Z" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "116" Then data2.Range("AA" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "916" Then data2.Range("AB" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "16" Then data2.Range("AC" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "216" Then data2.Range("AD" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "117" Then data2.Range("AE" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "917" Then data2.Range("AF" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "17" Then data2.Range("AG" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "118" Then data2.Range("AH" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "918" Then data2.Range("AI" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "18" Then data2.Range("AJ" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "700" Then data2.Range("AK" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "701" Then data2.Range("AL" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "610" Then data2.Range("AM" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "611" Then data2.Range("AN" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "20" Then data2.Range("AO" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "121" Then data2.Range("AP" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "21" Then data2.Range("AQ" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "415" Then data2.Range("AR" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "416" Then data2.Range("AS" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "417" Then data2.Range("AT" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "418" Then data2.Range("AU" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "860" Then data2.Range("AV" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
If Cell1 = "861" Then data2.Range("AW" & indexrow).Value = xlbook.ActiveSheet.Range("T" & indexrow2).Value
indexrow2 = indexrow2 + 1
Loop
data2.Range("A" & indexrow).Value = xlbook.ActiveSheet.Range("I1").Value
data2.Range("B" & indexrow).Value = xlbook.ActiveSheet.Range("B1").Value
data2.Range("C" & indexrow).Value = xlbook.ActiveSheet.Range("B3").Value
data2.Range("D" & indexrow).Value = xlbook.ActiveSheet.Range("A5").Value
data2.Range("E" & indexrow).Value = xlbook.ActiveSheet.Range("B5").Value
'data2.Range("AC" & indexrow).Value = xlbook.ActiveSheet.Range("B3").Value
data2.Range("F" & indexrow).Value = xlbook.ActiveSheet.Range("A" & (indexrow2)).Value
If xlbook.ActiveSheet.Range("A" & indexrow2).Value <> "0" Then
data2.Range("G" & indexrow).Value = xlbook.ActiveSheet.Range("B" & (indexrow2)).Value
data2.Range("H" & indexrow).Value = xlbook.ActiveSheet.Range("C" & (indexrow2)).Value
Else
End If
indexrow = indexrow + 1
indexrow2 = 1
Fname = Dir
xlbook.Close
Loop
Application.ScreenUpdating = True
End Sub
Sub ConvertNum()
Range("A1:A10").Select
Selection.NumberFormat = "0.000"
End Sub
Private Sub CommandButton1_Click()
Database_generateur
End Sub