简体   繁体   English

当在组合框中选择名称时,VBA 用户表单将文本框.值保存到 Excel 工作表

[英]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.

相关问题 VBA Excel - 将用户表单文本框值添加到工作表 - VBA Excel - add userform textbox value to worksheet Excel用户窗体TextBox.Value填充MS Word书签 - Excel Userform TextBox.Value Populate MS Word Bookmarks 将TextBox.Value转换为Double到VBA(Excel 2013) - Convert TextBox.Value to Double into VBA (Excel 2013) 在 Excel VBA 中设置默认的 TextBox.Value TypeName - Setting the Default TextBox.Value TypeName in Excel VBA EXCEL VBA USERFORM-如果选择了某些组合框,则将值设为负 - EXCEL VBA USERFORM - Make value negative if certain combobox selected VBA:如果工作簿中的工作表名称等于从用户窗体中选择的组合框值,则复制该工作表并将其粘贴到另一个工作簿中 - VBA: If Worksheet name in Workbook equals Combo Box value selected from Userform then copy that Worksheet and paste it into another Workbook 用户窗体组合框列表项+将用户窗体值另存为VBA Excel中的变体 - Userform combobox list items + save userform values as variants in VBA Excel 将UserForm textbox.value作为日期格式传输到工作表 - Transfer UserForm textbox.value as date format to sheet 从excel vba userform组合框中选定行的列中提取数据 - Extract data from column of selected row in excel vba userform combobox VBA 用户表单根据 2 个组合框选择使用单元格值填充文本框 - VBA Userform Populate textbox with cell value based on 2 combobox selections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM