want code import dat convert to vba array

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear All Master,
I want the code below to be an array vba code because the actual record is 100000 so it makes it very slow


thanks
roykana

VBA Code:
Option Explicit
Sub Get_Data_From_File()
    OptimizeVBA True
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim wsSelect As Worksheet
    Dim i As Long
    Dim k As Long
    Dim L As Long
    Dim J As Long
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim objTable As ListObject
    Dim objTable2 As String
    Dim startTime As Single, endTime As Single
    Dim TableFound As Boolean
    startTime = Timer
    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
    With wsSelect
        .Columns("A:G").Clear
        .Range("A1").Value = "ID"
        .Range("B1").Value = "DATE & TIME"
        .Range("C1").Value = "DATE"
        .Range("D1").Value = "YEAR"
        .Range("E1").Value = "PERIOD"
        .Range("F1").Value = "CATEGORY"
        .Range("G1").Value = "NAME"
        .Range("A1:G1").HorizontalAlignment = xlCenter
        LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
    End With
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Dat Files (*.dat*),*dat*", MultiSelect:=True)
    On Error Resume Next
    If IsArray(FileToOpen) Then
        On Error GoTo 0
        For i = LBound(FileToOpen) To UBound(FileToOpen)
            Set OpenBook = Application.Workbooks.Open(FileToOpen(i))
            OpenBook.Sheets(1).Range(Cells(1, 1), Cells(1, 2).End(xlDown)).Copy
            TableFound = TableExists(wsSelect, "TableDat")
            If Not TableFound Then
                wsSelect.Range("A2:B2").PasteSpecial xlPasteValues
                OpenBook.Close False
                wsSelect.Range("A1", "G" & wsSelect.Cells(Rows.Count, "A").End(xlUp).Row).Select
                Set objTable = wsSelect.ListObjects.Add(xlSrcRange, Selection, , xlYes)
                objTable.Name = "TableDat"
            Else
                wsSelect.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                OpenBook.Close False
            End If
        Next
        With wsSelect
            For k = 2 To LastRow
                .Cells(k, 2).Value = WorksheetFunction.Text(.Cells(k, 2).Value, "DD/MM/YYYY HH.MM")
            Next k
            For L = 2 To LastRow
                .Cells(L, 3).Value = DateSerial(Mid(.Cells(L, 2), 7, 4), Mid(.Cells(L, 2), 4, 2), Mid(.Cells(L, 2), 1, 2))
            Next L
             For J = 2 To LastRow
                 .Cells(J, 4).Value = Format(CDate(Cells(J, 2).Text), "YYYY")
            Next J
        End With
    Else
        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.Goto wsSelect.Range("A1")
    endTime = Timer
    Debug.Print (endTime - startTime) & " seconds have passed [VBA]"
    OptimizeVBA False
End Sub
Sub OptimizeVBA(isOn As Boolean)
    Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
    Application.EnableEvents = Not (isOn)
    Application.ScreenUpdating = Not (isOn)
    ActiveSheet.DisplayPageBreaks = Not (isOn)
End Sub

Function TableExists(ws As Worksheet, sTableName As String) As Boolean
    TableExists = ws.Evaluate("ISREF(" & sTableName & ")")
End Function
 
This is your code for the table:
VBA Code:
        wsSelect.Range("A1", "G" & wsSelect.Cells(Rows.Count, "A").End(xlUp).Row).Select
        Set objTable = wsSelect.ListObjects.Add(xlSrcRange, Selection, , xlYes)             ' Create Table
        objTable.Name = "TableDat"

Put that code right before the last line of code. ;)
@johnnyL
Thank you for your information
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Here is an updated version, it combines @Marc L and my code that I previously submitted:

VBA Code:
Sub Get_Data_From_FileV3()
'
    Dim startTime                       As Single
    Dim FilesToOpen                     As Variant
'
    FilesToOpen = Application.GetOpenFilename("Text files,*.dat", , "Select files(s)", , True)
    If Not IsArray(FilesToOpen) Then Exit Sub
'
    startTime = Timer
'
    Application.ScreenUpdating = False
'
    Dim TableFound                      As Boolean
    Dim objTable                        As ListObject
    Dim FreeFileNumber                  As Long
    Dim DatFileRowFromFileArray         As Long
    Dim ListArray_A                     As Object, ListArray_B  As Object, ListArray_C  As Object, ListArray_D  As Object
    Dim ID                              As String, DateTime     As String, JustDate     As String, JustYear     As String
    Dim DatFilePath                     As Variant
    Dim File                            As Variant
    Dim AllDatFileRowsFromFileArray     As Variant, DatFileRowColumnsArray              As Variant
    Dim wsSelect                        As Worksheet
'
    Set ListArray_A = CreateObject("System.Collections.ArrayList"): Set ListArray_B = CreateObject("System.Collections.ArrayList")
    Set ListArray_C = CreateObject("System.Collections.ArrayList"): Set ListArray_D = CreateObject("System.Collections.ArrayList")
'
    Set wsSelect = Sheets("selectfile")
'
    DatFilePath = CurDir & "\"
'
    If Dir(DatFilePath & "*.dat") > "" Then ChDrive DatFilePath
    ChDir DatFilePath
    FreeFileNumber = FreeFile
'
    With wsSelect
       .UsedRange.Offset(1).Clear
'
        For Each File In FilesToOpen
            Open File For Input As #FreeFileNumber
'
            AllDatFileRowsFromFileArray = Split(Input(LOF(FreeFileNumber), #FreeFileNumber), vbCrLf)  ' Load all Rows in file to AllDatFileRowsFromFileArray
            Close #FreeFileNumber
'
            For DatFileRowFromFileArray = 0 To UBound(AllDatFileRowsFromFileArray)
                DatFileRowColumnsArray = Split(AllDatFileRowsFromFileArray(DatFileRowFromFileArray), vbTab)  ' Load individual Rows of file to DatFileRowColumnsArray
'
                If UBound(DatFileRowColumnsArray) = 5 Then
                    ID = DatFileRowColumnsArray(0)
                    DateTime = DatFileRowColumnsArray(1)
                    JustDate = Split(DatFileRowColumnsArray(1))(0)
                    JustYear = Split(DatFileRowColumnsArray(1), "-", 2)(0)
'
                    ListArray_A.Add ID
                    ListArray_B.Add DateTime
                    ListArray_C.Add JustDate
                    ListArray_D.Add JustYear
                End If
            Next DatFileRowFromFileArray
        Next File
'
'---------------------------------------------------------------------------------------------------------
'
        .Range("A2").Resize(ListArray_A.Count, 1).Value = WorksheetFunction.Transpose(ListArray_A.ToArray)
        .Range("B2").Resize(ListArray_B.Count, 1).Value = WorksheetFunction.Transpose(ListArray_B.ToArray)
        .Range("C2").Resize(ListArray_C.Count, 1).Value = WorksheetFunction.Transpose(ListArray_C.ToArray)
        .Range("D2").Resize(ListArray_D.Count, 1).Value = WorksheetFunction.Transpose(ListArray_D.ToArray)
    End With
'
    TableFound = TableExists(wsSelect, "TableDat")
    If Not TableFound Then
        wsSelect.Range("A1:G1").Value = Array("ID", "DATE & TIME", "DATE", "YEAR", "PERIOD", "CATEGORY", "NAME")
        wsSelect.Range("A1:G1").HorizontalAlignment = xlCenter
'
        wsSelect.Range("A1", "G" & wsSelect.Cells(Rows.Count, "A").End(xlUp).Row).Select
        Set objTable = wsSelect.ListObjects.Add(xlSrcRange, Selection, , xlYes)             ' Create Table
        objTable.Name = "TableDat"
    End If
'
    wsSelect.Columns("A:G").EntireColumn.AutoFit                                            ' AutoFit the columns for the destination sheet
'
    Application.Goto wsSelect.Range("A1")
'
    Application.ScreenUpdating = False
'
    Debug.Print "Time to complete = " & Timer - startTime & " seconds."
End Sub

Function TableExists(ws As Worksheet, sTableName As String) As Boolean
    TableExists = ws.Evaluate("ISREF(" & sTableName & ")")
End Function

Test Dat file result:

Time to complete = 0.03125 seconds.
 
Upvote 0
Multiple test files totaling 96.5k rows processed in 9 seconds.
 
Upvote 0
I use it on a different computer so I keep using "value2"
Your bad …​
Anyway according to your post #29 attachment my VBA demonstration revamped :​
VBA Code:
Sub Demo1r()
    Dim V, W, F%, R&, X, S, L&, Y
        V = ThisWorkbook.Path & "\test dat file update\":  If Dir(V & "*.dat") > "" Then ChDrive V: ChDir V
        W = Application.GetOpenFilename("Text files,*.dat", , "Select files(s)", , True):  If Not IsArray(W) Then Exit Sub
        F = FreeFile
        R = 2
    With Sheets("selectfile")
       .UsedRange.Clear
        Application.ScreenUpdating = False
        ReDim V(.Rows.Count - 2, 1 To 4)
    For Each X In W
        Open X For Input As #F
        S = Split(Input(LOF(F), #F), vbCrLf)
        Close #F
    For L = 0 To UBound(S) + (S(UBound(S)) = "")
        Y = Split(S(L), vbTab)
    If IsDate(Y(1)) Then
        V(L, 4) = Split(Y(1), "-", 2)(0)
    Else
        Y(1) = Replace(Replace(Y(1), "--", "/"), "-", "")
        V(L, 4) = Split(Y(1), "/", 2)(0)
    End If
        V(L, 1) = Y(0)
        V(L, 2) = Y(1)
        V(L, 3) = Split(Y(1))(0)
    Next
       .Cells(R, 1).Resize(L, UBound(V, 2)).Value = V
        R = R + L
    Next
       .[A1:G1] = [{"ID","DATE & TIME","DATE","YEAR","PERIOD","CATEGORY","NAME"}]
       .ListObjects.Add 1, .[A1].CurrentRegion, , 1
    End With
        Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Solution
That changes nothin' to the result, just a little optimization :
Rich (BB code):
.ListObjects.Add 1, .UsedRange, , 1
 
Upvote 0
Your bad …​
Anyway according to your post #29 attachment my VBA demonstration revamped :​
VBA Code:
Sub Demo1r()
    Dim V, W, F%, R&, X, S, L&, Y
        V = ThisWorkbook.Path & "\test dat file update\":  If Dir(V & "*.dat") > "" Then ChDrive V: ChDir V
        W = Application.GetOpenFilename("Text files,*.dat", , "Select files(s)", , True):  If Not IsArray(W) Then Exit Sub
        F = FreeFile
        R = 2
    With Sheets("selectfile")
       .UsedRange.Clear
        Application.ScreenUpdating = False
        ReDim V(.Rows.Count - 2, 1 To 4)
    For Each X In W
        Open X For Input As #F
        S = Split(Input(LOF(F), #F), vbCrLf)
        Close #F
    For L = 0 To UBound(S) + (S(UBound(S)) = "")
        Y = Split(S(L), vbTab)
    If IsDate(Y(1)) Then
        V(L, 4) = Split(Y(1), "-", 2)(0)
    Else
        Y(1) = Replace(Replace(Y(1), "--", "/"), "-", "")
        V(L, 4) = Split(Y(1), "/", 2)(0)
    End If
        V(L, 1) = Y(0)
        V(L, 2) = Y(1)
        V(L, 3) = Split(Y(1))(0)
    Next
       .Cells(R, 1).Resize(L, UBound(V, 2)).Value = V
        R = R + L
    Next
       .[A1:G1] = [{"ID","DATE & TIME","DATE","YEAR","PERIOD","CATEGORY","NAME"}]
       .ListObjects.Add 1, .[A1].CurrentRegion, , 1
    End With
        Application.ScreenUpdating = True
End Sub
@Marc L
Thank you very much and I'm sorry I'm late in reply. It went perfectly.

thanks
roykana
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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