简体   繁体   English

Excel VBA If Then函数取决于单元格值

[英]Excel VBA If Then Function conditional on cell values

I want to calculate a price based on a proposed discount % or calculate a discount based on a proposed price. 我想根据建议的折扣百分比计算价格,或根据建议的价格计算折扣。

I need users to be able to use either parameter and adjust back and forth to determine their ideal price. 我需要用户能够使用这两个参数并来回调整以确定其理想价格。

I have coded the formulas so that nothing is overwritten by the user but have a problem as the 'value if false' element of the "IF" formula is dependent on a zero value in one column and so resets if the user doesn't manually adjust this before inputting a figure into another. 我已经对公式进行了编码,以便用户不会覆盖任何内容,但是“ IF”公式的“如果为假的值”元素依赖于一列中的零值,因此会出现问题,如果用户不手动操作,则会重置在将图形输入到另一个图形之前进行调整。 This results in the new figure being reset. 这导致新的数字被重置。

Ideally I would like the sheet to reset the value in column "Proposed Promo POR%" to zero if a value is input in "Promo Price per Unit". 理想情况下,如果在“每单位促销价格”中输入值,则我希望工作表将“ Propro Promo POR%”列中的值重置为零。 Is this possible? 这可能吗?

This is the code I have at the moment. 这是我目前的代码。

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Range("b21:b26")) Is Nothing Then Exit Sub

Application.EnableEvents = False
ActiveSheet.Range("$i21:$i26").Formula ="=IF(Proposal!$b21>Formulae!$a$5,Proposal!$g21-(Proposal!$g21*Proposal!$b21),  (Proposal!$g21-Proposal!$i21)/Proposal!$g21)"
Application.EnableEvents = True

End Sub

For clarity Formulae!$a$5 is zero and it is column $i21:$i26 that the user will enter a price in if they want to. 为了清楚起见,Formula!$ a $ 5为零,如果需要,用户将在其中输入价格栏$ i21:$ i26。 If they do enter something here then column b should reset to zero. 如果他们确实在此处输入内容,则b列应重置为零。

This is a screenshot of the columns and headers in my sheet 这是工作表中列和标题的屏幕截图

在此处输入图片说明

The Worksheet_Change event will help you. Worksheet_Change事件将为您提供帮助。 Worksheet_SelectionChange fires each time a user changes the active cell , whereas Worksheet_Change fires anytime a user makes an edit to any cell(s) . Worksheet_SelectionChange每个用户改变活动小区时触发,而Worksheet_Change火灾随时用户进行编辑的任何细胞(一个或多个)。

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("$i21:$i26"")) Is Nothing _ 
    And Target.Rows.Count = 1 and Target.Columns.Count = 1 Then
    'assumes event will fire only on changing one cell at a time

    Application.EnableEvents = False
    Target.Offset(0,-7).Value = 0
    Application.EnableEvents = True

End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM