Array handling in VBA

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,956
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
After populating an array with filenames, if any exist I want to Filter it.
But I can't filter an empty array, nor use LBound if the path is empty and the array not built.
Is there a solution for this, I've tried various things but can't get a catch all.
Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Could you show us what you have so far?
Basically you can check the variable using IsEmpty & IsArray function.
 
Upvote 0
What I have now is in the testing stage as I fine tune. I didn't think of IsArray so that may help.
VBA Code:
Public CurrFA As Variant

If FolderExists(mPath) Then
      CurrFA = FindFiles(mPath, Me!Pic & "*.*", False)
   Else
      ReDim CurrFA(0)
      CurrFA(0) = ""   'trying this
End If


Sub SelectFile(MyYear)
    Dim Afile As Variant, BFile As Variant
    Dim a1 As String, b1 As String
    Select Case MyYear
         Case Is < 1970
            Afile = Filter(CurrEntryFA, "jpg", True) 'Keep any jpgs
        Case Else
            Afile = Filter(CurrEntryFA, "jpg", False) 'Remove any jpgs
    End Select
    a1 = OneOrZeroBased(Afile)
    b1 = OneOrZeroBased(BFile)
End Sub

Function OneOrZeroBased(Afile) As String
    'return empty string or first item whether 1 or 0 based Afile
    If IsEmpty(Afile) = False Then
                If Afile(LBound(Afile)) > "" Then
                    OneOrZeroBased = Afile(0)
                    ElseIf UBound(Afile) > 0 Then
                    OneOrZeroBased = Afile(1)
                End If
        Else
            OneOrZeroBased = ""
    End If
End Function
 
Upvote 0
You need to check 2 things, first if the variable is empty and if it's not empty then check if it's an array.
For example, if you populate a variant with a single non-empty cell then it's not empty but it's not an array.
Here's an example:
VBA Code:
Sub check_array()
Dim ary
Range("A1") = "x"
ary = Range("A1")

If IsEmpty(ary) Then
    Debug.Print "ary is empty"
Else
    If IsArray(ary) Then
       Debug.Print "ary is an array"
    Else
       Debug.Print "ary is not an array"
       'if you want to make it an array (with only 1 value), you can use array function:
       'ary = Array(ary)
    End If
End If

End Sub
I don't understand your code, you're filtering CurrEntryFA instead of CurrFA?
 
Upvote 0
Thank you. I retyped to tidy. It is CurrFA.
Yes, being not empty and not array I thought was the problem but couldn't resolve it.
I will run your CheckArray and see what Array(ary) does. I do only want one (first) value or an empty string/
 
Upvote 0

Forum statistics

Threads
1,221,677
Messages
6,161,232
Members
451,692
Latest member
jmaskin

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