Excel函数不能正常工作

我对编码并不陌生,但对于Excel来说我是新手,因为这对我来说是一个新的平台,所以我很难理解问题所在。

我试图写我自己的function,但每当我debugging我得到“叮”的声音,因为没有错误代码,只是一个“叮”的声音,我不知道我在做什么错了。 这是function。

Function userItems(Range) Set userObj = CreateObject("Scripting.Dictionary") For Each Cell In Range If Not userObj.Exists(Cell.Value) Then userObj.Add Cell.Value, Cell.Address End If Next Cell userItems = userObj(1) End Function 

增加了第二个参数

你有几个问题。

  1. 你必须声明该函数的variables名称
  2. 你忘了声明你的字典对象
  3. 不知道你想要对字典结果做什么,但这是一个有趣的把戏,所有的键/项目输出到列

希望这可以帮助!

 Sub test() Call testFunction(Range("A1:A100")) End Sub 

和代码:

 'You need to declare the variable name and type Sub testFunction(myRange As Range) 'First need to declare the object Dim dict As Object Set dict = CreateObject("Scripting.Dictionary") 'Good practice to declare variables, otherwise they are Variants Dim cell As Range For Each cell In myRange If Not dict.Exists(cell.Value) Then dict.Add cell.Value, cell.Address End If Next 'Do something with the result, transpose is good fun Range("B1").Resize(dict.Count, 1).Value = WorksheetFunction.Transpose(dict.keys) Range("C1").Resize(dict.Count, 1).Value = WorksheetFunction.Transpose(dict.items) End Sub 

我是字典对象的狂热粉丝,但请注意,这些代码不能在Office for Mac上运行。