【问题标题】:VBA Array of integers from integer to integer without typing out every single oneVBA从整数到整数的整数数组,无需输入每一个整数
【发布时间】:2022-01-18 07:44:12
【问题描述】:

我正在尝试找出正确的代码

FormatStrokeArray = Array (120 to 300 step 1)

因为输入数百个整数会很疯狂。

稍后我尝试使用

查找我的值是否在该数组中
If IsInArray(FormatStroke, FormatStrokeArray) = True Then
MsgBox ("WORKS")
end if

功能是

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = UBound(Filter(arr, stringToBeFound)) > -1
End Function

【问题讨论】:

  • 为什么要比较一个字符串(stringToBeFound)和一个用整数填充的数组?
  • 因为我不知道如何为我想要实现的目标编写代码,所以我发布了一个关于它的帖子
  • 不要使用数组,使用 ArrayList。您将需要对 mscorlib 的引用或通过“Sytem.Collections.Generic.ArrayList”使用后期绑定创建。您使用 .Add 方法填充 ArrayList。您可以使用 .contains 方法检查值是否存在。请注意,ArrayList 不提供任何智能感知,并且您可能需要对某些重载方法进行反复试验。如果你想更勇敢一点,你可以试试我的 Lyst 对象,它没有这些限制,但 IS 正在进行中。您需要给我发消息以获取下载链接。
  • 其他快捷方式。在标注数组时,Step 不是 To 的合法语法。 Array 语句(即 = Array)接受一个值列表并返回一个 Variant,其中包含您提供的项目数组。要获取字符串数组,您将使用 Dim FormatStrokeArray(80)。 VBA 确实允许 120 到 300,但你为什么需要这个而不是 80 个项目的数组。顺便说一句,ArrayList 还有一个 ToArray 方法,如果您需要将数据推回 Excel 中,该方法很有用。
  • 我确实意识到,使用它只是以某种代码形式解释逻辑的手段

标签: arrays vba integer


【解决方案1】:

请尝试理解下一个代码:

Sub testEvaluate()
   Dim FormatStrokeArray
   FormatStrokeArray = Application.Transpose(Evaluate("row(120:300)")) 'create an array as you need (based 1 array)
   Debug.Print Join(FormatStrokeArray, "|") 'see the array in Immediate Window
   
   'play here with integers:
   Debug.Print IsInArray(FormatStrokeArray, 300) 'it returns true
   Debug.Print IsInArray(FormatStrokeArray, 100) 'it returns false
   
   Debug.Print PositionInArray(FormatStrokeArray, 150) 'it returns 31 (the 31th element)
   Debug.Print PositionInArray(FormatStrokeArray, 100) 'it returns -1 (no match)
End Sub
Function IsInArray(arr As Variant, myVal As Integer) As Boolean
   Dim mtch
   mtch = Application.Match(myVal, arr, True)
   If Not IsError(mtch) Then
     If mtch = UBound(arr) Then
        If arr(UBound(arr)) = myVal Then IsInArray = True: Exit Function
     Else
        IsInArray = True: Exit Function
     End If
   End If
   IsInArray = False
End Function
Function PositionInArray(arr As Variant, myVal As Integer) As Variant
   Dim mtch: mtch = Application.Match(myVal, arr, True)
   
   If Not IsError(mtch) Then
     If mtch = UBound(arr) Then
        If arr(UBound(arr)) = myVal Then PositionInArray = mtch: Exit Function
     Else
        PositionInArray = mtch: Exit Function
     End If
   End If
   PositionInArray = -1
End Function

如果有不清楚的地方,请不要犹豫,要求澄清。

【讨论】:

  • @Eduards PositionInArray 这是一个奖励......只是看看它是如何完成的。有时,您会/可能需要它。它不应该......你是如何调用这个函数的?像Debug.Print IsInArray(FormatStrokeArray, ActiveCell.Value) 这样的东西会返回一个数组吗?当然,值必须是数字。它不应该是一个看起来像数字的字符串...
  • 这正是我想要的,谢谢你,朋友!不需要知道任何项目在数组中的位置,但感谢您的添加
  • @Eduards 不是真的...它返回数组中搜索到的元素的位置。它也适用于二维数组,在这种情况下是行位置。 数组的行...您可以简单地将数组创建为arr = Range("A2:A100").value。数组中的迭代比范围中的迭代要快得多...
  • @Eduards Evaluate 有(在某种程度上......)。并且Transpose 将二维行数组转换为一维数组(无行...)。
  • Gotcha,所以它表示数组中的行。在这种情况下,你的答案是完美的!
【解决方案2】:

我认为您不需要数组。如果你想检查一个范围内的数字,你可以这样做:

isValueInRange 检查给定值是否等于或大于 minValue (120) 并且 等于或小于 maxValue。如果是,则返回 true,否则返回 false。就是这样。

Option Explicit

Private Const const_minValue As Long = 120
Private Const const_maxValue As Long = 300


Public Sub test_isValueInRange()
'this is for testing the result of isValueInRange
Dim v As Long

v = 123
If isValueInRange(v) = True Then Debug.Print "Test 1: OK" Else Debug.Print "Test 1: error"

v = 10
If isValueInRange(v) = True Then Debug.Print "Test 2: error" Else Debug.Print "Test 2: OK"

v = 301
If isValueInRange(v) = True Then Debug.Print "Test 3: error" Else Debug.Print "Test 3: OK"

v = 300
If isValueInRange(v) = True Then Debug.Print "Test 1: OK" Else Debug.Print "Test 1: error"


End Sub

Public Function isValueInRange(valueToCheck As Long, _
    Optional minValue As Long = const_minValue, _
    Optional maxValue As Long = const_maxValue)

If valueToCheck >= minValue And _
    valueToCheck <= maxValue Then
        isValueInRange = True
End If

End Function

'One-liner version of above code that uses the const-values for min and max.
Public Function isValueInRange_shortVersion(valueToCheck As Long)
   isValueInRange_shortVersion= (valueToCheck >= minValue And valueToCheck <= maxValue)
End Function

【讨论】:

  • 我更喜欢更短的 waaaaaaaay,因为我描述的任务从字面上看就像 1-2 行代码
  • 你不需要测试功能——只需要isValueInRange——你可以把它放在两行——如果你愿意的话。但我更喜欢可读代码而不是短代码:-)
  • 添加了一个衬里版本:-)
  • 是的,我仍然需要它是一个数组或字符串列表或其他什么,因为我将拥有 600 个数组,在创建它们之后我将使用 150 个 if 函数来检查哪个数组具有什么值。答案中的这段代码表明有一个数组并在它之后立即检查它
  • 否 - 此解决方案中没有数组。您可以使用 600 种不同的最小/最大组合 - 而不是 const 值。您甚至可以将这 600 个最小值/最大值放在工作表上 - 或将它们定义为二维数组
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 2020-04-28
  • 2014-04-21
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
相关资源
最近更新 更多