首页 文章

在组合框中选择名称时,VBA Userform将textbox.Value保存到excel工作表

提问于
浏览
0

我提前为发布相同的问题道歉,但我不知道如何添加其他代码示例 . 如果有办法在上一个问题中添加其他代码,请提供建议 . 基本上,我试图将一些文本框值保存到我的工作表中,以便在关闭和重新打开userform时重新启动它们 . 这就是我到目前为止......但显然是错误的!

基本上,我有一个组合框(procNamecombobox),从工作表"DailyNumbers"上的"A"列填充 . 我只想让下面的textboxe.Values保存在每个名称旁边的相应列(B,C,D&E)中,当它在组合框中选中时 .

Private Sub procNamecombobox_Change()    

  Dim ws As Worksheet
  Dim EmptyRow As Long

  Set ws = Sheets("DailyNumbers")
  EmptyRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
  ' *** Check combobox selection ***
  If procNamecombobox.ListIndex > -1 Then

  ws.Range("B" & EmptyRow).Value = completeCount.Text
  ws.Range("C" & EmptyRow).Value = handledCount.Text
  ws.Range("D" & EmptyRow).Value = wipCount.Text
  ws.Range("E" & EmptyRow).Value = suspendCount.Text
  ws.Range("B2:B" & EmptyRow).Sort key1:=ws.Range("A1:A" & EmptyRow),  order1:=xlAscending, Header:=xlNo
  Else
 MsgBox "Please select your name"
 End If

  End Sub

1 回答

  • 0

    如下所示,它将在列A中搜索Combobox值,如果发现它将更新该行,如果没有找到,它将填充EmptyRow:

    Private Sub procNamecombobox_Change()
    Dim ws As Worksheet: Set ws = Sheets("DailyNumbers")
    Dim EmptyRow As Long
    Dim FoundVal As Range
    EmptyRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
    ' *** Check combobox selection ***
        If procNamecombobox.ListIndex > -1 Then
            Set FoundVal = ws.Range("A1:A" & EmptyRow).Find(procNamecombobox.Value) 'find Combobox value in Column A
            If Not FoundVal Is Nothing Then 'if found
                ws.Range("B" & FoundVal.Row).Value = completeCount.Text 'use that row to populate cells
                ws.Range("C" & FoundVal.Row).Value = handledCount.Text
                ws.Range("D" & FoundVal.Row).Value = wipCount.Text
                ws.Range("E" & FoundVal.Row).Value = suspendCount.Text
            Else 'if not found use EmptyRow to populate Cells
                ws.Range("A" & EmptyRow).Value = procNamecombobox.Value
                ws.Range("B" & EmptyRow).Value = completeCount.Text
                ws.Range("C" & EmptyRow).Value = handledCount.Text
                ws.Range("D" & EmptyRow).Value = wipCount.Text
                ws.Range("E" & EmptyRow).Value = suspendCount.Text
            End If
        Else
            MsgBox "Please select your name"
        End If
    End Sub
    

相关问题