Excel vba单元格值取决于其他单元格

时间:2017-03-22 14:50:19

标签: excel excel-vba excel-formula vba

全部,

我试图找出如何制作一个表格来做到这一点。     细胞数量     单元b单元     Cell c单价     细胞d单元b。 所以     细胞a。细胞b。细胞c。细胞d。细胞e     100.克。 200公斤20    如果在单元格b中选择克,则单元格d为。然后,单元e必须是单元c,乘以1000与单元a相乘  我有Excel中的formule,但想把它放在vba中。对于删除等事故更安全。

这是我的代码,效果很好。 我怎么能把它放在vba编辑器中?

=(IF(B4="Gr",(A4*1),IF(B4="Ml",
   (A4*1),IF(B4="Paket",
 (A4*D4),IF(B4="Yk",
 (A4*12),IF(B4="Ck",
 (A4*8),IF(B4="Adet",(A4*1),
 IF(B4="Kg",(A4*1000),IF(B4="Lt",
 (A4*1),IF(B4>7,""))))))))*D4))


=IF(G10="Gr", ((F10*1)*E10),
IF(G10="Ml",((F10/16)*E10),
IF(G10="Paket",(F10*E10),
IF(G10="Yk",((F10/32)*E10),
IF(G10="Ck",((F10/128)*E10),
IF(G10="Adet",((F10*1)*E10),
IF(G10="Kg",((F10/1000)*E10),
IF(G10="Lt",((F10/1000)*E10),
IF(G10="","0")))))))))

1 个答案:

答案 0 :(得分:1)

VBA函数IIf是直接对应于工作表函数IF的函数。因此,如果你想要那些精确公式的文字VBA转录,它将看起来像这样:

Function FirstFormula() As Variant
    FirstFormula = (IIf(Range("B4").Value = "Gr", (Range("A4").Value * 1), IIf(Range("B4").Value = "Ml", _
        (Range("A4").Value * 1), IIf(Range("B4").Value = "Paket", _
        (Range("A4").Value * Range("D4").Value), IIf(Range("B4").Value = "Yk", _
        (Range("A4").Value * 12), IIf(Range("B4").Value = "Ck", _
        (Range("A4").Value * 8), IIf(Range("B4").Value = "Adet", (Range("A4").Value * 1), _
        IIf(Range("B4").Value = "Kg", (Range("A4").Value * 1000), IIf(Range("B4").Value = "Lt", _
        (Range("A4").Value * 1), IIf(Range("B4").Value > 7, "", False)))))))) * Range("D4").Value))
End Function    

Function SecondFormula() As Variant
    SecondFormula = IIf(Range("G10").Value = "Gr", ((Range("F10").Value * 1) * Range("E10").Value), _
        IIf(Range("G10").Value = "Ml", ((Range("F10").Value / 16) * Range("E10").Value), _
        IIf(Range("G10").Value = "Paket", (Range("F10").Value * Range("E10").Value), _
        IIf(Range("G10").Value = "Yk", ((Range("F10").Value / 32) * Range("E10").Value), _
        IIf(Range("G10").Value = "Ck", ((Range("F10").Value / 128) * Range("E10").Value), _
        IIf(Range("G10").Value = "Adet", ((Range("F10").Value * 1) * Range("E10").Value), _
        IIf(Range("G10").Value = "Kg", ((Range("F10").Value / 1000) * Range("E10").Value), _
        IIf(Range("G10").Value = "Lt", ((Range("F10").Value / 1000) * Range("E10").Value), _
        IIf(Range("G10").Value = "", "0", False)))))))))
End Function

=FirstFormula()置于单元格中应该在功能上等同于将精确的公式放在单元格中,对于第二个公式也是如此。请注意,如果你走这条路线,那么当你复制公式时,公式的引用不会自动更新,单元格引用“B4”等是硬连接到它。

我还没有测试过上面的内容,但它编译得很好,它完全遵循你的逻辑。如果它不符合您的需求,请考虑使用@VBAPete的优秀建议。

开启编辑以下是一个更灵活的版本:

Function FirstFormula(A As Range, B As Range, D As Range) As Variant
    FirstFormula = (IIf(B.Value = "Gr", (A.Value * 1), IIf(B.Value = "Ml", _
        (A.Value * 1), IIf(B.Value = "Paket", _
        (A.Value * D.Value), IIf(B.Value = "Yk", _
        (A.Value * 12), IIf(B.Value = "Ck", _
        (A.Value * 8), IIf(B.Value = "Adet", (A.Value * 1), _
        IIf(B.Value = "Kg", (A.Value * 1000), IIf(B.Value = "Lt", _
        (A.Value * 1), IIf(B.Value > 7, "", False)))))))) * D.Value))
End Function


Function SecondFormula(E As Range, F As Range, G As Range) As Variant
    SecondFormula = IIf(G.Value = "Gr", ((F.Value * 1) * E.Value), _
        IIf(G.Value = "Ml", ((F.Value / 16) * E.Value), _
        IIf(G.Value = "Paket", (F.Value * E.Value), _
        IIf(G.Value = "Yk", ((F.Value / 32) * E.Value), _
        IIf(G.Value = "Ck", ((F.Value / 128) * E.Value), _
        IIf(G.Value = "Adet", ((F.Value * 1) * E.Value), _
        IIf(G.Value = "Kg", ((F.Value / 1000) * E.Value), _
        IIf(G.Value = "Lt", ((F.Value / 1000) * E.Value), _
        IIf(G.Value = "", "0", False)))))))))
End Function

然后在工作表中,您可以使用

=FirstFormula(A4,B4,D4)

它将是等效的。此外,在将公式复制到某个范围内时,对A4B4D4的引用会按预期更新。

进一步编辑如果您想要使用VBA直接在单元格中插入公式,您可以使用以下内容:

Dim form As String
form = "=IF(G10=""Gr"", ((F10*1)*E10),"
form = form & "IF(G10=""Ml"",((F10/16)*E10),"
form = form & "IF(G10=""Paket"",(F10*E10),"
form = form & "IF(G10=""Yk"",((F10/32)*E10),"
form = form & "IF(G10=""Ck"",((F10/128)*E10),"
form = form & "IF(G10=""Adet"",((F10*1)*E10),"
form = form & "IF(G10=""Kg"",((F10/1000)*E10),"
form = form & "IF(G10=""Lt"",((F10/1000)*E10),"
form = form & "IF(G10="""",""0"")))))))))"

Range("H10").Formula = form

在上面,为了便于阅读,我分阶段建立了公式字符串。你不需要这样做,但结果会更不易读。

如果您希望引号在字符串中出现,则必须将它们加倍(使用""而不是"),以便VBA可以告诉整体字符串不应该在那里结束。