簡體   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