在VBA中的if_elseif语句

我坚持做If .. Else陈述了几个星期。 我面临的问题,如“types不匹配”,或者当我运行的代码没有出现在我的单元格。 请帮我解决这个问题。

 Sub ifstatement() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("SamplePO") Dim rg, rg1 As Range Set rg = ActiveSheet.Range("L17:L90") Set rg1 = ActiveSheet.Range("I17:I90") Dim pound As Double Dim nettwt As Double Dim grosswt As Double If rg < 130 Then grosswt = rg1 + 20 ElseIf rg = 131 And rg <= 200 Then grosswt = (rg1 * 0.15) + 15 ElseIf rg = 201 And rg <= 500 Then grosswt = rg1 * 0.11 ElseIf rg = 501 And rg <= 999 Then grosswt = rg1 * 0.7 ElseIf rg = 1000 And rg <= 2000 Then grosswt = rg1 * 0.5 ElseIf rg = 2001 And rg <= 4999 Then grosswt = rg1 * 0.5 ElseIf rg = 5000 And rg <= 8000 Then grosswt = rg1 * 0.5 ElseIf rg = 8001 And rg <= 10000 Then grosswt = rg1 * 0.5 End If End Sub 

对原始代码的这些修改可能会解决您的问题。
你的代码有几个问题,其中一些是在评论中的同行编码人员提到的。
我build议你去研究每一个,以了解你的代码不工作的原因。

  Sub ifstatement() Dim rg As Range Dim cell As Range Set rg = ActiveSheet.Range("L17:L90") Dim grosswt As Double For Each cell In rg.Cells grosswt = 0 If cell <= 130 Then grosswt = cell.Offset(0, -3).Value + 20 ElseIf cell >= 131 And cell <= 200 Then grosswt = (cell.Offset(0, -3).Value * 0.15) + 15 ElseIf cell >= 201 And cell <= 500 Then grosswt = cell.Offset(0, -3).Value * 0.11 ElseIf cell >= 501 And cell <= 999 Then grosswt = cell.Offset(0, -3).Value * 0.7 ElseIf cell >= 1000 And cell <= 2000 Then grosswt = cell.Offset(0, -3).Value * 0.5 ElseIf cell >= 2001 And cell <= 4999 Then grosswt = cell.Offset(0, -3).Value * 0.5 ElseIf cell >= 5000 And cell <= 8000 Then grosswt = cell.Offset(0, -3).Value * 0.5 ElseIf cell >= 8001 And cell <= 10000 Then grosswt = cell.Offset(0, -3).Value * 0.5 End If cell.Offset(0, 1).Value = grosswt ' Will output the answer in column "M" from "M17:M90" Next cell End Sub 

你也可以(如评论中的其他人所说)利用你自己的function。
创build这个函数,然后在你想要答案的单元格中input: =myfunc(I17,L17)然后拖动它直到你到达=myfunc(I90,L90)

  Function myfunc(check As Range, use As Range) As Double Dim grosswt As Double If check.Value <= 130 Then grosswt = use.Value + 20 ElseIf check.Value >= 131 And check.Value <= 200 Then grosswt = (use.Value * 0.15) + 15 ElseIf check.Value >= 201 And check.Value <= 500 Then grosswt = use.Value * 0.11 ElseIf check.Value >= 501 And check.Value <= 999 Then grosswt = use.Value * 0.7 ElseIf check.Value >= 1000 And check.Value <= 2000 Then grosswt = use.Value * 0.5 ElseIf check.Value >= 2001 And check.Value <= 4999 Then grosswt = use.Value * 0.5 ElseIf check.Value >= 5000 And check.Value <= 8000 Then grosswt = use.Value * 0.5 ElseIf check.Value >= 8001 And check.Value <= 10000 Then grosswt = use.Value * 0.5 End If myfunc = grosswt End Function 
  1. “你不能把多个单元格的范围看作是单个单元格,你可能需要某种循环。 – 时间威廉斯
  2. “他说了什么,所有你的第一个条件是检查完全相等的事实,你可能想检查{单个数字}是否>,而不是检查{一系列variables}是否=” – Bango
  3. 您从不将任何单元格分配给计算的值。

在UDF下面添加这个UDF,并将其用在类似的内置公式中。
例如在第7行的毛重单元中, =GetGrossWeight(I7,L7) 。 填写公式。 假设切断磅数为130,200,500,1000。

 Function GetGrossWeight(LBS As Range, NetWeight As Range) As Double Dim GrossWeight As Double Select Case CDbl(LBS.Value) Case Is <= 130: GrossWeight = NetWeight.Value + 20 Case Is <= 200: GrossWeight = NetWeight.Value * 0.15 + 15 Case Is <= 500: GrossWeight = NetWeight.Value * 0.11 Case Is <= 1000: GrossWeight = NetWeight.Value * 0.7 Case Else: GrossWeight = NetWeight.Value * 0.5 End Select GetGrossWeight = GrossWeight End Function 

尝试使用下面的countif函数来查看该范围内有多less个单元格符合条件。

  If application.worksheetfunction.countif(rg, "<130") >= 0 Then 

条件满足后,您需要使用循环来执行范围内的操作。 试试这样的for loop

 for each cell in rng cell.value = cell.value + 20 'you can put whatever operation you want to execute on the range here next cell 

然后可以按照相同的格式进入下一个elseif ,使用countif来确定条件是否满足, for loop在单元格范围内进行操作。

希望有所帮助!