【问题标题】:VBA - Filling a cell value after a combobox text is entered/changedVBA - 输入/更改组合框文本后填充单元格值
【发布时间】:2018-04-12 19:59:20
【问题描述】:

我在工作表上的工作簿上有以下代码,旨在使用 ws change 测试交集,然后转到组合框并检索框中输入的任何值。但是,发生的情况是,在组合框中第一次输入值后,单元格未使用其值进行更新。我必须再次单击它,然后它将填充。我知道我可能必须使用另一个事件过程,但我对组合框事件一无所知。有人能指出我正确的方向吗?

谢谢迈克。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim aRng As Range
Dim tRng As Range

Set aRng = Range("C19:C36")
Set tRng = Sheet2.Range("I2")

Application.EnableEvents = False  'to prevent re-iteration of event

On Error GoTo cleanup:
If Not Intersect(aRng, Target) Is Nothing Then
Call Sheet2.ComboBox1_Change
Target.Value = Sheet2.ComboBox1.Value
End If

cleanup:            'enable events once again
Application.EnableEvents = True

End Sub

在方框所在的第 2 页上。

Public Sub ComboBox1_Change()
With ComboBox1
.Activate
.SelText = Empty
.DropDown
.MatchRequired = True
End With
End Sub

【问题讨论】:

  • 这是关于 Excel 的吗?用产品标记您的问题!
  • 完成。微软 Excel 2010

标签: vba events combobox excel-2010


【解决方案1】:

要了解您问题的基础知识,Combobox 有一个 LinkedCell 属性。如果您在Sheet1 上输入单元格地址,ComboBox 中的选定项目将显示在Sheet1 上。 (如果在 Sheet1 的那个单元格中输入了一个值,它也会显示在 ComboBox 中。)

ComboBox LinkedCell

您可以使用此代码(在 Sheet2 的模块中)在 ComboBox 中进行选择后强制显示 Sheet1:

Private Sub ComboBox1_Change()
Sheet1.Select
End Sub

使用 Worksheet_Change 仅在单元格中输入了某些内容(并按下 RETURN)后才会生效。

【讨论】:

  • 那么您是否建议我重新构建代码流程?
  • 确实,从设置我给出的示例开始,然后添加额外内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多