简体   繁体   中英

VBA Userform to save textbox.Value to excel worksheet when name is selected in combobox

I apologize in advance for posting the same question, but I don't know how else to add additional code example. If there is a way to add additional code to a previous question, please advise. Basically, I am trying to save some textbox values into my worksheet so they can be reinitiated when userform is closed and re-opened. This is what I have thus far..but clearly its wrong!

Basicaly, I have a combobox(procNamecombobox) that populates from column "A" on worksheet "DailyNumbers". I just want the below textboxe.Values to save in the corresponding columns (B,C,D & E) next to each name, when its selected in the combobox.

  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

How about the following, it will search Column A for the Combobox value, if found it will update that row, if not found, it will populate the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM