0

I struggle with one thing when working on my macro. I have an array (defined as Variant) where I placed names of files I want to import to the sheet. I have a counter (defined as Long) placed in For cycle to go through all array's positions. When going through each of the file I call a function to count number of rows in the file to assure I will not import more than allowed in 1 excel sheet. And I have a second array (defined again as Variant) where I would like to store this number of lines counted within the file opened. This public variable is defined as Long and when I try to assign the value to an array I get an error 13 "Type mismatch".

For FilNamCntr = LBound(FilNams) To UBound(FilNams)
        File_To_Be_Checked = FilNams(FilNamCntr)
        Call File_Lenght_Checker
        FilNams_Rows(FilNamCntr) = File_To_Be_Checked_Rows '<< Here I get an error
Next FilNamCntr

Do you have any advice how could I assign a number from Long variable to an array? I simply want to have one array with filename to open the file and second array with its size (nb of rows) to check before I import the file how many rows do I already have in the sheet and if I can import the file to current sheet.

Below I attach a complete code:

    Option Explicit
    Public Sub FileImport_Multiple()
    Dim qry             As QueryTable
    Dim FilNams         As Variant
    Dim FilNams_Rows    As Variant
    Dim FilNamCntr      As Long
    Dim strQryName      As String
    Dim LastRow         As Long
    Dim ContainerWB     As Workbook
    Dim msgString       As String
    Dim RowCounter01    As Long
    Dim RowCounter02    As Long
    Dim wB              As Workbook
        FilNams = Application.GetOpenFilename(FileFilter:="Text Files (*.txt),*.txt", _
                                                   Title:="Select text files to Import", _
                                                   MultiSelect:=True)

        '===========================
        'Defining a name of this sheet to variable and setting up a statusbar
        '===========================

        Tool_name = ActiveWorkbook.Name

        Range("A1").Select

        'Check to see if any files were selected
        If TypeName(FilNams) = "Boolean" Then
                MsgBox "No Files Selected. Exiting Program."
                Exit Sub
            Else
                'msgString = Join(FilNams, vbCr)
                'MsgBox "FilNams is: " & msgString
        End If

        For FilNamCntr = LBound(FilNams) To UBound(FilNams)
                File_To_Be_Checked = FilNams(FilNamCntr)
                Call File_Lenght_Checker
                FilNams_Rows(FilNamCntr) = File_To_Be_Checked_Rows
        Next FilNamCntr

        For FilNamCntr = LBound(FilNams) To UBound(FilNams)
            FilNams(FilNamCntr) = "TEXT;" & FilNams(FilNamCntr)
        Next FilNamCntr

        'msgString = Join(FilNams, vbCr)
        'MsgBox "FilNams is: " & msgString
        For FilNamCntr = LBound(FilNams) To UBound(FilNams)
            With ActiveSheet
                On Error GoTo ErrorCatch:
                RowCounter01 = .Cells(.Rows.Count, "D").End(xlUp).Row
                'Append to previous data, if applicable
                If .Range("D" & Rows.Count).End(xlUp).Row = 1 Then
                        LastRow = 1
                    Else
                        LastRow = .Range("D" & Rows.Count).End(xlUp).Row + 1
                End If
                If (RowCounter01 + FilNams_Rows(FilNamCntr)) >= 1048576 Then
                        MsgBox "Not enough space to import text files. Exiting.."
                        Exit Sub
                End If
                'MsgBox "LastRow value is:" & LastRow 'verification test
                Set qry = .QueryTables.Add(Connection:=FilNams(FilNamCntr), _
                                        Destination:=.Range("D" & LastRow))
                With qry
                    .Name = "Filename"
                    .FieldNames = True
                    .RowNumbers = False
                    .FillAdjacentFormulas = False
                    .PreserveFormatting = True
                    .RefreshOnFileOpen = False
                    .RefreshStyle = xlInsertDeleteCells
                    .SavePassword = False
                    .SaveData = True
                    .AdjustColumnWidth = True
                    .RefreshPeriod = 0
                    .TextFilePromptOnRefresh = False
                    .TextFilePlatform = 437
                    .TextFileStartRow = 1
                    .TextFileParseType = xlDelimited
                    .TextFileTextQualifier = xlTextQualifierDoubleQuote
                    .TextFileConsecutiveDelimiter = False
                    .TextFileTabDelimiter = False
                    .TextFileSemicolonDelimiter = False
                    .TextFileCommaDelimiter = False
                    .TextFileSpaceDelimiter = False
                    .TextFileOtherDelimiter = "|"
                    .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2)
                    .TextFileTrailingMinusNumbers = True
                    .Refresh BackgroundQuery:=False
                End With
            End With
        Next FilNamCntr
    Exit Sub
    ErrorCatch:
    MsgBox "Unexpected Error. Type: " & Err.Description
    End Sub
6
  • How do I declare an array variable in VBA? Commented Feb 14, 2015 at 12:10
  • 1
    Wow, great. Got it now. Problem was when I tried to declare a 2nd array as Long I got an error it expected an array. It seems I forgot to ReDim this array before its usage which seems that solved my problem now. Thanks a lot! :) Commented Feb 14, 2015 at 12:22
  • 1
    you should show the changes you made in an answer Commented Feb 14, 2015 at 12:27
  • Updated in the original post with the code which works now. Thanks again for help. Commented Feb 14, 2015 at 13:23
  • 1
    On Stackoverflow it is preferred that you put the solution in the answer box below the question and then accept it. This is much better than editing the solution into the question, and you can get rewarded for it too. Commented Apr 30, 2015 at 20:10

1 Answer 1

0

Solution by OP.

I managed to modify the code so the macro works now. I forgot to ReDim the array when defined as Long instead of Variant. Here is the updated code:

Option Explicit
Public Sub FileImport_Multiple()
Dim qry             As QueryTable
Dim FilNams         As Variant
Dim FilNams_Rows    As Long  '<< Changed here from Variant to Long
Dim FilNamCntr      As Long
Dim strQryName      As String
Dim LastRow         As Long
Dim ContainerWB     As Workbook
Dim msgString       As String
Dim RowCounter01    As Long
Dim RowCounter02    As Long
Dim wB              As Workbook
    FilNams = Application.GetOpenFilename(FileFilter:="Text Files (*.txt),*.txt", _
                                               Title:="Select text files to Import", _
                                               MultiSelect:=True)

    '===========================
    'Defining a name of this sheet to variable and setting up a statusbar
    '===========================

    Tool_name = ActiveWorkbook.Name

    Range("A1").Select

    'Check to see if any files were selected
    If TypeName(FilNams) = "Boolean" Then
            MsgBox "No Files Selected. Exiting Program."
            Exit Sub
        Else
            'msgString = Join(FilNams, vbCr)
            'MsgBox "FilNams is: " & msgString
    End If

    For FilNamCntr = LBound(FilNams) To UBound(FilNams)
            ReDim Preserve FilNams_Rows(FilNamCntr)  '<< Here I forgot to ReDim
            File_To_Be_Checked = FilNams(FilNamCntr)
            Call File_Lenght_Checker
            FilNams_Rows(FilNamCntr) = File_To_Be_Checked_Rows
    Next FilNamCntr

    For FilNamCntr = LBound(FilNams) To UBound(FilNams)
        FilNams(FilNamCntr) = "TEXT;" & FilNams(FilNamCntr)
    Next FilNamCntr

    'msgString = Join(FilNams, vbCr)
    'MsgBox "FilNams is: " & msgString
    For FilNamCntr = LBound(FilNams) To UBound(FilNams)
        With ActiveSheet
            On Error GoTo ErrorCatch:
            RowCounter01 = .Cells(.Rows.Count, "D").End(xlUp).Row
            'Append to previous data, if applicable
            If .Range("D" & Rows.Count).End(xlUp).Row = 1 Then
                    LastRow = 1
                Else
                    LastRow = .Range("D" & Rows.Count).End(xlUp).Row + 1
            End If
            If (RowCounter01 + FilNams_Rows(FilNamCntr)) >= 1048576 Then
                    MsgBox "Not enough space to import text files. Exiting.."
                    Exit Sub
            End If
            'MsgBox "LastRow value is:" & LastRow 'verification test
            Set qry = .QueryTables.Add(Connection:=FilNams(FilNamCntr), _
                                    Destination:=.Range("D" & LastRow))
            With qry
                .Name = "Filename"
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False
                .TextFilePlatform = 437
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = False
                .TextFileOtherDelimiter = "|"
                .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
        End With
    Next FilNamCntr
Exit Sub
ErrorCatch:
MsgBox "Unexpected Error. Type: " & Err.Description
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.