我目前正在使用VBA自动化销售报告,但在使用动态范围内的VBA插入公式时遇到了麻烦。我的查找公式可以查找客户订购的最后一周。
当前星期是最后一周,并且始终是“总计”列之前的列。我在参考本周的最后一栏时遇到麻烦。
=LOOKUP(2,1/(CL[@[Week 1]:[Week 17]]>0),COLUMN(CL[[#Headers],[Week 1]:[Week 17]]))
我不确定如何使用标头进行引用。
因此,我的代码的第一部分找到了列号,并使用列号得到了Letter引用。不确定如何在我的LOOKUP
公式
Sub LastOrder()
Dim strSearch As String
Dim strSearchEnd As String
Dim aCell As Range
Dim endCell As Range
Dim startingCol As Variant
Dim endingCol As Variant
Dim colFirstWeek As Variant
Dim ColLastWeek As Variant
Dim firstCheck As Variant
Dim lastCheck As Variant
'find the column number for week 1 and total
strSearch = "Week 1"
strSearchEnd = "Total"
Set aCell = Sheet1.Rows(1).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
startingCol = aCell.Column
End If
Set endCell = Sheet1.Rows(1).Find(What:=strSearchEnd, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not endCell Is Nothing Then
endingCol = endCell.Column - 1
'this is used to get column number of current week
End If
'Use letter reference
firstCheck = Split(Cells(, startingCol).Address, "$")(1)
lastCheck = Split(Cells(, endingCol).Address, "$")(1)
Debug.Print (firstCheck)
Debug.Print (lastCheck)
Range("CL[Last Week Ordered]").FormulaR1C1 = _
"LOOKUP(2,1/(firstCheck:lastCheck>0),COLUMN(firstCheck:lastCheck))
答案 0 :(得分:0)
您可以尝试以下公式:
=LOOKUP(2, 1 / ( [@[Week 1]]:INDEX([@], , COLUMNS([@]) - 1) > 0 ),
COLUMN( [@[Week 1]]:INDEX([@], , COLUMNS([@]) - 1) ) )
很少有像INDEX
和OFFSET
这样的Excel函数返回范围引用,因此它们可以与VBA中的范围运算符:
和Range("INDEX(A1:B1, 1, 1)")
一起使用。