返回列表 上一主題 發帖

[發問] 複製資料轉寫到另一工作表

回復 15# c_c_lai
不好意思, 現在無法上傳圖片, 只能先用文字敘述
我想只要用2個ListBox 完成選項, 太多物件會佔空間,我的做法是:
單號的[List Box2] 可一次勾選單號TW-1301001及TW1301002
明細的[List Box1]就會出現共4筆序號明細TW-1301001 001, TW-1301001 002, TW-1301001 003, TW-1301002 001
只是希望想用別的寫法簡化我的多層迴圈判斷.
learner

TOP

回復 14# yliu
你要的是 圈起來的部分,附上的是全部的 (ComboBox、CheckBox、ListBox1、ListBox2)應用。
請參考裡面相關的程式碼:

TOP

回復 13# c_c_lai

不好意思,  我要的是2個ListBox都能複選, 也就是單號的ListBox複選完後(跨單號,不是單一), 其明細都會出現在明細的ListBox, 然後再去勾選要的序號明細.
我的寫法太多層,希望能簡化. 不知是否提供較好的寫法.檔案是昨天貼上的那個檔案. 謝謝~
learner

TOP

回復 12# yliu
套用你原本的檔案,並加以稍稍修改,看看是否符合你的需求。
請觀察 ThisWorkbook 與 Sheet1 (login) 間之互動。
P.S.  另額外增加了 Checkbox 的應用,供參考。
ListBox 複製資料轉寫到另一工作表.rar (41.65 KB)

TOP

有兩個ListBox, 一個是單號(ListBox2), 一個是單號明細(ListBox1),都可以複選, 請詳附件 ListBox問題(2).zip (23.22 KB)
想請問版上高手, ListBox2的程式是否有更好的寫法?
我目前寫法是:
Private Sub ListBox2_Change()
Dim lrow, irow, ai As Integer
Sheets("login").ListBox1.Clear
[A9:E19] = ""
With ListBox2

For lrow = 0 To .ListCount - 1

  If .Selected(lrow) Then
   With Sheets(Sh)
     ai = 2
     Do While .Cells(ai, "A") <> ""
If .Cells(ai, "A") = ListBox2.List(lrow, 0) Then
      With ListBox1
        .AddItem
        irow = .ListCount
       .List(irow - 1, 0) = Sheets(Sh).Cells(ai, "A")
       .List(irow - 1, 1) = Sheets(Sh).Cells(ai, "B")
       .List(irow - 1, 2) = Sheets(Sh).Cells(ai, "C")
       .List(irow - 1, 3) = Sheets(Sh).Cells(ai, "D")
       .List(irow - 1, 4) = Sheets(Sh).Cells(ai, "E")
        End With
     End If
    ai = ai + 1
    Loop
   End With
End If
Next
End With
Application.EnableEvents = True
End Sub
learner

TOP

回復 10# GBKEE


    謝謝GBKEE!
learner

TOP

回復 9# yliu
  1. Private Sub CommandButton1_Click()
  2.     Dim g As Integer, E As Range, C As Range, 單號 As String, SS As String, Rng As Range
  3.     Dim i As Integer
  4.     With Sheets("login")
  5.         單號 = .ComboBox1.Value
  6.         Set Rng = .[B9:B19]
  7.         SS = Application.Phonetic(Rng)                                                  '結合所有序號
  8.     End With
  9.    
  10.     With Sheets("final").[A:A]
  11.         If Application.CountIf(.Cells, 單號) > 1 Then
  12.             .Replace 單號, "=xxx", xlWhole                                               ''Replace 方法
  13.             With .SpecialCells(xlCellTypeFormulas, xlErrors)
  14.                 .Cells = 單號
  15.                 For Each C In .Cells                                                     ''比對到 序號 踢除 此序號
  16.                     If InStr(SS, C.Offset(, 1)) Then SS = Replace(SS, C.Offset(, 1), "") 'Replace 函數
  17.                     If SS = "" Then Exit Sub
  18.                 Next
  19.             End With
  20.         End If
  21.         For Each E In Rng
  22.             If E = "" Then Exit For
  23.             If InStr(SS, E) Then                                              '比對到 序號
  24.                 g = Application.CountA(.Cells) + 1                            '讀取A欗有資料數的儲存格數 +1
  25.                 i = Application.CountA(Rng)
  26.                 .Cells(g, "A").Resize(1) = 單號
  27.                 .Cells(g, "B").Resize(1, 2) = E.Cells(1).Resize(1, 2).Value
  28.                 .Cells(g, "D").Resize(1, 6) = E.Cells(1, 4).Resize(1, 6).Value
  29.             End If
  30.         Next
  31.     End With
  32. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 7# GBKEE

GBKEE版大,
延伸另一個問題, 使用ListBox ,請問如何讓已選取且已COPY到工作表"final"的單號&序號不會再次被選取COPY?

ListBox問題(2).zip (26.2 KB)
learner

TOP

回復 7# GBKEE

真的很謝謝GBKEE, 以及上次幫忙的Hsieh.
想藉由Excel VBA來解決工作上一些繁瑣的資料處理,但我只學過基本的VBA, 功力實在不足,
感激版主們無私的解答,讓我解決工作上問題.
learner

TOP

回復 6# yliu
  1. Private Sub CommandButton1_Click()
  2.     Dim g As Integer, E As Range
  3.     g = Application.CountA(Sheets("final").[A:A]) + 1               '讀取A欗有資料數的儲存格數 +1
  4.     For Each E In Sheets("login").[B9:B19]
  5.         If E = "" Then Exit For
  6.         With Sheets("final")
  7.             .Cells(g, "A") = Sheets("login").ComboBox1.Value
  8.             .Cells(g, "B").Resize(1, 2) = E.Resize(1, 2).Value
  9.             .Cells(g, "D").Resize(1, 6) = E.Cells(1, 4).Resize(1, 6).Value
  10.         End With
  11.         g = g + 1
  12.     Next
  13. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 知識要用心體會,才能變成自己的智慧。
返回列表 上一主題