Array with 2 Dimensions

jagrenet

Board Regular
Joined
Feb 23, 2022
Messages
81
Office Version
  1. 365
  2. 2013
  3. 2011
  4. 2010
Platform
  1. Windows
Hello Gurus,

I am working with an Array(arr1) that is comparing a known or "static" list of filenames against the contents of a specific Folder. A loop runs through the list of filenames and checks the folder to verify if the file is there or not.
Dimension "1" of the Array is the filename ("i"). - Sheet1.Range("E11:E22")
Dimension "2" of the Array is the result of the comparison ("J"). - Uses Offset to indicate if file is there. It puts a "1" in the corresponding row of Column F

Everything is working beautifully - VERY well.
Now, what I would like to do, ..... since "J" already returns the count of "missing" files correctly, I want to take each missing file and create another Array (arr2), and have it return the filename to arr2, rather that the .Value of "J" which is simply 1 or "" (Empty).
And this is where I am getting stuck.

I have tried a few different code functions but still only get a numeric from "J".


Dim arr1 As Variant
arr1 = Sheet1.Range("E11").CurrentRegion 'Pass REQUIRED FILE List to Array

Dim arr2 As Variant

Dim i As Long, J As Variant

For i = LBound(arr1, 1) To UBound(arr1, 1) 'Enumerate(Index) "NAME" of file (Column "E")
For J = LBound(arr1, 2) To UBound(arr1, 2) 'Enumerate "COUNT" of file, if it exists in the folder (Column "F")

Debug.Print i, J, arr1(i, J)

Next J
Next i

Sheet1.Range("F25").Formula = "=SUM(F11:F22)"
tResult = Sheet1.Range("F25")

For Each J In arr1
If arr1(J) = "" Then
arr2 = i
Exit For
End If
Next
What am I missing ??

Thanks in advance,
Jeff
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Jeff,

maybe

VBA Code:
Public Sub MrE_1232987_1705009()
' https://www.mrexcel.com/board/threads/array-with-2-dimensions.1232987/
Dim arr1 As Variant
Dim arr2 As Variant
Dim i As Long, j As Long

arr1 = sheet1.Range("E11").CurrentRegion 'Pass REQUIRED FILE List to Array
ReDim arr2(1 To 1)

For i = LBound(arr1, 1) To UBound(arr1, 1) 'Enumerate(Index) "NAME" of file (Column "E")
  If arr1(i, 2) = 1 Then
    j = j + 1
    ReDim Preserve arr2(1 To j)
    arr2(j) = arr1(i, 1)
  End If
Next i

For j = LBound(arr2) To UBound(arr2)
  Debug.Print j, arr2(j)
Next i

End Sub

Ciao,
Holger
 
Upvote 0
Hi Jeff,

maybe

VBA Code:
Public Sub MrE_1232987_1705009()
' https://www.mrexcel.com/board/threads/array-with-2-dimensions.1232987/
Dim arr1 As Variant
Dim arr2 As Variant
Dim i As Long, j As Long

arr1 = sheet1.Range("E11").CurrentRegion 'Pass REQUIRED FILE List to Array
ReDim arr2(1 To 1)

For i = LBound(arr1, 1) To UBound(arr1, 1) 'Enumerate(Index) "NAME" of file (Column "E")
  If arr1(i, 2) = 1 Then
    j = j + 1
    ReDim Preserve arr2(1 To j)
    arr2(j) = arr1(i, 1)
  End If
Next i

For j = LBound(arr2) To UBound(arr2)
  Debug.Print j, arr2(j)
Next i

End Sub

Ciao,
Holger
Hi Holger,

Yes, ..... that is very close to what I am needing however, just the inverse. - "j" represents the files that are missing and those are the files I wish to reference by filename. "1" means the file exists. Blank means file is missing. I am trying to get the names of files missing. The Column on the right is Column F which is the result of "j". From my original post above - (Dimension "2" of the Array is the result of the comparison ("J"). - Uses Offset to indicate if file is there. It puts a "1" in the corresponding row of Column F). I hope this makes sense. I think you are on the right track ....... just need to "invert" ReDim somehow.

1679393815598.png
 
Upvote 0
Hi Jeff,

looking at the code the Next for the second loop should not be for i but for j (my very bad habit of changing variables and not testing).

From what I understand just change

VBA Code:
  If arr1(i, 2) = 1 Then

to either

VBA Code:
  If arr1(i, 2) = "" Then
or
VBA Code:
  If arr1(i, 2) = vbNullString Then

Ciao,
Holger
 
Upvote 1
Solution
Hi Jeff,

looking at the code the Next for the second loop should not be for i but for j (my very bad habit of changing variables and not testing).

From what I understand just change

VBA Code:
  If arr1(i, 2) = 1 Then

to either

VBA Code:
  If arr1(i, 2) = "" Then
or
VBA Code:
  If arr1(i, 2) = vbNullString Then

Ciao,
Holger
Hi Holger,

Brilliant !!! - I should have been able to catch that myself but, -
If arr1(i, 2) = "" Then worked perfectly !!
Thank you so much for taking the time to work with this. I appreciate your help !!
Have a great day,
Jeff
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,130
Members
453,021
Latest member
Justyna P

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