2

是否可以应用 IF .. 然后在 VBA 中的数组上?我有以下代码,我在数组 plaga 上应用 For Each 和 If - 计算每个 Interval 的出现次数,但是 VBA 返回“下标超出范围。所以,我想也许我必须以不同的方式制定这个.. .

Option Explicit
Public plaga As Variant

Sub dort()
  Dim cMin As Double
  Dim cMax As Double
  Dim lDer As Long

  Dim plaga() As Variant
  plaga = Worksheets("Sheet3").Range("A1:A10").Value

  Call tri1(plaga)

  cMin = WorksheetFunction.Min(plaga)
  cMax = WorksheetFunction.Max(plaga)

  Dim longInter As Double
  longInter = (cMax - cMin) / 3

  Dim pla As Variant
  Dim lCom As Long

  lDer = 2

  For Each pla In plaga
    If pla < cMin + longInter Then
      lCom = 1
    Else
      For lCom = lDer To 3
        If pla < cMin + longInter * lCom Then
          lDer = lCom
          Exit For
        End If
      Next
    End If
    plaga(lCom) = plaga(lCom) + 1
  Next

  Dim Destination As Range
  Set Destination = Worksheets("Sheet3").Range("B1")
  Destination.Resize(3, 1).Value = plaga

End Sub
4

1 回答 1

1

我不确定您的子程序 tri1(plaga) 中发生了什么,但我认为这是您的代码的正确演绎。

Sub dort()
'
Dim cMin As Double
Dim cMax As Double
Dim lDer As Long
Dim plaga() As Variant
'
  ReDim Preserve plaga(10)
  plaga = Worksheets("Sheet3").Range("A1:A10").Value
  '
  Call tri1(plaga)
  '
  cMin = WorksheetFunction.Min(plaga)
  cMax = WorksheetFunction.Max(plaga)
  '
Dim longInter As Double
  longInter = (cMax - cMin) / 3
  '
Dim pla As Variant
Dim lCom As Long
  '
  lDer = 2
  '
  For Each pla In plaga
    If pla < cMin + longInter Then
      lCom = 1
    Else
      For lCom = lDer To 3
        If pla < (cMin + longInter) * lCom Then
          lDer = lCom
          Exit For
        End If
      Next
    End If
    plaga(lCom, 1) = plaga(lCom + 1, 1)
  Next
  '
Dim Destination As Range
  Set Destination = Worksheets("Sheet3").Range("B1:B1")
  Destination.Resize(3, 1).Value = plaga
'
End Sub

更新

注意:我添加了 plaga2() 来显示 longinter 的整数

Sub dort()

    Dim cMin As Double
    Dim cMax As Double
    Dim lDer As Long
    Dim plaga() As Variant
    Dim plaga2() As Variant


    ReDim Preserve plaga(10)
    ReDim Preserve plaga2(10)
    plaga = Worksheets("Sheet3").Range("A1:A10").Value
    plaga2 = Worksheets("Sheet3").Range("A1:A10").Value

    'Call tri1(plaga)

    cMin = WorksheetFunction.Min(plaga)
    cMax = WorksheetFunction.Max(plaga)

    Dim longInter As Double
    'In this case useing the numbers you stated
    'longinter = (7 - 1)/3 = 2
    longInter = (cMax - cMin) / 3

    Dim pla As Variant
    Dim lCom As Integer
    For lCom = 1 To 10
        If plaga(lCom, 1) >= (CMin + longInter) Then
            plaga(lCom, 1) = (plaga(lCom, 1) / longInter)
            plaga2(lCom, 1) = Int(plaga(lCom, 1))
        Else
            plaga(lCom, 1) = 1
            plaga2(lCom, 1) = 1
        End If
    Next

  Dim Destination1 As Range
  Set Destination1 = Worksheets("Sheet3").Range("B1:B1")
  Destination1.Resize(10, 1).Value = plaga

  Dim Destination2 As Range
  Set Destination2 = Worksheets("Sheet3").Range("C1:C1")
  Destination2.Resize(10, 1).Value = plaga2

End Sub
于 2013-04-14T09:50:53.773 回答