0

感谢你们。根据回复,再深入一点,我保持我的代码是这样的:

Public Function LowHigh(custo As Integer)
    If custo > 10.99 And custo <> 0 Then
        LowHigh = "1.4"
    Else
    If custo < 11 And custo <> 0 Then
        LowHigh = "1.35"
    Else
    If custo <= 0 Then
        LowHigh = "Valor Inválido"
    End If
    End If
    End If

End Function
Public Sub lorh()
ActiveCell.Formula = "=LowHigh(" & ActiveCell.Offset(0, 0).End(xlToLeft).Address & ")"

End Sub

示例图片

在选择有问题的正确值并按下按钮时,forfunção LowHigh 运行,问题是它需要逐个单元格完成,我需要选择左侧的整个范围并立即单击按钮单元格右侧间隔是填充公式如下图: 最终图像

我试过这样的事情:

Public Function LowHigh (cost As Integer)
     If cost> And cost 10.99 <> 0 Then
         LowHigh = "1.4"
     else
     If cost <11 And cost <> 0 Then
         LowHigh = "1:35"
     else
     If cost <= 0 Then
         LowHigh = "Invalid"
     end If
     end If
     end If
    
end Function
Public Sub lorh ()
Range ("ActiveCell", "ActiveCell"). FormulaR1C1 = "LowHigh = (" & Range (ActiveCell.Offset (0, 0). End (xlToLeft)). & Address ")"
   
end Sub

但它没有用,你能帮帮我吗?

4

1 回答 1

1

无意冒犯,但你真的需要阅读任何关于使用 if...else 的教程,因为你使用它们的方式都是错误的。您在其他 if 语句中插入 if 语句,而不是说else if,让我告诉你。

if something then
    blah
else
    if something else
        blah else
    end if
end if

这就是你在做什么,这就是你的意思

if something then
    blah
else if something else then
    blah else
else if something else again then
    blah else again
end if

差异应该通过适当的缩进(您的代码没有)来说明问题。

编辑:哦,我忘了最明显的:

If cost> And cost 10.99 <> 0 Then

这完全没有意义。你是说'如果成本大于 and (这是一个运算符)并且成本 10.99 <> 0 (这意味着什么)那么。如果 VBA 中的语句实际上是您用简单的英语所说的话:

If cost > 10.99 and cost <> 0 then

我想这就是你的意思,但是有那个and cost <> 0部分有点不必要。

于 2012-10-19T02:54:40.337 回答