Columns function - object-defined error

30percent

Board Regular
Joined
May 5, 2011
Messages
123
Office Version
  1. 2016
Platform
  1. Windows
Hi,

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)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
definitely just shooting ideas out here but ummm could possibly be in your UDF the last 2 parameters might need to be Integers, not strings, or putting quotes around the numbers when calling the UDF?
 
Upvote 0
Try
Code:
Function CreateDictFromColumns(sheet As String, keyCol As [COLOR=#ff0000]Long[/COLOR], valCol As [COLOR=#ff0000]Long[/COLOR]) As Dictionary
    Set CreateDictFromColumns = New Dictionary
    Dim rng As Range
    Dim i As Long
    Dim lastCol As Long
    [COLOR=#ff0000]With Worksheets(sheet)
       Set rng = .Range(.Columns(keyCol), .Columns(valCol))
    End With[/COLOR]
    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
 
Upvote 0
Shouldn't that be:
Code:
 If (rng[COLOR="#FF0000"].Cells[/COLOR](i, 1).Value = "") Then Exit Function
        CreateDictFromColumns.Add rng[COLOR="#FF0000"].Cells[/COLOR](i, 1).Value, rng[COLOR="#FF0000"].Cells[/COLOR](i, lastCol).Value
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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