- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在玩它,问题出在我的数组 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
最佳答案
在代码中发现了一些问题,例如 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/
我是一名优秀的程序员,十分优秀!