Excel 返回全零的数组

Excel 返回全零的数组,excel,vba,Excel,Vba,我一直在玩它,问题在于我的数组resultArray(I) 当使用.Range(“M”&i).Value代替行resultArray(i)=Sheets(“DeSL\u CP”).Range(“p”&j).Value时,它可以工作,但需要更长的时间 为什么resultarray(i)返回所有零 原职: 我有两张表:Summary在a列中有一个productid,在AK中有一个字段将产品标记为未授权或已授权。DeSL_CP对每个productId有多行(列B) 我需要为未经许可的产品找到带有活动代

我一直在玩它,问题在于我的数组resultArray(I)

当使用
.Range(“M”&i).Value代替行
resultArray(i)=Sheets(“DeSL\u CP”).Range(“p”&j).Value
时,它可以工作,但需要更长的时间

为什么resultarray(i)返回所有零

原职:

我有两张表:Summary在a列中有一个productid,在AK中有一个字段将产品标记为未授权或已授权。DeSL_CP对每个productId有多行(列B)

我需要为未经许可的产品找到带有活动代码(K列)AA0001的行,并返回基线结束日期(p列)。然后我需要找到剩余产品的代码A0003,并返回基线结束的行。基线N应在汇总表的M栏中

我的代码没有抛出错误。它用1/0/1900填充所有列M

Sheets("Summary").Select
Dim lastRow As Long, lastRow1 As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
lastRow1 = Sheets("DeSL_CP").Range("A" & Rows.Count).End(xlUp).Row
lastRow1 = lastRow1 - 1

Dim BaselineEnd As Variant, ActivityCode As Variant, ProductIDDeSL As Variant, _
  Licensed As Variant, ProductIDSumm As Variant

BaselineEnd = ThisWorkbook.Worksheets("DeSL_CP").Range("P2:P" & lastRow1).Value
ActivityCode = ThisWorkbook.Worksheets("DeSL_CP").Range("K2:K" & lastRow1).Value
ProductIDDeSL = ThisWorkbook.Worksheets("DeSL_CP").Range("B2:B" & lastRow1).Value
Licensed = ThisWorkbook.Worksheets("Summary").Range("AK7:AK" & lastRow).Value
ProductIDSumm = ThisWorkbook.Worksheets("Summary").Range("A7:A" & lastRow).Value

Dim resultArray() As Date
ReDim resultArray(7 To lastRow)
Dim i As Long, j As Long

With ThisWorkbook.Worksheets("Summary")
    For i = 7 To UBound(ProductIDSumm)
        For j = 2 To UBound(ProductIDDeSL)
            If ProductIDSumm(i, 1) = ProductIDDeSL(j, 1) Then
                If Licensed(i, 1) = "Unlicensed" Then
                    If ActivityCode(j, 1) = "AA0001" Then
                        resultArray(i) = Sheets("DeSL_CP").Range("P" & j).Value
                        Exit For
                    End If
                Else
                    If ActivityCode(j, 1) = "A0003" Then
                        resultArray(i) = Sheets("DeSL_CP").Range("P" & j).Value
                        Exit For
                    End If
                End If
            End If
        Next j
    Next i

    .Range("M7").Resize(lastRow - 7 + 1, 1).Value = resultArray
End With
有时它是空白的,但很多时候不是。我隐藏了大量数据,以关注重要的栏目。现在是世纪月,这有关系吗


在代码中发现了一些问题,如
lastRow1=Sheets(“DeSL\u CP”).Range(“A”和Rows.Count”).End(xlUp)。行
首选基于列B。还认为
for
循环的起始值应为1,而不是7和2(取决于选项基数)。ResultArray可以直接从
BaselineEnd(j,1)
填充。最后,使用
Range(“M7”).Resize(UBound(resultaray),1)解决ResultArray问题。Value=ResultArray
。合并最终代码:

    Option Base 1
Sub test()
Sheets("Summary").Select
Dim lastRow As Long, lastRow1 As Long
lastRow = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
lastRow1 = Sheets("DeSL_CP").Range("B" & Rows.Count).End(xlUp).Row
lastRow1 = lastRow1 - 1


Dim BaselineEnd As Variant, ActivityCode As Variant, ProductIDDeSL As Variant, Licensed As Variant, ProductIDSumm As Variant
BaselineEnd = ThisWorkbook.Worksheets("DeSL_CP").Range("P2:P" & lastRow1).Value
ActivityCode = ThisWorkbook.Worksheets("DeSL_CP").Range("K2:K" & lastRow1).Value
ProductIDDeSL = ThisWorkbook.Worksheets("DeSL_CP").Range("B2:B" & lastRow1).Value
Licensed = ThisWorkbook.Worksheets("Summary").Range("AK7:AK" & lastRow).Value
ProductIDSumm = ThisWorkbook.Worksheets("Summary").Range("A7:A" & lastRow).Value

Dim resultArray() As Date
ReDim resultArray(lastRow - 7 + 1, 1)
Dim i As Long, j As Long

With ThisWorkbook.Worksheets("Summary")
For i = 1 To UBound(ProductIDSumm)
    For j = 1 To UBound(ProductIDDeSL)
    'If Not Sheets("DeSL_CP").Rows(j).Hidden Then
    If ProductIDSumm(i, 1) = ProductIDDeSL(j, 1) Then
        If Licensed(i, 1) = "Unlicensed" Then
            If ActivityCode(j, 1) = "AA0001" Then
            resultArray(i, 1) = BaselineEnd(j, 1)
            Exit For
            End If
        Else
            If ActivityCode(j, 1) = "A0003" Then
            resultArray(i, 1) = BaselineEnd(j, 1)
            Exit For
            End If
        End If
    End If
    'End If
    Next j
Next i

Range("M7").Resize(UBound(resultArray), 1).Value = resultArray
End With
End Sub
我只是简单地尝试了一下,没有使用任何数组,结果发现工作正常

Sub test2()
Sheets("Summary").Select
Dim lastRow As Long, lastRow1 As Long
Dim i, j As Long, Found As Boolean
lastRow = Range("A" & Rows.Count).End(xlUp).Row
lastRow1 = Sheets("DeSL_CP").Range("B" & Rows.Count).End(xlUp).Row
lastRow1 = lastRow1


Dim BaselineEnd As Variant, ActivityCode As Variant, ProductIDDeSL As Variant, Licensed As Variant, ProductIDSumm As Variant

For i = 7 To lastRow
Found = False
ProductIDSumm = ThisWorkbook.Worksheets("Summary").Cells(i, 1).Value
Licensed = ThisWorkbook.Worksheets("Summary").Cells(i, 37).Value
If ProductIDSumm <> "" Then
    For j = 2 To lastRow1
    ProductIDDeSL = ThisWorkbook.Worksheets("DeSL_CP").Cells(j, 2).Value    'Col B
    ActivityCode = ThisWorkbook.Worksheets("DeSL_CP").Cells(j, 11).Value   'Col K
    BaselineEnd = ThisWorkbook.Worksheets("DeSL_CP").Cells(j, 16).Value    ' Col P
    If ProductIDDeSL <> "" Then              ' to skip blank rows
    If ProductIDSumm = ProductIDDeSL Then
        If Licensed = "Unlicensed" Then
            If ActivityCode = "AA0001" Then
            Found = True
            Exit For
            End If
        Else
            If ActivityCode = "A0003" Then
            Found = True
            Exit For
            End If
        End If
    End If
    End If
    Next j
ThisWorkbook.Worksheets("Summary").Cells(i, 13).Value = IIf(Found, BaselineEnd, "Not Found")
End If
Next i
子测试2()
表格(“摘要”)。选择
将最后一行的长度调暗,最后一行的长度调暗
Dim i,j等于Long,发现为布尔值
lastRow=范围(“A”和Rows.Count).End(xlUp).Row
lastRow1=工作表(“DeSL_CP”)。范围(“B”和行数。计数)。结束(xlUp)。行
lastRow1=lastRow1
Dim BaselineEnd作为变体,ActivityCode作为变体,ProductIDDeSL作为变体,许可作为变体,ProductIDSumm作为变体
从i=7到最后一行
发现=错误
ProductIDSumm=此工作簿。工作表(“摘要”)。单元格(i,1)。值
许可=此工作簿。工作表(“摘要”)。单元格(i,37)。值
如果ProductIDSumm为“”,则
对于j=2到最后一行1
ProductIDDeSL=此工作簿。工作表(“DeSL_CP”)。单元格(j,2)。值“列B”
ActivityCode=ThisWorkbook.Worksheets(“DeSL_CP”).单元格(j,11).值'Col K
BaselineEnd=此工作簿。工作表(“DeSL_CP”)。单元格(j,16)。值“Col P”
如果ProductIDDeSL为“”,则“”将跳过空行
如果ProductIDSumm=ProductIDDeSL,则
如果许可=“未许可”,则
如果ActivityCode=“AA0001”,则
找到=真
退出
如果结束
其他的
如果ActivityCode=“A0003”,则
找到=真
退出
如果结束
如果结束
如果结束
如果结束
下一个j
ThisWorkbook.Worksheets(“Summary”).Cells(i,13).Value=IIf(已找到,基线结束,“未找到”)
如果结束
接下来我
编辑:因为假设您拥有大量数据,并且存在处理时间问题。出于好奇,我添加了find方法解决方案作为第三个选项

Sub test3()
Sheets("Summary").Select
Dim lastRow As Long, lastRow1 As Long
Dim i, j As Long, Found As Boolean
lastRow = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
lastRow1 = Sheets("DeSL_CP").Range("B" & Rows.Count).End(xlUp).Row
lastRow1 = lastRow1
Dim RngIDsm, RngIDde, Cl, Cl2 As Range
Set RngIDsm = Sheets("Summary").Range("A7:A" & lastRow)
Set RngIDde = Sheets("DeSL_CP").Range("B2:B" & lastRow1)
Dim BaselineEnd As Variant, ActivityCode As Variant, ProductIDDeSL As Variant, Licensed As Variant, ProductIDSumm As Variant

For Each Cl In RngIDsm
Found = False
ProductIDSumm = Cl.Value
Licensed = Cl.Offset(, 36).Value
    With RngIDde
    Set Cl2 = .Find(ProductIDSumm, LookIn:=xlValues)
    If Not Cl2 Is Nothing Then
        firstAddress = Cl2.Address
        Do
        ActivityCode = Cl2.Offset(, 9).Value  'Col K
            If Licensed = "Unlicensed" Then
                If ActivityCode = "AA0001" Then
                BaselineEnd = Cl2.Offset(, 14).Value
                Found = True
                Exit Do
                End If
            Else
                If ActivityCode = "A0003" Then
                BaselineEnd = Cl2.Offset(, 14).Value   
                Found = True
                Exit Do
                End If
            End If
        Set Cl2 = .FindNext(Cl2)
        Loop While Not Cl2 Is Nothing And Cl2.Address <> firstAddress
    End If
    End With
Cl.Offset(, 12).Value = IIf(Found, BaselineEnd, "Not Found")
Next Cl
End Sub
子测试3()
表格(“摘要”)。选择
将最后一行的长度调暗,最后一行的长度调暗
Dim i,j等于Long,发现为布尔值
lastRow=工作表(“摘要”).Range(“A”和Rows.Count).End(xlUp).Row
lastRow1=工作表(“DeSL_CP”)。范围(“B”和行数。计数)。结束(xlUp)。行
lastRow1=lastRow1
变暗RngIDsm、RngIDde、Cl、Cl2作为范围
设置RngIDsm=工作表(“摘要”)。范围(“A7:A”和最后一行)
设置RngIDde=图纸(“图纸”)范围(“B2:B”和最后一行1)
Dim BaselineEnd作为变体,ActivityCode作为变体,ProductIDDeSL作为变体,许可作为变体,ProductIDSumm作为变体
对于RngIDsm中的每个Cl
发现=错误
ProductIDSumm=Cl.值
许可=Cl.偏移量(,36).值
与恩吉德
Set Cl2=.Find(ProductIDSumm,LookIn:=xlValues)
如果不是,那么Cl2什么都不是
firstAddress=Cl2.Address
做
ActivityCode=Cl2.偏移量(,9).值'Col K
如果许可=“未许可”,则
如果ActivityCode=“AA0001”,则
BaselineEnd=Cl2.Offset(,14).Value
找到=真
退出Do
如果结束
其他的
如果ActivityCode=“A0003”,则
BaselineEnd=Cl2.Offset(,14).Value
找到=真
退出Do
如果结束
如果结束
设置Cl2=.FindNext(Cl2)
循环而非Cl2为Nothing,Cl2.Address为firstAddress
如果结束
以
Cl.偏移量(,12).值=IIf(已找到,基线结束,“未找到”)
下一个Cl
端接头

很高兴听到你让它工作了

关于您的原始问题,您需要
WorksheetFunction.Transpose(resultArray)
才能将其粘贴到垂直列


不确定这是否会更快,但您能给我们一个它正在使用的数据示例吗?目前它看起来像是
Sheets(“DeSL_CP”)。范围(“p”&i)。值可能为空,或者是错误的范围参考。添加了示例!请注意:1/0/1990是Excel的一种说法,你给了它一个0或没有该点的值……这意味着在你的
resultaray
中的那些点是空的。我明白这一点,但它给了我所有的1/0/1990,它们不都是空白的…
resultaray(i)
将等于
/
下一个
循环的
j
的最后一个结果,所有以前的值都将被覆盖。这就是你想要的吗?嗨!谢谢你全面的回答。第一段代码仍然返回全零。但我确实意识到,我的问题是我的结果数组需要有两个维度。所以我正在努力。第二段代码确实有效,但运行时间超过5分钟。如果你把我的代码和编辑放在问题的顶部,它大约是9秒-这现在起作用,但我现在是tr