Error in VBA(wrong number of arguments or invalid property assignment)

JiangSH

New Member
Joined
Dec 13, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
hi everyone!
I was doing some exercise about Function in VBA when an error occurred(wrong number of arguments or invalid property assignment).Here is the picture.
Really needs your help !! thx!!
 

Attachments

  • Screenshot 2021-12-14 121349.png
    Screenshot 2021-12-14 121349.png
    15 KB · Views: 185

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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.
 
  • Like
Reactions: Zot
Upvote 0
A bit troublesome since code is short ? . I'm just guessing your problem here.
This is not way to add data to Dictionary
The Dict(KeyColumn(i, 1)) = ValueColumn(i, 1)

should be
Dict.Add KeyColumn(i, 1), ValueColumn(i, 1)

However, your column has no range limit. When KeyColumn(i, 1) becomes rows of zero or blank, then you will have error.
 
Upvote 0
This is not way to add data to Dictionary
The Dict(KeyColumn(i, 1)) = ValueColumn(i, 1)
Dict(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.
 
Upvote 0
Dict(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.
Yes, you are right. Value can be added that way too. My bad. Here is what I converted
VBA Code:
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
 
Upvote 0
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
 
Upvote 0
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
you estimate is right! That is excactly what i want ! thinks!!
 
Upvote 0
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.
ok I will keep this in mind! really appreciate your instructions!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,335
Members
452,636
Latest member
laura12345

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