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