简体   繁体   中英

How to input a range object to a function and return an array in VBA?

I have a procedure which generates cell range depending on certain criteria. I also have a function which takes a cell range object as input and returns an array. However, if I call the function within a procedure, I get this error. "Compile error: Can't assign to array" Is there something wrong with my syntax? Thanks.

Option Explicit

'Reads a database table from current Active worksheet and paste to output sheet

Function DBReader(DBMarker As Range) As String
    Dim arr(5) As String
    'Read data
    arr(0) = ActiveWorksheet.DBMarker.Value              
    arr(1) = ActiveWorksheet.DBMarker.Offset(1, 1).Value 
    arr(2) = ActiveWorksheet.DBMarker.Offset(2, 1).Value 
    arr(3) = ActiveWorksheet.DBMarker.Offset(0, 2).Value 
    arr(4) = ActiveWorksheet.DBMarker.Offset(1, 2).Value 
    arr(5) = ActiveWorksheet.DBMarker.Offset(2, 2).Value 
    DBReader = arr()
End Function

The procedure is kinda long, so I'll only show relevant lines

Public Sub LogSum()
    'Declare variables
    ...
    Dim DBInfo(5) As String         'Stores info from function

    ...
    Set CellDB = bookLOG.Worksheets(j).UsedRange.Find("Ref:", LookIn:=xlValues)
    ...
    DBInfo = DBReader(CellDB)       'Returns Array from function???
    ...
End Sub

Give this a try...

Function DBReader(DBMarker As Range) As Variant
    Dim arr(5) As String
    'Read data
    arr(0) = DBMarker.Value
    arr(1) = DBMarker.Offset(1, 1).Value
    arr(2) = DBMarker.Offset(2, 1).Value
    arr(3) = DBMarker.Offset(0, 2).Value
    arr(4) = DBMarker.Offset(1, 2).Value
    arr(5) = DBMarker.Offset(2, 2).Value
    DBReader = arr()
End Function

Public Sub LogSum()
    'Declare variables

    Dim CellDB As Range
    Dim DBInfo() As String         'Stores info from function

    Set CellDB = bookLOG.Worksheets(j).UsedRange.Find("Ref:", LookIn:=xlValues)
    DBInfo = DBReader(CellDB)       'Returns Array from function???
End Sub

I suspect the problem is that you are trying to set your function equal to the array you just populated in the final line of your DBReader function.

DBReader = arr() may be failing since DBReader is a function, not an array.

So, there's a couple of issues you need to look at.

First, let's start with your subroutine.

You are declaring an array with 6 elements before you assign it. Let's leave that blank by changing Dim DBInfo(5) to Dim DBInfo()

You should also add a check in your subroutine to make sure you don't have an empty object for CellDB . After that line add something like:

If CellDB Is Nothing Then...

And create a rule for what happens when it's Nothing .

Public Sub LogSum()
    'Declare variables
    ...
    Dim DBInfo() As String

    ...
    Set CellDB = bookLOG.Worksheets(j).UsedRange.Find("Ref:", LookIn:=xlValues)
    ...
    DBInfo = DBReader(CellDB)
    ...
End Sub

Next, we can look at your function itself. At the end of your declaration As String , you are wanting to pass it off as an array so you could designate as an array by using As String() .

Also, the syntax of your line DBReader = arr() is done incorrectly. You should just keep it as DBReader = arr .

Function DBReader(DBMarker As Range) As String()
    Dim arr(5) As String
    'Read data
    arr(0) = ActiveWorksheet.DBMarker.Value
    arr(1) = ActiveWorksheet.DBMarker.Offset(1, 1).Value
    arr(2) = ActiveWorksheet.DBMarker.Offset(2, 1).Value
    arr(3) = ActiveWorksheet.DBMarker.Offset(0, 2).Value
    arr(4) = ActiveWorksheet.DBMarker.Offset(1, 2).Value
    arr(5) = ActiveWorksheet.DBMarker.Offset(2, 2).Value
    DBReader = arr
End Function

Also, in general, it's not advised that you use ActiveWorksheet by instead declaring your worksheets. You can pass this off to your function by adding another argument like:
Function DBReader(DBMarker As Range, ws As WorkSheet)...

Will your function still work? Probably.
Do you want to have to debug it later on when you start using it for other worksheets? Doubtful.
Save yourself a headache now so you don't have to have unnecessary debugging later.

Lastly, I am not entirely sure of your intention with DBInfo , but that is a one-dimensional array. This means that if you were to paste this array to your worksheet that it would come out horizontal unless you were to transpose it. You can make it easier on yourself now by making it a 2-D array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM