[英]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".基本上,我有一个组合框(procNamecombobox),它从工作表“DailyNumbers”上的“A”列填充。 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.
我只希望下面的 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
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:下面如何,它将在 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.