繁体   English   中英

VBA中的if_elseif语句

[英]if_elseif statement in VBA

我坚持做If .. Else声明数周。 我遇到诸如“类型不匹配”之类的问题,或者当我运行代码时,单元格中什么都没有出现。 请帮我解决这个问题。

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

将其添加到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函数,以查看范围内有多少个单元格符合条件。

  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对单元格范围进行操作。

希望有帮助!

对原始代码的这些修改可能会解决您的问题。
您的代码有一些问题,其他程序员在注释中提到了一些问题。
我建议您去研究每个代码,以了解为什么您的代码不起作用。

    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

您也可以(如其他人在评论中提到的那样)使用您自己的功能。
然后在想要答案的单元格中创建此函数: =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. “ ^他说的是,+您的所有第一个条件都在检查完全相等的事实。您可能想检查{单个数字}是否大于等于=,而不是检查{变量范围}是否等于=“-Bango
  3. 您永远不会将任何单元格分配给计算值。

暂无
暂无

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

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