gpt4 book ai didi

excel - 返回全零的数组

转载 作者:行者123 更新时间:2023-12-04 22:30:30 24 4
gpt4 key购买 nike

我一直在玩它,问题出在我的数组 resultArray(i) 中。

当而不是行 resultArray(i) = Sheets("DeSL_CP").Range("P" & j).Value , 我用 .Range("M" & i).Value = Sheets("DeSL_CP").Range("P" & j).Value ,它有效,但需要更长的时间。

为什么 resultarray(i) 返回全零?

原帖:

我有两张表:摘要在 col A 中有一个 productid,在 AK 中有一个将产品标记为未许可或许可的字段。 DeSL_CP 对每个 productId 都有多行(在 col B 中)。

我需要为未经许可的产品找到带有事件代码 (Col K) AA0001 的行,并返回基线结束日期 (col 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

有时它是空白的,但很多时候不是。我隐藏了大量数据以专注于重要的列。现在是世纪月——这有关系吗?

DeSL_CP Tab

Summary Tab

最佳答案

在代码中发现了一些问题,例如 lastRow1 = Sheets("DeSL_CP").Range("A" & Rows.Count).End(xlUp).Row首选基于 Col B。还认为 For 的起始值loops 应该是 1 而不是 7 和 2(取决于 Option Base)。 ResultArray 可以直接从 BaselineEnd(j, 1) 填充.最后 ResultArray 解决了 Range("M7").Resize(UBound(resultArray), 1).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

编辑:因为您应该拥有大量数据并且存在处理时间问题。只是出于好奇,我将查找方法解决方案添加为第三个选项
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

关于excel - 返回全零的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53266998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com