當前位置:編程學習大全網 - 源碼下載 - Excel VBA下拉表單問

Excel VBA下拉表單問

樓主的代碼運行良好,在2003版中作了多次嘗試,兩個問題均未發現。因而無法再現。既然分享到了樓主的代碼,那我也分享點自己的看法。

雙擊結束輸入有點累贅且操作者不容易控制。建議改為LostFocus事件,並在SelectionChange中,Clear List之前,主動調用LostFocus

在選中C2時,建議在list中檢索所有已經在C2中的值,並在list中顯示。

草擬了壹個代碼,給樓主參考:

Private?Sub?Worksheet_SelectionChange(ByVal?Target?As?Range)

If?Target.Count?>?1?Then?Exit?Sub

If?Target.Row?=?2?And?Target.Column?=?3?Then

arr?=?Sheets("database").Range("C1:C29")

With?ListBox1

.MultiSelect?=?1

.ListStyle?=?1

.List?=?Sheets("database").Range("C1:C29").Value

.Top?=?Target.Top

.Left?=?Target.Left?+?Target.Width

.Height?=?Target.Height?*?28?'原來是36

.Width?=?90

ArrangeList?'增加的判斷

.Visible?=?True

End?With

Else

ListBox1_LostFocus?’調用輸出結果

ListBox1.Clear

ListBox1.Visible?=?False

End?If

End?Sub

Private?Sub?ArrangeList()

If?ListBox1.ListIndex?=?-1?Then?Exit?Sub

Dim?i&,?s$

With?ListBox1

For?i?=?0?To?.ListCount?-?1

If?.List(i)?<>?""?And?InStr(Range("C2").Value?&?"-",?.List(i)?&?"-")?>?0?Then?.Selected(i)?=?True

Next

End?With

End?Sub

Private?Sub?ListBox1_LostFocus()

If?ListBox1.ListIndex?=?-1?Then?Exit?Sub

Dim?i&,?s$

With?ListBox1

For?i?=?0?To?.ListCount?-?1

If?.Selected(i)?Then?s?=?s?&?"-"?&?.List(i)

Next

.TopLeftCell.Offset(,?-1).Value?=?Mid(s,?2)

.Visible?=?False

End?With

End?Sub

說明下:由於我的測試數據Database中使用的是數值,所以,間隔符“,”會被認作數據千分位格式符,故而在代碼中改為“-”;ListBox的高度也根據我這裏的顯示做了修改

以上淺見,僅為***同提高而探討。發了附件

  • 上一篇:android怎麽動態調試dex
  • 下一篇:農業諺語
  • copyright 2024編程學習大全網