不能在流程生成下拉列表()中訪問d。
為了解決這個問題
1,可以將d定義為模塊級變量:即把Dim d作為新字典放在模塊的頂部。代碼如下:
Dim d As New DictionarySub的代碼表保存為數組()Dim data As New執行dim SQL as string dim arr,y SQL = "從[定額計件工資$]中選擇定額名稱、單價、計價單位、申報單位、換算系數" arr = data。y = 1到bound (arr,2) d (arr (0,y)) = arr (1,y)的篩選結果(SQL)&;“-”& amp;arr(2,y)和amp“-”& amp;arr(3,y)和amp“-”& amp;Arr(4,y) Next yEnd Sub Sub生成下拉()Dim sr作為字符串調用代碼表,並將其保存為數組sr = Join(d.Keys,“,”)with range(“M6:M25”)。validation . delete . addtype:= xlvalidatelist,formula 1:= Sr end with end sub 2。將碼表保存為數組()並定義為函數,並返回壹個dictionary對象,將d的定義放在生成下拉()的過程中。代碼如下:
功能碼表保存為數組()作為字典,dim數據作為新的,執行dim SQL作為字符串,dim arr,y dim d作為新的字典SQL = "從[定額計件工資$]中選擇定額名稱、單價、計價單位、申報單位、換算系數" arr = data。y = 1到bound (arr,2) d (arr (0,y)) = arr (1,y)的篩選結果(SQL)&;“-”& amp;arr(2,y)和amp“-”& amp;arr(3,y)和amp“-”& amp;arr(4,Y) Next y set碼表另存為array =dEnd Sub Sub生成下拉()Dim sr作為String Dim d作為Dictionary '把d的定義放在生成下拉()set d =碼表另存為array () sr = Join(d.Keys,",")With Range("m6:m25 ")。驗證。刪除。Add Type:=xlValidateList,Formula 1:= srendwithendsub 3,將過程碼表保存為數組()定義為接收dictionary對象的過程,將d的定義放入生成下拉()的過程中,將帶參數的碼表保存為生成下拉()的過程中的數組()過程。代碼如下:
子碼表保存為數組(Byref d為字典),DIMMDATA為new,DIMMSQL為string DIMMARR,YSQL = "Select quota name,unit price,pricing unit,reporting unit,Conversion coefficient from[quota piece $]" arr = data。篩選結果(SQL)如果d什麽都不是,則設置d =新字典'如果d沒有實例化,則實例化它。For y = 1 To UBound(arr,2) d(arr(0,y)) = arr(1,y)& amp;“-”& amp;arr(2,y)和amp“-”& amp;arr(3,y)和amp“-”& amp;Arr(4,y) Next yEnd Sub Sub生成下拉()dim Sr as string dim d as new dictionary調用代碼表保存為array (d) sr = Join(d.Keys,",")With Range("m6:m25 ")。驗證。刪除。添加類型:=xlValidateList,formula 1:= Sr End with End Sub
4.將兩個過程合並成壹個過程。還有其他答案。具體代碼這裏就不給出了。