我不是新编,但我是Excel新手,因为这对我来说是一个新平台,我很难理解问题所在。
我正在尝试编写自己的函数,但每当我调试时,我都会得到" ding"声音,因为没有错误代码,只是一个" ding"声音我不知道我做错了什么。这是功能。
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
添加了第二个参数
答案 0 :(得分:1)
你有几个问题。
希望这有帮助!
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上运行。