返回列表 上一主題 發帖

[發問] 擷取LISTBOX當中的文字

[發問] 擷取LISTBOX當中的文字

假設我的LISTBOX的值為 A欄位(員工編號) 及 B欄位(員工姓名)所組成

當我選取LISTBOX其中一個值顯示為 " 80523 李大仁 "

後續要做其他動作時,是否有辦法只擷取出"80523"三個字呢?或是"李大人"。

(員工編號可能5位數~6位數) (姓名也可能2~4個字)

回復 1# lifedidi

Private Sub ListBox1_Change()
With ListBox1
MsgBox "第一欄" & .List(.ListIndex, 0)
MsgBox "第二欄" & .List(.ListIndex, 1)
End With
End Sub
學海無涯_不恥下問

TOP

Hsieh 大師 您好
感謝您的回覆,我嘗試了幾次,資料仍然無法分開,
應該是我編寫的方式有問題,我是用ListBox1.AddItem A欄 & B欄,是否是要用其他方式?
再向您請教,附上檔案,謝謝!

另外衍伸一個問題,LISTBOX是否可以打開USERFORM時,自動選擇第一欄位,不然LISTBOX沒有選擇項目時,操作下去會顯示型態不符合,謝謝!

LISTBOX.zip (20.53 KB)

TOP

回復 3# lifedidi

Q1:請將CommandButton1_Click事件程序改為如下試試看。

Private Sub CommandButton1_Click()
    With ListBox1
        MsgBox "編號:" & Split(.Value, " ")(0)
        MsgBox "姓名:" & Split(.Value, " ")(1)
    End With
End Sub

Q2:於UserForm_Initialize事件程序後面加入底下兩行。
ListBox1.ListIndex = 0
ListBox2.ListIndex = 0

TOP

Kubi 大師 您好
感謝您的幫忙~~~ 問題解決了 謝謝!

TOP

各位先進,不好意思,

我執行了程式碼

ListBox1.ListIndex = 0

Msgbox Listbox1.Value

他抓出來的值是"" 抓不到第一個項目

但是有時候又可以抓的到,請問是什麼原因呢?

TOP

各位先進您好:

附上測試檔案,下圖是要抓取AFJ 但是只抓取到AF,



LISTBOX_20180226.zip (20.23 KB)

TOP

回復 7# lifedidi
給駐點
  1. Private Sub CommandButton2_Click()

  2. ListBox1.ListIndex = 0
  3. ListBox2.ListIndex = 1
  4. ListBox3.ListIndex = 0
  5. ListBox4.ListIndex = 3
  6. ListBox5.ListIndex = 2
  7. ListBox5.SetFocus
  8. MsgBox ListBox1.Value & ListBox2.Value & ListBox3.Value & ListBox4.Value & ListBox5.Text

  9. End Sub
複製代碼
學海無涯_不恥下問

TOP

Hsieh 大師 您好

學習到了!感謝您的指導,謝謝!

TOP

        靜思自在 : 人事的艱難與琢磨,就是一種考驗。
返回列表 上一主題