我无法解决以下问题,任何帮助将不胜感激
我有一个如下所示的二维数组;
我想从中创建一个新数组(PFArr),但没有删除="is"的行。我可以使这个新数组的大小与原始数组的大小相同,只在删除了数据的地方留下空白行,但是这个数组将在代码中的多次循环中使用,所以我想使其尽可能紧凑。
通过下面的代码,我尝试使用循环来获取已删除= 0(我想保留的)的元素数量。
然后将新数组重新调整为这个大小。
然后它应该通过第二个循环将旧数组中的相关元素分配给新数组。然而,它没有按预期将元素分配到数组中,它似乎循环得很好,只是没有添加新元素。有什么想法吗?
Sub AddSelectDataFromBigArrayToSmallOne()
Dim PFAllArr As Variant
Dim PFArr As Variant
Dim c1, i1, c2, i2 As Long
PFAllArr = Sheets("PF File Simple").Range("A2").CurrentRegion.Value
'get number of elements i want to transfer to new array
c1 = 1
For i1 = LBound(PFAllArr) To UBound(PFAllArr)
If PFAllArr(i1, 2) = 0 Then
c1 = c1 + 1
End If
Next i1
'Make new array this size
ReDim PFArr(LBound(PFAllArr) To c1, 1 To 4)
'Assign elements from old array nto new one
c2 = 1
For i2 = LBound(PFAllArr) To UBound(PFAllArr)
If PFAllArr(i2, 2) = 0 Then
PFArr(c2, 3) = PFAllArr(i2, 3)
PFArr(c2, 4) = PFAllArr(i2, 4)
c2 = c2 + 1
End If
Debug.Print c2, PFArr(c2, 3), PFArr(c2, 4)
Next i2
End Sub
最佳答案
也许是这样的?
Sub tgr()
Dim aTemp As Variant
Dim aData As Variant
Dim iyTemp As Long
Dim iyData As Long
Dim ix As Long
With ActiveWorkbook.Sheets("PF File Simple").Range("A2").CurrentRegion
aTemp = .Value
ReDim aData(1 To WorksheetFunction.CountIf(.Resize(, 1).Offset(, 1), 0), 1 To .Columns.Count)
End With
For iyTemp = 1 To UBound(aTemp, 1)
If aTemp(iyTemp, 2) = 0 Then
iyData = iyData + 1
For ix = 1 To UBound(aTemp, 2)
aData(iyData, ix) = aTemp(iyTemp, ix)
Next ix
End If
Next iyTemp
'aData is now populated with only values where the second column is 0
End Sub
关于excel - 从更大的二维数组创建二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562776/