【问题标题】:User Form - VLOOKUP and Form Calculation用户表单 - VLOOKUP 和表单计算
【发布时间】:2019-05-10 17:23:35
【问题描述】:

我有一个包含多个字段的表单。我在表单上有一个字段,我想在其中进行计算。这需要一个辅助列函数的VLOOKUP。

在常规单元格中,我可以输入以下内容:

=VLOOKUP(B3&C3&D3,Ins_Price_Tbl,5,0)

Ins_Price_Tbl 是一个数据表。

并得到我正在寻找的答案。

我的表单有文本框

txtAdjustmentCountYearA - An integer
txtInsurance - A String
txtAdjustmentYearA - A year like 2018
txtAdjustmentCostYearA - The result field

我需要txtAdjustmentCostYearA 来执行VLOOKUP,在这种情况下,C3 将等于“调整”或我输入的任何文本并将其乘以txtAdjustmentCountYearA.Value

所以我可能会有类似的东西:

txtAdjustmentCountYearA.Value 等于 2

txtAdjustmentYearA.Value 等于 2018

txtAdjustmentCostYearA 等于:

=VLOOKUP(Me.txtInsurance.Value&"Whatever I type in here"&Me.txtAdjustmentYearA,Ins_Price_Tbl,5,0) * txtAdjustmentCountYearA.Value

【问题讨论】:

  • 通过Application.VLookup 为您的帖子添加了解决方案,并考虑到文本框始终是字符串。

标签: excel vba vlookup userform


【解决方案1】:

尽可能靠近您的 OP

Option Explicit                             ' declaration head of code module

Private Sub CommandButton1_Click()
Dim ws As Worksheet                         ' provide for range reference
Set ws = ThisWorkbook.Worksheets("MySheet") ' <~~ change to your sheet name
Dim searched As String, found As Variant, factor As Double
searched = Me.txtInsurance.Text & ws.Range("C3").value & Me.txtAdjustmentYearA.Text
' use the Application function VLookup instead of worksheetfunction.VLookup
' allowing to ask a IsError condition (variable found has to be of type Variant!)
  found = Application.VLookup(searched, ws.Range("Ins_Price_Tbl"), 5, 0)
' Textbox values are always strings and have to be converted (e.g. to Double)
  If IsNumeric(txtAdjustmentCountYearA.Text) Then factor = CDbl(txtAdjustmentCountYearA.Text)
' Check possible errors before assigning result to textbox (again as String!)
  If Not IsNumeric(found) Or IsError(found) Then
     Me.txtAdjustmentCostYearA.Text = "Nothing found"
  Else
     Me.txtAdjustmentCostYearA.Text = found * factor
  End If
End Sub

【讨论】:

  • 不胜感激。 - 您是否发现我的回答有帮助,以便您可以将其标记为已接受?
猜你喜欢
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2012-08-07
  • 2018-03-21
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多