【问题标题】:Data updated in the Excel sheet, but name was not retrieved from the dropdownExcel 工作表中的数据已更新,但未从下拉列表中检索到名称
【发布时间】:2020-01-21 08:40:08
【问题描述】:

我是这个 Excel VBA 的新手和初学者。 我创建了一个 VBA 程序来注册和更新患者的信息或数据以进行健康检查。使用同一张表(寄存器)来存储注册和更新的数据,我能够根据下拉选择(cmbName)检索以前注册/更新的数据,我从这个站点引用了更新功能https://yodalearning.com/tutorials/update-delete-using-excel-vba-userform-vba-userform/。 但是,在更新时,其余数据会更新并反映在相应的单元格中,但不会检索名称,并且名称 (C2) 的单元格为空白。

这是我的代码(用于更新):

Private Sub BT_Update_Click()
Dim emptyRow As Long

If Me.cmbName.Value = "" Then
  MsgBox "Select name!", vbExclamation, "Name"
  Exit Sub
End If

Sheets("register").Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
Cells(emptyRow, 1).Value = TB_RegNum.Value
Cells(emptyRow, 2).Value = TB_Date.Value

'Here where I failed to retrieve the name
'Cells(emptyRow, 3).Value = Me.cmbName.Value 'Name populated but new value not updated
'Cells(emptyRow, 3).Value = cmbName.Value    'Name populated but new value not updated
Cells(emptyRow, 4).Value = TB_NRIC.Value
Cells(emptyRow, 5).Value = TB_Address.Value
Cells(emptyRow, 6).Value = TB_Phone.Value
Cells(emptyRow, 7).Value = TB_Gender
Cells(emptyRow, 8).Value = TB_Race.Value
Cells(emptyRow, 9).Value = TB_Height.Value

下面是下拉列表(cmbName):

Private Sub cmbName_Change()

If Me.cmbName.Value = "" Then
MsgBox "Select Name!", vbExclamation, "Name"
Exit Sub
End If

On Error Resume Next

Me.TB_Date.Value = WorksheetFunction.Index(Range("B2:B1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_RegNum.Value = WorksheetFunction.Index(Range("A2:A1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_NRIC.Value = WorksheetFunction.Index(Range("D2:D1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_Address.Value = WorksheetFunction.Index(Range("E2:E1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_Phone.Value = WorksheetFunction.Index(Range("F2:F1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_Gender.Value = WorksheetFunction.Index(Range("G2:G1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_Race.Value = WorksheetFunction.Index(Range("H2:H1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))
Me.TB_Height.Value = WorksheetFunction.Index(Range("I2:I1000"), WorksheetFunction.Match(cmbName.Value, Range("C2:C1000"), 0))

【问题讨论】:

  • 没有明显的理由说明您的重新整理的代码(任何一行)不应该工作。 cmbName.Value "" 代码顶部的测试证明了这一点。因此,如果 cmbName.Value 没有到达“Register”,原因必须在其他地方。错误可能出在您的测试中,而不是您的代码中。 cmbName中如何生成下拉列表?当“C2 为空白”时,emptyRow 的值是多少?仔细查看测试的逻辑、数据的来源和传输方式。
  • 您正在激活“注册”,然后在其余代码中引用 ActiveSheet(默认)。那不好。我从来不这样做,所以我不知道。可能是您的代码写入了另一张纸,或者根本没有,而您在第 2 行中看到的内容一直都在那里?重复测试 emptyRow 为 >2
  • 下拉列表是使用索引匹配函数使用 Private Sub cmbName_Change() 生成的。以前的要求是为注册和更新创建单独的工作表,后来改为只使用一张工作表,这就是为什么注册和更新都在同一个工作表中具有不同的用户表单(注册和更新)。
  • 请将Sub cmbName_Change的代码添加到您的问题中。只有在您做出选择之后才应该加载下拉菜单,这听起来很奇怪。这可能是问题的根源。人们会期望在初始化表单时设置下拉菜单,之后不会更改。
  • 我已更新代码并尝试其他方法。我将这一行“Cells(emptyRow, 3).Value = cmbName.Value”放在“Cells(emptyRow, 9).Value = TB_Height.Value”下面,现在它可以工作了。但还是没搞懂这背后的逻辑,我猜这和你之前@Variatus所说的有关。

标签: excel vba


【解决方案1】:
Private Sub BT_Update_Click()
Dim emptyRow As Long

If Me.cmbName.Value = "" Then
  MsgBox "Select name!", vbExclamation, "Name"
  Exit Sub
End If

Sheets("register").Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
Cells(emptyRow, 1).Value = TB_RegNum.Value
Cells(emptyRow, 2).Value = TB_Date.Value
Cells(emptyRow, 4).Value = TB_NRIC.Value
Cells(emptyRow, 5).Value = TB_Address.Value
Cells(emptyRow, 6).Value = TB_Phone.Value
Cells(emptyRow, 7).Value = TB_Gender
Cells(emptyRow, 8).Value = TB_Race.Value
Cells(emptyRow, 9).Value = TB_Height.Value
'Bring this line to the bottom and it works!
Cells(emptyRow, 3).Value = cmbName.Value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多