Forum Discussion

hrh_dash's avatar
hrh_dash
Iron Contributor
Jun 26, 2022
Solved

Error showing Expected Array for VBA to name file name in column

I would like to name Range("A1") as May22 which can be extracted from the filename.

 

However when the code was executed, error message was triggered: Expected Array.

Sub copyfilename ()

Dim ws As Worksheet
Set ws = Sheet1

Dim fileName As String
Dim columnName As String

fileName = "V:\Finance\Systems-Risk-ERM\OrderToCash\C2C\Corp Credit Control\3. Monthly Combined Aging\2022\May22\Top Segments & Top All_May22.xlsm"

columnName = Split(fileName, "\")

ws.Range("A1").Value = columnName(8)

End Sub

 

Is there a way around this? Appreciate the help!

 

 

  • hrh_dash 

    Change the declaration of columnName to

     

    Dim columnName() As String

     

    The () make columnName an array.

Resources