以下是引用gzxw在2007-1-21 15:05:20的发言:Function dx(r1) dx = Application.WorksheetFunction.VLookup(r1,arr("","";"吖","A";"八","B";"嚓","C";"咑","D";"鵽","E";"发","F";"猤","G";"铪","H";"夻","J";"咔","K";"垃","L";"嘸","M";"旀","N";"噢","O";"妑","P";"七","Q";"囕","R";"仨","S";"他","T";"屲","W";"夕","X";"丫","Y";"帀","Z"),2) End Function
可是在编写的时候,老是提醒: "编译错误,缺少列表分隔符" 我主要的意思是请教老师,在Excel的VBA中怎样设置常量数量,能否以此来详细说明一下. 二维数组的使用不对,上面的代码我修改了一下,虽然麻烦些,但是只能这样使用 Sub dx() Dim r1 As String, i%, iMax%, x Dim arr() Dim arr1, arr2 r1 = "名" arr1 = Array("", "吖", "八", "嚓", "咑", "鵽", "发", "猤", "铪", "夻", "咔", "垃", "嘸", "旀", "噢", "妑", "七", "囕", "仨", "他", "屲", "夕", "丫", "帀") arr2 = Array("", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z") iMax = UBound(arr1) ReDim arr(0 To iMax, 0 To 1) '重新定义一个二维数组 For i = 0 To iMax arr(i, 0) = arr1(i) arr(i, 1) = arr2(i) Next x = Application.WorksheetFunction.VLookup(r1, arr, 2) '这里的vlookup只能接受上面那种类型的二维数组 Debug.Print r1, x End Sub 其实也可以直接用公式,然后取消公式转变成值,不过这样就不能用自定义函数了 增加一个Match的示例,我想是一个效果 Sub dx() Dim r1 As String, i%, iMax%, x Dim arr1, arr2 r1 = "名" arr1 = Array("", "吖", "八", "嚓", "咑", "鵽", "发", "猤", "铪", "夻", "咔", "垃", "嘸", "旀", "噢", "妑", "七", "囕", "仨", "他", "屲", "夕", "丫", "帀") arr2 = Array("", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z") x = arr2(Application.WorksheetFunction.Match(r1, arr1, 1) - 1) '用match函数的近似查找,好像也可以哦,请测试 Debug.Print r1, x End Sub
[此贴子已经被作者于2007-1-24 10:28:34编辑过] |