Cannot able to store value in a array - VBA - arrays

I am trying to store a filename in a array but i am getting Type mismatch error. I have changed the data type but it didn't work. Kindly help.
The code block that is throwing error,
Sub Example2()
Dim objFile,objFile1,objFolder,objFolder1 As Object
Dim splitting, counter, filename, filename1, splitting1, counter1,As Variant
Dim myarray() As Variant
For Each objFile In objFolder.Files
splitting = Split(objFile.Name, "\", 9)
counter = UBound(splitting)
filename = splitting(counter)
For Each objFile1 In objFolder1.Files
splitting1 = Split(objFile1.Name, "\", 9)
counter1 = UBound(splitting1)
filename1 = splitting1(counter1)
If srch1 = srch2 Then
ReDim Preserve myarray(UBound(myarray) + 1)
myarray() = filename1
End If
Next
Next

Get File Paths (to Array) Function
Links
Objects
FileSystemObject Object
GetFolder Method
File Object
The Code
Option Explicit
Function getFilePaths(ByVal FolderPath As String, _
Optional ByVal FirstIndex As Long = 1) _
As Variant
Dim fso As Object
Dim fsoFldr As Object
Dim fsoFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFldr = fso.GetFolder(FolderPath)
Dim LastIndex As Long
LastIndex = FirstIndex - 1
Dim Data() As Variant
For Each fsoFile In fsoFldr.Files
LastIndex = LastIndex + 1
ReDim Preserve Data(FirstIndex To LastIndex)
Data(LastIndex) = fsoFile.Path ' or .Name, .ParentFolder ...
Next fsoFile
getFilePaths = Data
End Function
Sub TESTgetFilePath()
' Define Folder Path ('fPath').
Const fPath As String = "F:\Test\2020"
' Populate File Paths Array ('Data').
Dim Data As Variant
Data = getFilePaths(fPath)
' Validate File Paths Array.
If IsEmpty(Data) Then
MsgBox "No files found.", vbCritical, "Fail"
Exit Sub
End If
' Write title to the Immediate window (CTRL+G).
Debug.Print "The List"
' Write values from File Paths Array to a String ('Result').
Dim Result As String
Result = Join(Data, vbLf)
' Write file paths to the Immediate window (CTRL+G).
Debug.Print Result
End Sub
EDIT 1:
Sub Example2()
Const FolderPath As String = "C:\Test"
Dim fso As Object
Dim objFolder As Object
Dim objFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(FolderPath)
Dim LastIndex As Long
LastIndex = -1
Dim MyArray() As Variant
For Each objFile In objFolder.Files
LastIndex = LastIndex + 1
ReDim Preserve MyArray(LastIndex)
MyArray(LastIndex) = objFile.Name
Next objFile
Dim n As Long
For n = LBound(MyArray) To UBound(MyArray)
Debug.Print n, MyArray(n)
Next n
End Sub
EDIT 2:
Sub Example3()
' For a fileformat aaa-bbb-rev*.*, where 'rev' is to be tested if greater.
' Two hyphens only.
Const FolderPath As String = "F:\Test\2020\64568450"
Const fSep As String = "-"
Dim pSep As String
pSep = Application.PathSeparator
Dim fso As Object
Dim objFolder As Object
Dim objFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(FolderPath)
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim FileParts As Variant ' An array containing the split file name.
Dim fName As String ' File part before the 2nd hyphen (minus) '-'
Dim fRevision As String ' File part after the 2nd hyphen (minus) '-'
Dim LastIndex As Long
LastIndex = -1
Dim MyArray() As Variant
' Write file paths to array.
For Each objFile In objFolder.Files
FileParts = Split(objFile.Name, fSep)
fName = FileParts(0) & fSep & FileParts(1)
fRevision = FileParts(2)
If Not dict.Exists(fName) Then
dict(fName) = fRevision
Else
LastIndex = LastIndex + 1
ReDim Preserve MyArray(LastIndex)
If dict(fName) < fRevision Then
MyArray(LastIndex) = FolderPath & pSep & fName _
& fSep & fRevision
dict(fName) = fRevision
Else
MyArray(LastIndex) = objFile.Path
End If
End If
Next objFile
' Now 'MyArray' contains the list of file paths of the files to be moved.
Dim n As Long
For n = LBound(MyArray) To UBound(MyArray)
Debug.Print n, MyArray(n)
Next n
End Sub

Arrays in VBA can be either static or dynamic.
A static array is declared with a fixed size:
Dim myStaticArr(10) As String
declares an array with a fixed number of members (usually 11 as the lower index starts at 0, but you can overwrite this).
If you want to be sure about the lower index, you can specify
Dim myStaticArr(1 to 10) As String
Now you have 10 elements (from 1 to 10).
Similar, a multidimensional array can be defined
Dim myStaticArr3D(1 to 10, 1 to 5, 1 to 8) As String
Now you have an array with 10 * 5 * 8 members.
All of these arrays have in common that you need to declare at compile time the size of the array. The VBA compiler will reserve the necessary amount of memory and you cannot resize it.
If you don't know at compile time how large your array will be, you can declare it as dynamic array (as you do)
Dim myDynamicArr() as String
This reserves no memory at all. Before you can write something into the array, you need to tell VBA how big the array will be. This is done using the Redim statement. Easiest form:
Redim myDynamicArr(1 to 10) as String
Usually, this is done after calculating the size needed, so you will usually find the Redim having a variable that was used to calculate the needed size:
Redim myDynamicArr(1 to sizeNeeded) as String
Now there are cases where you find at runtime that the needed size is too small. You can issue another Redim to increase the size - but as you want to keep the content of the array, you specify the option Preserve:
Redim Preserve myDynamicArr(1 to 2*sizeNeeded) as String
This will double the size and keep the content of the first members (omitting the Preserve option will double the size but the content of the existing members will get lost).
To get the current size of an array, you can use the functions LBound and UBound. This can be used on static and dynamic arrays:
Dim myStaticArr(5 to 99) As String
Debug.Print LBound(myStaticArr), UBound(myStaticArr)
>> 5 99
Dim myDynamicArr() As String
ReDim myDynamicArr(1 to 20)
Debug.Print LBound(myDynamicArr), UBound(myDynamicArr)
>> 1 20
However, if you have a dynamic array and you never assigned memory to it, the functions LBound and UBound will throw a runtime error 9 "Subscript out of range"
Now what you want to do is to increase the size of the array by 1 every time you find a new value. You achieve this with
ReDim Preserve myarray(UBound(myarray) + 1)
which will look to the current size of the array using the UBound-function and resize it by 1, preserving its contents. That's fine, except for the fact that the very first time this statement is hit, the size of the array is undefined.
The easiest way to handle this is to use a variable that keeps track of your array size:
Dim myArray() as String, myArraySize as Long
(...)
myArraySize = myArraySize + 1
ReDim Preserve myArray(1 to myArraySize)
myarray(myArraySize) = filename1
One remark: ReDim Preserve is a rather expensive command. If you are dealing with a few entries, this doesn't matter, but if you are dealing with 100s or 1000s of elements, you should consider to use a Collection.

You should index to the array to set the value after Redim:
ReDim Preserve myarray(UBound(myarray) + 1)
myarray(ubound(myarray)) = filename1
VBA arrays are so finicky and frustrating. I would add items to a string and after then split it to an array:
Dim strArray As String
strArray = ""
REM .....
strArray = strArray + filename1 + ","
REM .....
myarray = Split(strArray,",")

Related

VBA Assigning Array to Range and Writing to Sheet returning all zeros

I am trying to assign an array to a range of values in an Excel sheet.
When I do though, even though using debug the array is not all zeros, it returns all zeros.
The weird thing is for the dat1 variable it does write to the cells correctly. Though that along with dat2 is an array of strings.
Thanks in advance.
Sub Comparor()
Dim dat1() As Variant
Dim dat2() As Variant
dat1() = Sheets("Data1").Range("E1:E10").Value2
dat2() = Sheets("Data2").Range("E1:E10").Value2
Dim iTemp As Integer
iTemp = CInt(UBound(dat1))
Dim NumMatches() As Integer
ReDim NumMatches(iTemp)
Dim iNum As Integer
Dim iCompareInner As Integer 'dat 2 cycler
Dim iCompareOuter As Integer 'dat 1 cycler
For iCompareOuter = 1 To UBound(dat1)
For iCompareInner = 1 To UBound(dat2)
If (dat1(iCompareOuter, 1) = dat2(iCompareInner, 1)) Then
NumMatches(iCompareOuter) = NumMatches(iCompareOuter) + 1
End If
Next iCompareInner
Next iCompareOuter
Dim test22(10, 1) As Integer
For iNum = 1 To UBound(NumMatches)
'Debug.Print NumMatches(iNum)
test22(iNum, 1) = NumMatches(iNum)
Debug.Print test22(iNum, 1)
Next iNum
Sheets("Info").Range("E1:E10").Value2 = dat1
Sheets("Info").Range("F1:F10").Value2 = test22
Sheets("Info").Range("G1:G10").Value2 = NumMatches
End Sub
Count Matches (Dictionary, CountIf, Array (Double-Loop))
All three solutions do the same thing.
Using them with some serious data, e.g. 1K uniques on 100K values (means e.g. 100M iterations in the array version) will reveal the efficiency of each code.
But this is more about 2D one-based (one-column) arrays commonly used with (one-column) ranges.
The code is basic i.e. no blanks or error values are expected and each range has at least 2 cells
(i.e. Data = rg.Value with one cell doesn't work).
Option Explicit
Sub ComparorDictionary()
' Reference the workbook ('wb').
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Read values (duplicates)
Dim vws As Worksheet: Set vws = wb.Worksheets("Data2")
Dim vData() As Variant: vData = vws.Range("E1:E10").Value
Dim vrCount As Long: vrCount = UBound(vData, 1)
' Count matches using a dictionary.
Dim vDict As Object: Set vDict = CreateObject("Scripting.Dictionary")
vDict.CompareMode = vbTextCompare
Dim vr As Long
For vr = 1 To vrCount
vDict(vData(vr, 1)) = vDict(vData(vr, 1)) + 1
Next vr
Erase vData ' values data is counted in the dictionary
' Read uniques (no duplicates).
Dim uws As Worksheet: Set uws = wb.Worksheets("Data1")
Dim uData() As Variant: uData = uws.Range("E1:E10").Value
Dim urCount As Long: urCount = UBound(uData, 1)
' Write count.
Dim uMatches() As Long: ReDim uMatches(1 To urCount, 1 To 1)
Dim ur As Long
For ur = 1 To urCount
If vDict.Exists(uData(ur, 1)) Then
uMatches(ur, 1) = vDict(uData(ur, 1))
End If
Next ur
Set vDict = Nothing ' data is in the unique arrays
' Write result.
Dim dws As Worksheet: Set dws = wb.Worksheets("Info")
dws.Range("E1").Resize(urCount).Value = uData
dws.Range("F1").Resize(urCount).Value = uMatches
End Sub
Sub ComparorCountIf()
' Reference the workbook ('wb').
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Reference values (duplicates). No array is needed.
Dim vws As Worksheet: Set vws = wb.Worksheets("Data2")
Dim vrg As Range: Set vrg = vws.Range("E1:E10")
' Read uniques (no duplicates).
Dim uws As Worksheet: Set uws = wb.Worksheets("Data1")
Dim uData() As Variant: uData = uws.Range("E1:E10").Value
Dim urCount As Long: urCount = UBound(uData, 1)
' Count matches and write the count.
Dim uMatches() As Long: ReDim uMatches(1 To urCount, 1 To 1)
Dim ur As Long
For ur = 1 To urCount
uMatches(ur, 1) = Application.CountIf(vrg, uData(ur, 1))
Next ur
' Write result.
Dim dws As Worksheet: Set dws = wb.Worksheets("Info")
dws.Range("E1").Resize(urCount).Value = uData
dws.Range("F1").Resize(urCount).Value = uMatches
End Sub
Sub ComparorArray()
' Reference the workbook ('wb').
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Read values (duplicates).
Dim vws As Worksheet: Set vws = wb.Worksheets("Data2")
Dim vData() As Variant: vData = vws.Range("E1:E10").Value
Dim vrCount As Long: vrCount = UBound(vData, 1)
' Read uniques (no duplicates).
Dim uws As Worksheet: Set uws = wb.Worksheets("Data1")
Dim uData() As Variant: uData = uws.Range("E1:E10").Value
Dim urCount As Long: urCount = UBound(uData, 1)
' Count matches and write the count.
Dim uMatches() As Long: ReDim uMatches(1 To urCount, 1 To 1)
Dim vr As Long
Dim ur As Long
For ur = 1 To urCount
For vr = 1 To vrCount
If uData(ur, 1) = vData(vr, 1) Then
uMatches(ur, 1) = uMatches(ur, 1) + 1
End If
Next vr
Next ur
Erase vData ' data is in the unique arrays
' Write result.
Dim dws As Worksheet: Set dws = wb.Worksheets("Info")
dws.Range("E1").Resize(urCount).Value = uData
dws.Range("F1").Resize(urCount).Value = uMatches
End Sub
As I said in my comment, one of your declarations is wrong and because of that, the unexpected result. Please, try understanding the next (didactic) code, to clarify the issue:
Sub testArray1D2D()
Dim arr1D, arr2DStrange, arr2D, i As Long
arr1D = Split("a,b,c,d,e,f,g,h,i,j", ",")
ReDim arr2DStrange(10, 1): ReDim arr2D(1 To 10, 1 To 1)
For i = 0 To UBound(arr1D)
arr2DStrange(i, 1) = arr1D(i)
arr2D(i + 1, 1) = arr1D(i)
Next i
Range("A2").Resize(UBound(arr2DStrange), 1).value = arr2DStrange 'it returns nothing
Range("B2").Resize(UBound(arr2DStrange), 2).value = arr2DStrange 'it returns what you need in the second column (D:D)
Range("D2").Resize(UBound(arr2D), 1).value = arr2D 'it returns correctly (what you need)
Range("E2").Resize(UBound(arr1D) + 1, 1).value = Application.Transpose(arr1D) 'also correct (a 1D array does not have any column! and it must be transposed. Otherwise, it repeats its first element value)
End Sub
When use declaration Dim test22(10, 1) As Integer it creates a 2D array but it has two columns. It is the equivalent of Dim test22(0 to 10, 0 to 1) As Integer. When you fill only the second column (1) and try returning the first one (0), this column, is empty.
The correct declaration for obtaining a 2D array with 10 rows and 1 column should be Dim test22(1 to 10, 1 to 1) As Integer.
Then, iTemp = CInt(UBound(dat1)) declares a 1D array of 11 elements (from 0, inclusive, to 10). And you never loaded its first element, starting iteration with 1. That's why the line Sheets("Info").Range("G1:G10").Value2 = NumMatches returned the first empty element 10 times... If your code would fill correctly the first element and if it was a matching one, your code will return 10 rows of 1 value.
NumMatches(iCompareOuter) = NumMatches(iCompareOuter) + 1 is the equivalent of NumMatches(iCompareOuter) = 1. NumMatches(iCompareOuter) is always empty in that moment...
And it is good to cultivate the habit to avoid declarations As Integer in such a case. Working with Excel rows, the value of an Integer must be exceeded. Try using As Long. VBA is so designed to make the memory working in the same way, without any supplementary stress.
A more compact way to accomplish what you need will be the next approach:
Sub Comparor()
Dim dat1(), dat2(), NumMatches(), mtch, i As Long
dat1() = Sheets("Data1").Range("E1:E10").Value2
dat2() = Sheets("Data2").Range("E1:E10").Value2
ReDim NumMatches(1 To UBound(dat1), 1 To 1)
For i = 1 To UBound(dat1)
mtch = Application.match(dat1(i, 1), dat2, 0)
If IsNumeric(mtch) Then NumMatches(i, 1) = "OK"
Next i
Sheets("Info").Range("G1:G10").Value2 = NumMatches
End Sub
Not tested, but it should work. Except the case of a typo, when an error will be raised and sending some feedback I will rapidly correct...
This for example
Dim test22(10, 1) As Integer
in the absence of Option Base 1 is the same as
Dim test22(0 to 10, 0 to 1) As Integer
I'd use
Dim test22(1 to 10, 1 to 1) As Integer
if you want to match the arrays you read from the worksheet. Otherwise, dropping those arrays to a range only gives you the first "column" (which are all zeros since you never assigned anything there...)

printing array only resulting in first value being printed into excel?

Assigning word document lines of text to an array to then print into an excel column. I want to print each item in array to it's own cell.
Currently, all the items are storying correctly into the array, but it's only printing the first item over and over Action
Code:
Option Explicit
Sub ParaCopy()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
Set wDoc = wApp.Documents.Open("J:\Data Dictionary.docx", ReadOnly:=True)
Dim wPara As Word.Paragraph
Dim arr() As Variant
Dim i As Long
i = 0
For Each wPara In wDoc.Paragraphs
If wPara.Range.Words.Count > 1 Then
ReDim Preserve arr(i)
arr(i) = wPara.Range
End If
i = i + 1
Next wPara
For i = LBound(arr) To UBound(arr)
[a1].Resize(UBound(arr) + 1) = arr
Next i
End Sub
EDIT: Need to separate each block of text separated by a space (outlined in blue) to this
Create a 2D array with one column and load that:
Option Explicit
Sub ParaCopy()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
Set wDoc = wApp.Documents.Open("J:\Data Dictionary.docx", ReadOnly:=True)
Dim wPara As Word.Paragraph
Dim arr() As Variant
ReDim arr(1 To wDoc.Paragraphs.Count, 1 To 1)
Dim i As Long
i = 1
For Each wPara In wDoc.Paragraphs
If wPara.Range.Words.Count > 1 Then
arr(i, 1) = wPara.Range
i = i + 1
End If
Next wPara
[a1].Resize(UBound(arr) + 1) = arr
End Sub
Copy Word Paragraphs to Excel Cells Using an Array
The number of rows of the array is wDoc.Paragraphs.Count which may differ from r (the 'actual count') hence you have to use r with Resize, and not wDoc.Paragraphs.Count or UBound(Data, 1).
Don't forget to Close the Document and Quit the App.
The first solution is early-bound and needs the library reference. When using it, just use
Set wApp = New Word.Application.
The second solution is late-bound and doesn't need the library reference. Also, it has been 'stripped off' the document and application variables (not necessary, you can declare them As Object).
Option Explicit
' e.g. Tools>References>Microsoft Word 16.0 Object Library
Sub ParaCopy()
Const FilePath As String = "J:\Data Dictionary.docx"
Dim wApp As Word.Application: Set wApp = Set wApp = New Word.Application
Dim wDoc As Word.Document: Set wDoc = wApp.Documents.Open(FilePath, , True)
Dim Data As Variant: ReDim Data(1 To wDoc.Paragraphs.Count, 1 To 1)
Dim wPara As Word.Paragraph
Dim r As Long
For Each wPara In wDoc.Paragraphs
If wPara.Range.Words.Count > 1 Then
r = r + 1
Data(r, 1) = wPara.Range
End If
Next wPara
wDoc.Close False
wApp.Quit
[a1].Resize(r) = Data
End Sub
Sub ParaCopyNoReference()
Const FilePath As String = "J:\Data Dictionary.docx"
With CreateObject("Word.Application")
With .Documents.Open(FilePath, , True)
Dim Data As Variant: ReDim Data(1 To .Paragraphs.Count, 1 To 1)
Dim wPara As Object
Dim r As Long
For Each wPara In .Paragraphs
If wPara.Range.Words.Count > 1 Then
r = r + 1
Data(r, 1) = wPara.Range
End If
Next wPara
.Close False
End With
.Quit
End With
[a1].Resize(r) = Data
End Sub

edit dimension of array vba

I have an array like this
dim arr(1 to 5) as string
arr(1)="a"
arr(3)="b"
arr(5) = "c"
(arr(2),arr(4) are empty).
How can I redim this arr(1to5) to exclude empty values and save also values "a","b","c" (I want the output like arr(1to3), arr(1)="a", arr(2)="b", arr(3)="c")?
In general I do not know how many of them will be empty, so I need some general code for this (not for this specific example).
I was thinking about new temporary array to save all nonempty values and then redim arr(1to5).
Maybe it is a better (quick) way to do it?
I wrote sth similar:
Sub test()
Dim myArray() As String
Dim i As Long
Dim y As Long
ReDim Preserve myArray(3)
myArray(1) = "a"
myArray(3) = "c"
Dim myArray2() As String
y = 1
For i = LBound(myArray) To UBound(myArray)
If myArray(i) <> "" Then
ReDim Preserve myArray2(y)
myArray2(y) = myArray(i)
y = y + 1
End If
Next i
ReDim myArray(UBound(myArray2))
myArray = myArray2
End Sub
However I would like to avoid creating new array.
create a new array of the same size. Loop the first array and insert the values when not empty into the new array keeping track of the last spot with value in the new array, then redim preserve the new array to only the size that has values.
Sub kjlkj()
Dim arr(1 To 5) As String
arr(1) = "a"
arr(3) = "b"
arr(5) = "c"
Dim newArr() As String
ReDim newArr(1 To UBound(arr))
Dim j As Long
j = LBound(newArr)
Dim i As Long
For i = LBound(arr) To UBound(arr)
If arr(i) <> "" Then
newArr(j) = arr(i)
j = j + 1
End If
Next i
ReDim Preserve newArr(LBound(newArr) To j - 1)
'do what you want with the new array.
End Sub
Alternative via Filter() function
"However I would like to avoid creating new array."
A negative filtering allows a basically simple alternative, however you have to
declare your array dynamically (i.e. without preset number of elements) to allow a rebuild overwriting the original array,
execute a double replacement over the joined array elements to allow insertion of a unique character that can be filtered out.
Sub testFilter()
Dim arr() As String
ReDim arr(1 To 5)
arr(1) = "a"
arr(3) = "b"
arr(5) = "c"
'Debug.Print Join(arr, ",") ' ~~> a,,b,,c
'rearrange arr via RemoveEmpty()
arr = RemoveEmpty(arr) ' >> function RemoveEmpty()
Debug.Print Join(arr, ",") ' ~~> a,b,c
End Sub
Help function RemoveEmpty()
Adding an unused unique character, e.g. $, to the empty elements plus eventual negative filtering allows to remove these marked elements.
Note that the double replacement is necessary to allow to mark consecutive empty elements by the $ mark, as VBA would skip additional characters here.
Function RemoveEmpty(arr)
Dim tmp
tmp = Replace(Replace(Join(arr, "|"), "||", "|$|"), "||", "|$|")
RemoveEmpty = Filter(Split(tmp, "|"), "$", False)
End Function

VBA moving rows through arrays

I am pretty new with arrays in VBA, and need some help finishing a code...
The objective is to copy from one array to another if a value in the first part of the array is found.
Here's what I have so far, and I have put comments in the lines that I am struggling with.
Option Explicit
Sub ReadingRange()
Dim ARRAY_Multiwage As Variant
Dim ARRAY_TEMP_Multiwage() As Variant
ARRAY_Multiwage = Sheets("Multiwage").Range("A1").CurrentRegion
Dim a As Long
Dim b As Long
For a = LBound(ARRAY_Multiwage, 1) To UBound(ARRAY_Multiwage, 1)
If ARRAY_Multiwage(a, 1) = "60021184_2018/36/HE" Then
'add ARRAY_Multiwage(a, 1) to ARRAY_TEMP_Multiwage
'Debug print to see that it has been added
Else:
End If
Next a
End Sub
Any help would be greatly appreciated
Try this out. What you are looking for is ReDim option to dynamically expand an array before entering data into the newest slot.
Sub ReadingRange()
Dim ARRAY_Multiwage As Variant
Dim ARRAY_TEMP_Multiwage() As String
ARRAY_Multiwage = Sheets("Sheet2").Range("A1").CurrentRegion
Dim a As Long
Dim b As Long
' c is the counter that helps array become larger dynamically
Dim c As Long
c = 0
For a = LBound(ARRAY_Multiwage, 1) To UBound(ARRAY_Multiwage, 1)
If ARRAY_Multiwage(a, 1) = "60021184_2018/36/HE" Then
' change the dimension of the array
ReDim Preserve ARRAY_TEMP_Multiwage(c)
' add data to it
ARRAY_TEMP_Multiwage(c) = ARRAY_Multiwage(a, 1)
' print what was added
Debug.Print ("Ubound is " & UBound(ARRAY_TEMP_Multiwage) & ". Latest item in array is " & ARRAY_TEMP_Multiwage(UBound(ARRAY_TEMP_Multiwage)))
' get ready to expand the array
c = c + 1
Else:
End If
Next a
End Sub
I tend to use a Long data type variable as a counter within the loop for the destination array, that way each time the array is accessed, a new element can be written to. In past I've been steered towards declaring the new array with the maximum upper bound it could hold and resize it once at the end so the below example will follow that.
Option Explicit
Sub ReadingRange()
Dim ARRAY_Multiwage As Variant
Dim ARRAY_TEMP_Multiwage() As Variant
ARRAY_Multiwage = Sheets("Multiwage").Range("A1").CurrentRegion
Dim a As Long
Dim b As Long
Dim ArrayCounter as Long
ArrayCounter = 1 'Or 0, depends on if you are using a zero based array or not
For a = LBound(ARRAY_Multiwage, 1) To UBound(ARRAY_Multiwage, 1)
If ARRAY_Multiwage(a, 1) = "60021184_2018/36/HE" Then
ARRAY_TEMP_Multiwage(ArrayCounter) = ARRAY_Multiwage(a, 1)
Debug.Print ARRAY_TEMP_Multiwage(ArrayCounter)
ArrayCounter = ArrayCounter + 1
Else
'Do nothing
End If
Next a
ReDim Preserve ARRAY_TEMP_Multiwage (1 To (ArrayCounter - 1))
End Sub
Copy Range With Criteria
The following will copy from worksheet Sourceultiwage to worksheet
Targetultiwage both in ThisWorkbook, the workbook containing this
code.
Adjust the values in the constants section including wb.
Additionally you can choose to copy headers (copyHeaders)
The Code
Option Explicit
Sub copyWithCriteria()
' Source
Const srcName As String = "Sourceultiwage"
Const srcFirst As String = "A1"
' Target
Const tgtName As String = "Targetultiwage"
Const tgtFirst As String = "A1"
' Criteria
Const CriteriaColumn As Long = 1
Const Criteria As String = "60021184_2018/36/HE"
' Headers
Const copyHeaders As Boolean = False
' Workboook
Dim wb As Workbook: Set wb = ThisWorkbook
' Write values from Source Range to Source Array.
Dim rng As Range
Set rng = wb.Worksheets(srcName).Range(srcFirst).CurrentRegion
Dim NoR As Long
NoR = WorksheetFunction.CountIf(rng.Columns(CriteriaColumn), Criteria)
Dim Source As Variant: Source = rng.Value
' Write values from Headers Range to Headers Array.
If copyHeaders Then
Dim Headers As Variant: Headers = rng.Rows(1).Value
End If
' Write from Source to Target Array.
Set rng = Nothing
Dim UB1 As Long: UB1 = UBound(Source)
Dim UB2 As Long: UB2 = UBound(Source, 2)
Dim Target As Variant: ReDim Target(1 To NoR, 1 To UB2)
Dim i As Long, j As Long, k As Long
For i = 1 To UB1
If Source(i, CriteriaColumn) = Criteria Then
k = k + 1
For j = 1 To UB2
Target(k, j) = Source(i, j)
Next j
End If
Next i
' Write from Target Array to Target Range.
With wb.Worksheets(tgtName).Range(tgtFirst)
If copyHeaders Then .Resize(, UB2).Value = Headers ' Headers
.Offset(Abs(copyHeaders)).Resize(NoR, UB2).Value = Target ' Data
End With
' Inform user.
MsgBox "Data transferred.", vbInformation, "Success"
End Sub

ReDim Preserve raise error Subscript out of range

Getting a
Run-time : Error No. 9
Subscript out of range
at the line:
ReDim Preserve aryFileNames(UBound(aryFileNames) - 1)
In the code below which is meant to convert text files to Excel files in a particular folder.
Sub ConvertTextFiles()
Dim fso As Object '<---FileSystemObject
Dim fol As Object '<---Folder
Dim fil As Object '<---File
Dim strPath As String
Dim aryFileNames As Variant
Dim i As Long
Dim wbText As Workbook
Application.ScreenUpdating = False
'// I am assuming the textfiles are in the same folder as the workbook with //
'// the code are. //
strPath = ThisWorkbook.Path & Application.PathSeparator
'// Set a reference to the folder using FSO, so we can use the Files collection.//
Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder(strPath)
'// Using FSO's Files collection, we'll run through and build an array of //
'// textfile names that exist in the folder. //
ReDim aryFileNames(0)
For Each fil In fol.Files
If fil.Type = "Text Document" Then
'// If correct Type (a text file), we'll assign the name of the found //
'// textfile to the last element in the array - then add an empty //
'// element to the array for next loop around... //
aryFileNames(UBound(aryFileNames)) = fil.Name
ReDim Preserve aryFileNames(UBound(aryFileNames) + 1)
End If
Next
'// ... now since we were adding an empty element to the array, that means we'll//
'// have an emmpty ending element after the above loop - get rid of it here. //
ReDim Preserve aryFileNames(UBound(aryFileNames) - 1)
'// Basically, For Each element in the array... //
For i = LBound(aryFileNames) To UBound(aryFileNames)
'// ...open the textfile, set a reference to it, SaveAs and Close. //
Workbooks.OpenText Filename:=strPath & aryFileNames(i), _
Origin:=xlWindows, _
StartRow:=1, _
DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), _
Array(7, 1), _
Array(55, 1), _
Array(68, 1))
Set wbText = ActiveWorkbook
wbText.Worksheets(1).Columns("A:D").EntireColumn.AutoFit
wbText.SaveAs Filename:=strPath & Left(aryFileNames(i), Len(aryFileNames(i)) - 4), _
FileFormat:=xlWorkbookNormal
wbText.Close
Next
Application.ScreenUpdating = True
End Sub
You'll get a subscript out of range any time your For Each loop doesn't execute or you don't find any text documents. The starting bound of the array is 0 and in that case it never gets incremented, so this line of code...
ReDim Preserve aryFileNames(UBound(aryFileNames) - 1)
...is trying to size the array to a bound of -1. Since you're working with strings, you can take advantage of a quirk in the Split function to simplify your array sizing. If you Split a vbNullString, VBA will return a String array with a UBound of -1. Instead of initializing it with ...
ReDim aryFileNames(0)
... and then trimming it afterward, you can just do this:
aryFileNames = Split(vbNullString)
'UBound of the array is now -1.
For Each fil In fol.Files
If fil.Type = "Text Document" Then
ReDim Preserve aryFileNames(UBound(aryFileNames) + 1)
aryFileNames(UBound(aryFileNames)) = fil.Name
End If
Next
'Array is correct size - you can skip "trimming" it.
'ReDim Preserve aryFileNames(UBound(aryFileNames) - 1)

Resources