Array Value to String?

E_DPSG

Board Regular
Joined
Jul 17, 2008
Messages
82
:confused:

Okay - This is bothering me. I cant get the value out of my array to convert to a string for a FileExists check.

What am I missing??
Code:
Public Function CheckRatesFile(ByRef FPath As String, ByRef MyArray As Variant) As Boolean
    Dim FullPath As String
    Dim MyFlag As Boolean
    MyFlag = True
    For Each Item In MyArray
        FullPath = FPath + CStr(MyArray(Item))
        If Not FileExists(FullPath) Then
            MyFlag = False
        End If
    Next
        CheckRatesFile = Flag
End Function

On the CStr(MyArray(Item)) it gives me a type mismatch error.

Any Suggestions?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Without knowing what the MyArray parameter you are passing to the function looks like I can only make a guess. Try using:
Code:
For i = LBound(MyArray) to UBound(MyArray)
in place of For Each Item ...
Also, CheckRatesFile = MyFlag
 
Upvote 0
Loop through without using For Each
Code:
For I  = LBound(MyArray) To UBound(MyArray)
   ' other code
Next I
Or just use Item not MyArray(Item).
Code:
    FullPath = FPath + CStr(Item)
 
Upvote 0
Instead of + use & to concatenate strings.

Code:
FullPath = FPath & MyArray(Item)

If your backslash is not present in FPath or in the array elements:

Code:
FullPath = FPath & "\" & MyArray(Item)

Your MyFlag can come out of the If:

MyFlag = FileExists(FullPath)
 
Last edited:
Upvote 0
Thanks guys. I have been switching back and forth between vba, .Net, Java - forgetting proper syntax for each. I used the cstr(Item) and it works fine.

The MyArray is a list of filenames compiled from a user form select statement. This was a check to see if all files existed and were ready for an import call - as I am not controlling the creation of the Src Files.

Thanks again all!

:)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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