簡體   English   中英

在 excel 表上添加數據

[英]Adding data on excel sheet

我有一個這樣的數據列表:

在此處輸入圖像描述

我想使用用戶表單將測試從 N 列添加到 X bu。 在用戶表單中,我有一個 combobox 填充如下: 在此處輸入圖像描述 例如,如果第一次添加測試 D,則應將其添加到第 3 列,如果添加第二次測試 D,則應將其添加到第 4 列...如果我第一次添加測試 A,則應將其添加到第 1 列,第二個測試A應該被添加到第2列......(如第一張圖片)每次自動添加人員和服務的名稱。 我正在嘗試設置一個條件,以便能夠得到我想要的我已經寫了這段代碼:

' code for the button on my worksheet
Private Sub CommandButton1_Click()
'-------------Populate the comobox of persons and tests
Dim ws_Liste_Pers As Worksheet
Set ws_Liste_Pers = ActiveWorkbook.Worksheets("service ")
Fin_Liste_Pers = ws_Liste_Pers.Range("A65530").End(xlUp).Row

For i = 2 To Fin_Liste_Pers
    UserForm_SDE.ComboBox_Demandeur.AddItem ws_Liste_Pers.Range("A" & i)
  
Next i

Dim ws_tech_essais As Worksheet
Set ws_tech_essais = ActiveWorkbook.Worksheets(" tech essais")
Fin_Liste_tech_essais = ws_tech_essais.Range("A65530").End(xlUp).Row
For i = 2 To Fin_Liste_tech_essais
    UserForm_SDE.ComboBox_Tech_Essai.AddItem ws_tech_essais.Range("A" & i)
Next
  

UserForm_SDE.Show
End Sub



'Code for the userfom to add the data 
    Private Sub CommandButton1_Click()
    TPers = Feuil2.[A2].Resize(Feuil2.[A1000000].End(xlUp).Row - 1, 2).Value
    ReDim TPlaces(0 To ComboBox_Tech_Essai.ListCount - 1)
    
    Dim LP As Long, LS As Long, CS As Long
       LP = ComboBox_Demandeur.ListIndex + 1
    '   If LP = 0 Then Exit Sub
    '   If Not ComboBox_Tech_Essai.MatchFound Then Exit Sub
       CS = TPlaces(ComboBox_Tech_Essai.ListIndex) + 1: If CS < 14 Then CS = 14
       TPlaces(ComboBox_Tech_Essai.ListIndex) = CS
       On Error Resume Next
       LS = WorksheetFunction.Match(TPers(LP, 3), Feuil2.[A:A], 0)
       If Err Then LS = 0
       On Error GoTo 0
       If LS > 0 Then If Not IsEmpty(Feuil2.Cells(LS, CS).Value) Then LS = 0
       If LS = 0 Then
          LS = Feuil1.[A1000000].End(xlUp).Row + 1
         
          Feuil1.Cells(LS, 1) = TPers(LP, 1)
    '      Feuil1.Cells(LS, 2) = TPers(LP, 2)
          End If
       Feuil1.Cells(LS, CS) = ComboBox_Tech_Essai.Value
    
    Unload Me
    End Sub

問題是這段代碼只在 N 列上添加了測試。誰能幫我找到 teh pb。 謝謝

你有這行代碼:

CS = TPlaces(ComboBox_Tech_Essai.ListIndex) + 1: If CS < 14 Then CS = 14

這是設置您在子末尾附近使用的列索引:

Feuil1.Cells(LS, CS) = ComboBox_Tech_Essai.Value

14 = N 所以使用語句If CS < 14 Then CS = 14代碼將永遠不會填充 N 之前的列。

@FaneDuru 做簡單的看這張圖: 在此處輸入圖像描述 我想從 combobox 中選擇測試編號,然后通過從這樣的組合框中選擇它來添加測試: 在此處輸入圖像描述

添加新測試時,代碼應在 H 列上查找測試 N°,並從 combobox 中查找所選測試的名稱,如果測試存在於 N 列中,則應添加到 M 中,如果我們 select 相同的測試代碼必須將其添加到列 O... 列在同一列中我不能有相同的測試名稱,請查看綠色的測試 A 的第一張圖片。 (我從 combobox 中選擇了 001,因此在 N 列中添加了 tets A,第二個測試 AN°001 在 M 列中添加)對於黃色的測試 B,您會看到第一個值在 P 列中,因為我選擇了 tets N°OO1,對於第二次測試,BI 從 combobox 中選擇了測試編號 002,因此將其添加到 N 列

請使用下一個代碼。 為了正常工作,它需要與測試編號(來自工作表)匹配的字符串完全格式化,就像我的意思是組合框中一樣,比如“001”、“002”......我沒有觀察到你是如何加載的組合,但有必要對 H:H 列中的范圍執行相同的操作。 最佳文本格式是通過選擇討論中的列,然后: Data tab -> Text to Columns... -> Next -> Next ,然后選中 'Column data format' 中的 'Text' 並按 'Finish':

Private Sub CommandButton1_Click()
   Dim sh As Worksheet, rngTNo As Range, rngCol As Range, iRow As Long, i As Long
   Dim ComboBox_No As MSForms.ComboBox, ComboBox_Test As MSForms.ComboBox
   
   'use in the next row your real combo boxes. I named mine ComboBox_No, respectively, ComboBox_Test
   'You will use something like: Me.ComboBox_Tech_Essai, Me.ComboBox_Demandeur...
   Set ComboBox_No = frmTest.ComboBox_No: Set ComboBox_Test = frmTest.ComboBox_Test
   
   Set sh = ActiveSheet 'Feuil2
   Set rngTNo = sh.Range("H7:H" & sh.Range("H" & Rows.count).End(xlUp).Row) 'Test numbers range
   If rngTNo.cells.count < 1 Then MsgBox _
            "There necessary Test numbers range is missing...": Exit Sub
   If rngTNo.NumberFormat <> "@" Then MsgBox _
            "The Test numbers range must be formatted as text!": Exit Sub
    
   iRow = rngTNo.Find(ComboBox_No.Value).Row 'row to be used for dropping the test
   For i = 14 To 25
       Set rngCol = sh.Range(sh.cells(7, i), sh.cells(sh.cells(Rows.count, i).End(xlUp).Row, i))
       If rngCol.Find(ComboBox_Test.Value) Is Nothing Then
            If sh.cells(iRow, i).Value = "" Then
                sh.cells(iRow, i).Value = ComboBox_Test.Value: Exit For
            End If
       End If
   Next
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM