Dict(SomeKeyValue) = SomeItemValueThis is not way to add data to Dictionary
The Dict(KeyColumn(i, 1)) = ValueColumn(i, 1)
Yes, you are right. Value can be added that way too. My bad. Here is what I convertedDict(SomeKeyValue) = SomeItemValue
.. is a perfectly valid way to add an entry to a Dictionary.
One thing that I noticed about the code is that "Key" and "Value" are used as variable arguments for the function and it is a very bad idea to use words as variables that vba already has special meanings for.
There could also be an issue with the type of double quote marks used.
Option Explicit
Sub testl()
'Dim columnA
'coluinnA = ReadColumn("a")
'ThisWorkbook. Worksheets (1). Range ("D:D") = coluinnA
Dim dictl
dictl = CreateDictForTwoColumns("a", "b")
End Sub
Function CreateDictForTwoColumns(Key As String, Value As String)
Dim KeyColumn, ValueColumn
Dim Dict As Object
Set Dict = CreateObject("scripting.dictionary")
Dim i
KeyColumn = ThisWorkbook.Worksheets(1).Range(Key + ":" + Key)
ValueColumn = ThisWorkbook.Worksheets(1).Range(Value + ":" + Value)
For i = 1 To UBound(KeyColumn, 1)
Dict(KeyColumn(i, 1)) = ValueColumn(i, 1)
Next
CreateDictForTwoColumns = Dict
End Function
Sub test1()
Dim dictl As Variant
dictl = CreateDictForTwoColumns("a", "b")
End Sub
Function CreateDictForTwoColumns(sKey As String, sValue As String)
Dim KeyColumn As Variant, ValueColumn As Variant
Dim Dict As Object
Dim i As Long
Set Dict = CreateObject("scripting.dictionary")
KeyColumn = ThisWorkbook.Worksheets(1).Range(sKey + ":" + sKey)
ValueColumn = ThisWorkbook.Worksheets(1).Range(sValue + ":" + sValue)
For i = 1 To UBound(KeyColumn, 1)
Dict(KeyColumn(i, 1)) = ValueColumn(i, 1)
Next
CreateDictForTwoColumns = Array(Dict.Keys, Dict.Items)
End Function
you estimate is right! That is excactly what i want ! thinks!!It is not entirely clear what the OP is wanting to achieve, but this is my estimate.
VBA Code:Sub test1() Dim dictl As Variant dictl = CreateDictForTwoColumns("a", "b") End Sub Function CreateDictForTwoColumns(sKey As String, sValue As String) Dim KeyColumn As Variant, ValueColumn As Variant Dim Dict As Object Dim i As Long Set Dict = CreateObject("scripting.dictionary") KeyColumn = ThisWorkbook.Worksheets(1).Range(sKey + ":" + sKey) ValueColumn = ThisWorkbook.Worksheets(1).Range(sValue + ":" + sValue) For i = 1 To UBound(KeyColumn, 1) Dict(KeyColumn(i, 1)) = ValueColumn(i, 1) Next CreateDictForTwoColumns = Array(Dict.Keys, Dict.Items) End Function
ok I will keep this in mind! really appreciate your instructions!Welcome to the MrExcel board!
When asking questions about a particular code please post the actual code, not a picture of it. My signature block below has more help on how to do that.
Also, if asking about an error, as well as giving the error message, tell us which line of code caused the error.