【问题标题】:vba - how to copy values from drop down row and compare it with other sheetvba - 如何从下拉行中复制值并将其与其他工作表进行比较
【发布时间】:2020-09-16 06:25:03
【问题描述】:

我有以下情况

Sheet -Type 包含 (A1) 中的类别部分和从 D1:D2 获取值的单元格 B1(下拉列表)中的相应值

工作表 - 变量包含以下数据

Sheet - Price 包含单元格 A2:A3(dropdown) 中的类型部分,该部分从 E1:E2 获取值

场景 :在价格表中,用户选择类型值下拉值,当用户在 价格 表中输入单位数量值时,逻辑需要检查用户选择了什么类别从 Type 工作表并从变量中检查相应的组合价格并乘以没有单位并显示在 Price 工作表-价格列

Dim shT as Worksheet, ShP as Worksheet,ShV as Worksheet
Dim strCategory  as String, strType as Long,rowCategory as Long,rowType as Long,Price as Double

set shT = Worksheets("Type")
set shP = Worksheets("Price")
set shV = Worksheets("Variables")

strCategory = ShT.Range("B1").Value
strType = shP.Range("A2").Value


With WorksheetFunction
rowCategory = .Match(strCategory , shV.Range("A1:A5"),0)
rowType = .Match(strType, shV.Range("B" & rowCategory & ":B" & rowCategory +5),0)
rowType = rowType + rowCategory - 1


Price = shV.Range("C" & rowType).Value

End With

For i = 2 To 3
shP.Range("C" & i).value = Price * shP.Range("B" & i)
Next i

问题:上述逻辑仅适用于价格表中的 A2 值下拉菜单,但不适用于 A3 值。

有什么方法可以让我们获取整个下拉值数据并与另一张表中的表格数据进行比较,问题在下面的逻辑中退出

    strType = shP.Range("A2").Value

提前致谢

【问题讨论】:

  • Discount 是什么,应该从哪里收集?基本上你应该在'Price'表的元素之间迭代并根据迭代元素重新定义strType。
  • @FaneDuru - 为混淆道歉,没有折扣之类的东西,更新了问题,它是价格
  • 我开始相信这一点,但我不能确定...请测试我的答案代码,我在其中引入了正确的 Price,但将 Discount 保持为 1。您可能还需要这样的参数。或者不...无论如何,它不会影响结果。
  • 题外话:不要让当前的自己使用幻数、固定范围、硬编码字符串和合并单元格,对未来的自己有所帮助。你会感谢自己这样做。
  • @FaneDuru - 感谢您的帮助,它按预期工作

标签: excel vba


【解决方案1】:

请尝试下一个代码:

Sub testBringPrice()
Dim shT As Worksheet, ShP As Worksheet, ShV As Worksheet
Dim strCategory  As String, strType As Long, rowCategory As Long
Dim rowType As Long, Price As Double, i As Long, Discount As Double

Discount = 1 'you should define where from it should be taken
Set shT = Worksheets("Type")
Set ShP = Worksheets("Price")
Set ShV = Worksheets("Variables")

strCategory = shT.Range("B1").value

For i = 2 To 3
    strType = ShP.Range("A" & i).value
    
    With WorksheetFunction
        rowCategory = .Match(strCategory, ShV.Range("A1:A5"), 0)
        rowType = .Match(strType, ShV.Range("B" & rowCategory & ":B" & rowCategory + 5), 0)
        rowType = rowType + rowCategory - 1
                
        Price = ShV.Range("C" & rowType).value
        ShP.Range("C" & i).value = Price * Discount * ShP.Range("B" & i)
    End With
Next i
End Sub

以上代码考虑Discount = 1(无折扣)。您必须定义从哪里获取折扣值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多