Import multi dat files in vba run-time error 13

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear All Master,
I want to import multi dat files but there is a "run-time error of 13" and I mark the color red in the code below. Please solution.

VBA Code:
Option Explicit


Sub Get_Data_From_File()
    OptimizeVBA True
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    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
    startTime = Timer
    
    Application.ScreenUpdating = False
     With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False
 
   Range("A1").CurrentRegion.Clear
  
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Dat Files (*.dat*),*dat*", MultiSelect:=True)
[COLOR=rgb(184, 49, 47)]If FileToOpen <> False Then 'this line run-time error 13[/COLOR]
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range(Cells(1, 1), Cells(1, 2).End(xlDown)).Copy
        ThisWorkbook.Worksheets("selectfile").Range("A2:B2").PasteSpecial xlPasteValues
        OpenBook.Close False
Range("A1").CurrentRegion.Select
   Set objTable = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
   
   End If
 On Error GoTo getout 'if A1 doesn't belong to any Excel Table, then code will end execution
   If [A1].ListObject <> "" Then
    On Error GoTo 0 'restart Error handler
    objTable2 = [A1].ListObject.Name
   End If
  Application.ScreenUpdating = False
 Application.CutCopyMode = False: [H2].Select
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With


    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
Thanks
roykana
 
I made some re-arrangement to your code and also add a function. It could be better but as of now I tested this to be working. I hope it is to your requirement. I rarely work with table ?
VBA Code:
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
   
    Application.ScreenUpdating = False
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False
   
    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
 
    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
                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
    Else
        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    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
I made some re-arrangement to your code and also add a function. It could be better but as of now I tested this to be working. I hope it is to your requirement. I rarely work with table ?
VBA Code:
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
   
    Application.ScreenUpdating = False
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False
   
    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
 
    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
                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
    Else
        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    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
@Zot
Dear Mt. Zot,
Thanks for your reply.
from your code range table starting from A2 should start from A1
thanks
roykana
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
@Zot
Dear Mr.Zot,


what I mean below from your code then you can see the table range starting from A2 so that the contents of the data become table headers,

the contents of the data should not be table headers

07-01-2022.xlsm
AB
1
2501442943.3211
3500442943.32189
4501642943.32318
5502042943.32391
6500842943.3252
7500742943.71237
8500542943.7128
9502342944.30086
10502942944.30096
11502642944.30331
12100342861.74154
13100542861.7417
14100742861.74176
15100642861.74184
16200942861.74191
selectfile



VBA Code:
Range("A1").Value = "ID"
Range("B1").Value = "DATE & TIME"

So the table header uses this above code.

Thanks
roykana
 
Upvote 0
Sorry. I was just using the original paste location in your code and it was creating Column1 and Column 2 as Header. I thought you were able to work on it.

Here is the modified code. Note that I name the Table as TableDat for identification. You can change it to your liking. I have tested the code and is seems to work
VBA Code:
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
    
    Application.ScreenUpdating = False
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False
    
    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
    With wsSelect
        .Range("A1").Value = "ID"
        .Range("B1").Value = "DATE & TIME"
        .Range("A1", "B1").HorizontalAlignment = xlCenter
    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", wsSelect.Cells(Rows.Count, "B").End(xlUp)).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
    Else
        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    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
 
Upvote 0
Sorry. I was just using the original paste location in your code and it was creating Column1 and Column 2 as Header. I thought you were able to work on it.

Here is the modified code. Note that I name the Table as TableDat for identification. You can change it to your liking. I have tested the code and is seems to work
VBA Code:
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
   
    Application.ScreenUpdating = False
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False
   
    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
    With wsSelect
        .Range("A1").Value = "ID"
        .Range("B1").Value = "DATE & TIME"
        .Range("A1", "B1").HorizontalAlignment = xlCenter
    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", wsSelect.Cells(Rows.Count, "B").End(xlUp)).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
    Else
        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    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
@Zot
Dear Mr. Zot
Thanks for your reply. I tried to succeed but if I do it both times then the data increases to duplicates.
I added additional code as below and I marked yellow but the results of the table did not reach for column c up to column g and appear blank in column A. I provide screenshot below
Maybe you have another solution.
VBA Code:
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
    
    Application.ScreenUpdating = False
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False

    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
 [COLOR=rgb(247, 218, 100)]       wsSelect.Columns("A:G").Clear 'I do clear so that the incoming data is not duplicate[/COLOR]
    With wsSelect
        .Range("A1").Value = "ID"
        .Range("B1").Value = "DATE & TIME"
       [COLOR=rgb(247, 218, 100)] .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[/COLOR]
    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", wsSelect.Cells(Rows.Count, "B").End(xlUp)).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
             [COLOR=rgb(247, 218, 100)]   For k = 2 To LastRow
    Cells(k, 2).Value = WorksheetFunction.Text(Cells(k, 2).Value, "DD/MM/YYYY HH.MM")
    Next k[/COLOR]
    Else

        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    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
 

Attachments

  • result.JPG
    result.JPG
    93.6 KB · Views: 9
Upvote 0
I don't understand what you were trying to do. What do you meant by do it both times then the data increases to duplicates.

After import all the data from selected files, what then?
 
Upvote 0
I don't understand what you were trying to do. What do you meant by do it both times then the data increases to duplicates.

After import all the data from selected files, what then?
@Zot
Dear Mr. zot
First I do import then second I do import again then the result of the data becomes duplicate. The data should remain the same.
 
Upvote 0
@Zot
Dear Mr. zot
First I do import then second I do import again then the result of the data becomes duplicate. The data should remain the same.
Why do you want to import same file twice? or... are you saying different files also have duplicated data?

The main objective for the code is to import data from multiple files and append at the bottom of existing table. That's all.
 
Upvote 0
Why do you want to import same file twice? or... are you saying different files also have duplicated data?
@Zot
Dear Mr. Zot,
sorry I'm late in replying, an example of the 3 files that I shared, if one of the data files adds a record then I have to import all the data files a second time, the results should remain the same and only add additional records, not the result being a duplicate, which means first you have to clear the column first. first or do you have another solution
I used the code below but why did the table results not reach column G and also why was there a blank in column A.
Thanks
roykana
The main objective for the code is to import data from multiple files and append at the bottom of existing table. That's all.
test.xlsm
ABCDEFGH
1IDDATE & TIMEDATEYEARPERIODCATEGORYNAME
2501427/07/2017 07.4227/07/2017
3500427/07/2017 07.4327/07/2017
4501627/07/2017 07.4527/07/2017
5502027/07/2017 07.4627/07/2017
6500827/07/2017 07.4827/07/2017
7500727/07/2017 17.0527/07/2017
8500527/07/2017 17.0627/07/2017
9502328/07/2017 07.1328/07/2017
10502928/07/2017 07.1328/07/2017
11502628/07/2017 07.1628/07/2017
12100306/05/2017 17.4706/05/2017
13100506/05/2017 17.4806/05/2017
14100706/05/2017 17.4806/05/2017
15100606/05/2017 17.4806/05/2017
16200906/05/2017 17.4806/05/2017
1700/01/1900 00.0001/01/1900
1800/01/1900 00.0001/01/1900
1900/01/1900 00.0001/01/1900
2000/01/1900 00.0001/01/1900
2100/01/1900 00.0001/01/1900
2200/01/1900 00.0001/01/1900
2300/01/1900 00.0001/01/1900
2400/01/1900 00.0001/01/1900
2500/01/1900 00.0001/01/1900
2600/01/1900 00.0001/01/1900
2700/01/1900 00.0001/01/1900
2800/01/1900 00.0001/01/1900
2900/01/1900 00.0001/01/1900
3000/01/1900 00.0001/01/1900
3100/01/1900 00.0001/01/1900
selectfile


VBA Code:
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
    
    Application.ScreenUpdating = False
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    ActiveSheet.DisplayPageBreaks = False

    Set wsSelect = ActiveWorkbook.Sheets("selectfile")
        wsSelect.Columns("A:G").Clear
    With wsSelect
        .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", wsSelect.Cells(Rows.Count, "B").End(xlUp)).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
                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
    Else

        If FileToOpen = False Then
            On Error GoTo 0
            GoTo getout
        End If
    End If
getout:
    Application.ScreenUpdating = True
    With Application
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    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
 
Upvote 0
I don't really get what you meant.

You have multi-select where you select all 3 dat file. The data from 3 dat files are appended at the bottom after one another, correct?

After that what you were trying to do? Import another set of data from several other files but need to remove data that has already existed from previous import?

You also mentioned that you want to clear column first? I get confused. Tell me the sequence of actions you wanted to perform.
 
Upvote 0
I don't really get what you meant.

You have multi-select where you select all 3 dat file. The data from 3 dat files are appended at the bottom after one another, correct?
@Zot
Dear Mr.Zot
if I run the code without clear then the same data record will appear again at the bottom. so I apply the clear column code so that the data comes in without duplicate records
After that what you were trying to do? Import another set of data from several other files but need to remove data that has already existed from previous import?
so I apply the clear column code so that the data comes in without duplicate records
You also mentioned that you want to clear column first? I get confused. Tell me the sequence of actions you wanted to perform.
from the code I gave in post #18 then from there you can help modify, the problem is the table has not reached column g and in column A there is a blank record
so the point is every multi import file dat then if there is a previous record in the sheet "select file" it will be deleted .
Thanks
roykana
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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