Hi,
I have the following procedure:
Where UDF CreateDictFromColumns is defined as follow:
when I run store_Dict() sub I got an error message "Application-defined or object-defined error" that point to Set rng = Worksheets(sheet).Range(Columns(keyCol), Columns(valCol)). I surmised it is the function Columns() that is creating the problem.
Wonder if someone could point how to me how I could do with that line of error to make the UDF work?
I also tried but it doesn't work:
I have the following procedure:
Code:
Sub store_Dict()
Dim key As Variant
Dim myDict As New Dictionary
Set myDict = CreateDictFromColumns("Sheet1", 1, 4)
For Each key In myDict.Keys
Debug.Print "Key = ", key, " Change = ", myDict(key)
Next
End Sub
Where UDF CreateDictFromColumns is defined as follow:
Code:
Function CreateDictFromColumns(sheet As String, keyCol As String, valCol As String) As Dictionary
Set CreateDictFromColumns = New Dictionary
Dim rng As Range: Set rng = Worksheets(sheet).Range(Columns(keyCol), Columns(valCol))
Dim i As Long
Dim lastCol As Long
lastCol = rng.Columns.Count
For i = 2 To rng.Rows.Count
If (rng(i, 1).Value = "") Then Exit Function
CreateDictFromColumns.Add rng(i, 1).Value, rng(i, lastCol).Value
Next
End Function
when I run store_Dict() sub I got an error message "Application-defined or object-defined error" that point to Set rng = Worksheets(sheet).Range(Columns(keyCol), Columns(valCol)). I surmised it is the function Columns() that is creating the problem.
Wonder if someone could point how to me how I could do with that line of error to make the UDF work?
I also tried but it doesn't work:
Code:
Dim rng As Range: Set rng = Worksheets(sheet).Range(Cells(1, keyCol).EntireColumn, Cells(1, valCol).EntireColumn)