【问题标题】:VBA - Create an array from another arrayVBA - 从另一个数组创建一个数组
【发布时间】:2017-05-06 19:26:22
【问题描述】:

如果可能的话,我想知道如何做到这一点。我已将 arrayC(26) 定义为:

Dim arrayC(26) As String
arrayC(26) = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
Dim i As Integer 'Position in the arrayC(26)
i = 1

基于另一个单元格的值,如果它的值为 3,我想从 A 转到 C。如果它的值为 5,我想从 A 转到 E。依此类推。

Dim j As Integer
j = 1
ReDim arrayCont(j) As Variant

让我们假设它从 A 到 C。所以,我希望在 arrayCont(j) 中我有以下值“A,B,C”。然后我会去 Cells(8,"C") 检查它是否等于 arrayCont(j) 中的值。如果是这样,我想从 arrayCont(j) 中删除该值。例如,考虑 Cells(8,"C") = "B"。然后我的 arrayCont(j) 将是“A,C”。我怎样才能做到这一点?

我创建的代码是:

Do While p <= sh1.Range("D6").Value
 For p = 1 To sh1.Cells(6, "D").Value
  If sh3.Cells(8, "C").Value = arrayC(p) Then
   arrayCont(j) = arrayCont(j)
  Else
   arrayCont(j) = arrayC(p)
  End If
 Next p
 j = j + 1
Loop

提前谢谢你。

【问题讨论】:

  • 不是答案,但 arrayC 不是您认为的那样 - 它的前 26 个条目有空字符串(从索引 0 开始),然后 arrayC(26) 是完整字​​符串的字母表。我相信你想要Dim arrayC() As StringarrayC = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
  • 数组C = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q, R,S,T,U,V,W,X,Y,Z", ",") ?正如你所说,arrayC 从索引 0 开始,但如果我定义 i = 1 它从第一个条目开始,对吧?
  • 用你的代码试试Debug.Print (arrayC(26)),用我的代码试试Debug.Print (arrayC(25))看看有什么不同。
  • 谢谢大家。我也会努力学习的:D
  • bobajob,我认为两者都是正确的,因为它什么都没有,也没有显示任何错误。

标签: arrays excel dynamic vba


【解决方案1】:

首先,为了将括号内的所有字母添加到数组中的元素,数组需要是动态的 (Dim arrayC As...),而不是静态的 (Dim arrayC(26) As...)。另外,要么使用Split,要么需要在括号内的每个元素前后添加"

第二,为了在复制的数组(arrayCont)中找到“C8”中的值,也就是Redim直到i(=3),我们使用Application.Match 查找数组内匹配元素的索引。如果存在“匹配”,则我们从数组中删除该元素。

第三我正在使用一个名为 "DeleteElementAt"Sub,它从数组中删除某个元素,具体取决于您要删除的数组的索引。


我的代码(已测试)

Option Explicit

Sub SubArrayfromArray()

Dim arrayC As Variant
Dim i As Integer 'Position in the arrayC(26)
Dim arrayCont As Variant

' this is the way to put all the strings inside the brackets in an array (type Variant) in one line of code
arrayC = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
i = 3 ' i is the value of another worksheet cell

' copy original dynamic array to destination dynamic array
arrayCont = arrayC

' redim the copied array up to 3 (from 0 to 2)
ReDim Preserve arrayCont(0 To i - 1)

' value in Range C8  >> if "B" was found inside the arrayCont >> remove it from the array
If Not IsError(Application.Match(Range("C8").Value, arrayCont, 0)) Then
    Call DeleteElementAt(Application.Match(Range("C8").Value, arrayCont, 0) - 1, arrayCont)
End If
' for debug only
'For i = LBound(arrayCont) To UBound(arrayCont)
'    MsgBox "Array element " & i & " Value is " & arrayCont(i)
'Next i

' Edit 1: place arrayCont result in Cells(9, 3) >> Range("C9")
Range("C9").Resize(UBound(arrayCont) + 1, 1).Value = Application.Transpose(arrayCont)

End Sub

Sub DeleteElementAt 代码

Public Sub DeleteElementAt(ByVal ArrIndex As Integer, ByRef myArr As Variant)

' this sub removes a certain element from the array, then it shrinks the size of the array by 1    
Dim i As Integer

' move all element back one position
For i = ArrIndex + 1 To UBound(myArr)
    myArr(i - 1) = myArr(i)
Next

' shrink the array by one, removing the last one
ReDim Preserve myArr(UBound(myArr) - 1)

End Sub

【讨论】:

  • Shai Rado,我想将 arrayCont 的那些值从 Cells(9,3) 打印到 Cells(10,3) 但它会为其他维度动态执行。我写的代码: While p
  • 当它必须将字母 C 放入单元格 (10,3) 时,它会返回运行时错误类型 13。你能帮帮我吗?
  • 我现在在家,今晚试试看,如果没有,明天上班
  • @vbalearner 在我编辑的代码中看到我的Edit 1(“Sub SubArrayfromArray”中的最后 2 行)
  • 感谢 Shai Rado。有效。我不得不转置 arrayCont 来填充一列
猜你喜欢
  • 2012-12-16
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2015-08-28
相关资源
最近更新 更多