0

here, the validation_check function returns an error of invalid property assignment (code 450). when I don't use the set keyword, it returns the error from validate_check = finalCollection, and when I do use the set keyword, it highlights cellArray = validation_check(cellArray, C_VAL_LIST_KW) ie from where I call the function. Here is the code

The sub where i call the function

Sub validate_list()

Call declare_vars

validationRange.Select

Dim rng As Range
Dim colStartCell As Range
Dim colEndCell As Range
Dim cNum As Integer
Dim cellArray
Dim n As Integer
Dim msgboxString As String

n = 0

For Each cell In Application.Selection

    If 0 < InStr(UCase(cell), UCase(C_VAL_LIST_KW)) Then
        cNum = cell.Column

        Set colStartCell = Cells(firstDataRangeRow, cNum)

        'MsThgBox colStartCell.Address

        Set colEndCell = Cells(dataLastRowNum, cNum)
        'MsgBox colEndCell.Address

        Set rng = Range(colStartCell, colEndCell)
        cellArray = split_array(cell)
        cellArray = validation_check(cellArray, C_VAL_LIST_KW)

        ...
        ...
        End Sub

The Function of split array returns an array, so the problem doesn't lie there

validate check function:

Option Explicit

Public Function validation_check(arr, metaTag) As Collection

Dim newCollection As New Collection
Dim finalCollection As New Collection
Dim arraySize As Integer
Dim i As Integer
Dim val_index As Integer

arraySize = UBound(arr) - LBound(arr) + 1

For i = 0 To arraySize - 1
    If arr(i) = metaTag Then

        val_index = i
        Exit For
    End If
Next

For i = val_index To arraySize - 1
    newCollection.Add (arr(i))
Next

For i = 1 To newCollection.Count
    If InStr(1, newCollection(i), "#") <> 1 Then
        finalCollection.Add (newCollection(i))
    End If
Next

Set validation_check = finalCollection

End Function

Split array function

Public Function split_array(cell)

Dim arr1, arraySize
arr1 = Split(cell.Value, ",")
arraySize = UBound(arr1) - LBound(arr1) + 1

For i = 0 To arraySize - 1
    arr1(i) = Trim(arr1(i))
Next i

split_array = arr1

End Function
  • What does `split_array` do? What data type does it return? Might be worth including the `split_array` code in your question too. – Samuel Everson Apr 22 '20 at 09:35
  • Also you might want to read up on [How to avoid using Select...](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). You could change your `For Each...Next` to `For Each Cell in validationRange` - Assuming `validationRange` is a defined range. – Samuel Everson Apr 22 '20 at 09:39
  • @SamuelEverson I have added the split array code. It returns an array. And sure, I am gonna try those modifications – Deepansh Goel Apr 22 '20 at 10:08
  • What values are being entered into the `Array`? From what I can see I'm assuming they are `String` values from cells in a worksheet? – Samuel Everson Apr 22 '20 at 10:10
  • Yeah, they are split and trimmed strings(comma separator used), from value of a cell – Deepansh Goel Apr 22 '20 at 10:15
  • Just taking a stab at it, perhaps try declaring `newCollection ` and `finalCollection` `as Variant`. Collections ususally store things like `Objects` for example a collection of all worksheets in a workbook (the `Sheets()` code is a Collection that does just that) whereas if you declare as `Variant` it will accept most data types - Declaring as variant will still be fine with creating an array too. – Samuel Everson Apr 22 '20 at 10:19
  • I should mention, some of your code in the functions may need adjusting if you do this. – Samuel Everson Apr 22 '20 at 10:20
  • 1
    I found the solution, I just had to use the Set keyword according to my code, but your solutions are much better and cleaner. Thank you! – Deepansh Goel Apr 22 '20 at 11:00

0 Answers0