我有以下代码:
With Sh.Range("A1:A" & LastRow + 1).Offset(0, 4)
.FormulaArray = "=MAX(IF(A2:A" & LastRow + 1 & "= ** , D2:D" & LastRow + 1 & "))"
.Value = .Value
End With
在我有**的地方,我想要一个动态的细胞参考。如果我使用的是.formulaR1C1,我会插入RC [-1],但我无法使用.formulaArray。
是否有人知道如何插入相对单元格引用,该引用会随着在该范围内粘贴的公式而改变?
谢谢
编辑#1
整个代码如下所示:
Sub RemoveDuplicates_SumMarketValue()
Dim Sh As Worksheet
Dim LastRow As Long
Dim Rng As Range
Set Sh = Worksheets(1)
Sh.Columns(6).Insert
LastRow = Sh.Range("A65536").End(xlUp).Row
With Sh.Range("A1:A" & LastRow).Offset(0, 5)
.FormulaR1C1 = "=IF(COUNTIF(R1C[-5]:RC[-5],RC[-5])>1,"""",SUMIF(R1C[-5]:R[" & LastRow & "]C[-5],RC[-5],R1C[-1]:R[" & LastRow & "]C[-1]))"
.Value = .Value
End With
Sh.Columns(5).Delete
Sh.Rows(1).Insert
Sh.Columns(5).Insert
With Sh.Range("A1:A" & LastRow + 1).Offset(0, 4)
.FormulaArray = "=MAX(IF(A2:A" & LastRow + 1 & "= A1 , D2:D" & LastRow + 1 & "))"
.Value = .Value
End With
Set Rng = Sh.Range("E1:E" & LastRow + 1)
With Rng
.AutoFilter Field:=1, Criteria1:="="
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
此代码的目的是通过数据样本和
来看现在我还希望它将所有重复项的第4列的最大值保留在最终版本中,但我无法使数组公式正确引用该行。
答案 0 :(得分:2)
编辑:尝试在" ThisWorkbook"代码表:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim Sh As Worksheet
Dim LastRow As Long
Dim Rng As Range
Set Sh = Worksheets(1)
LastRow = Sh.Range("A65536").End(xlUp).Row
With Sh.Range("A1:A" & LastRow).Offset(0, 5)
.FormulaR1C1 = "=IF(COUNTIF(R1C[-5]:RC[-5],RC[-5])>1,"""",SUMIF(R1C[-5]:R[" & LastRow & "]C[-5],RC[-5],R1C[-1]:R[" & LastRow & "]C[-1]))"
.Value = .Value
End With
With Sh.Range("A1:A" & LastRow + 1).Offset(0, 4)
.FormulaArray = "=MAX(IF(A2:A" & LastRow + 1 & "= A1 , D2:D" & LastRow + 1 & "))"
.Value = .Value
End With
'This section you might want to remove from this routine
Set Rng = Sh.Range("E1:E" & LastRow + 1)
With Rng
.AutoFilter Field:=1, Criteria1:="="
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
它基本上与您的函数相同,但它不会删除列或添加任何行。每当你的一个单元格内容发生变化时,会发生什么情况,这个宏会自动运行,更新单元格中的公式。
您可以实现最接近的目标,即在每次对工作表进行更改时将在后台运行宏。如果您有数万行,或者计算机速度非常慢,这可能不是理想的解决方案。但是,如果不是这种情况,您可能会发现只需很少的更改就可以轻松地使用代码。
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Update your formula here with the new content/reference
' (your code + some changes to update where the last row is)
End Sub
将其粘贴到" ThisWorkbook"中,只需将代码放入其中。
答案 1 :(得分:1)
这就是我想出来解决.formulaArray不接受RC单元格引用表示法的问题。我只是使用循环将数组公式插入到每个单元格中,并使用循环变量i来引用目标行。
代码:
Sub RemoveDuplicates_SumMarketValue()
Dim Sh As Worksheet
Dim LastRow As Long
Dim Rng As Range
Dim targetcell As Range
Set Sh = Worksheets(1)
Sh.Columns(6).Insert
LastRow = Sh.Range("A65536").End(xlUp).Row
With Sh.Range("A1:A" & LastRow).Offset(0, 5)
.FormulaR1C1 = "=IF(COUNTIF(R1C[-5]:RC[-5],RC[-5])>1,"""",SUMIF(R1C[-5]:R[" & LastRow & "]C[-5],RC[-5],R1C[-1]:R[" & LastRow & "]C[-1]))"
.Value = .Value
End With
Sh.Columns(5).Delete
Sh.Rows(1).Insert
Sh.Columns(5).Insert
For i = 2 To LastRow + 1
Cells(i, 5).FormulaArray = "=MAX(IF(A2:A" & LastRow + 1 & "= A" & i & " , D2:D" & LastRow + 1 & "))"
Cells(i, 5) = Cells(i, 5).Value
Next
Sh.Columns(4).Delete
Set Rng = Sh.Range("E1:E" & LastRow + 1)
With Rng
.AutoFilter Field:=1, Criteria1:="="
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
表格(1)。细胞(1,4)=“价格” 表格(1)。细胞(1,5)=“市场价值”
End Sub
所以这段代码的作用是,在Col 1中循环重复,在col 5中汇总相关值,并在col 4中选择最大相关值。
答案 2 :(得分:0)
你可以使用,填充单元格并复制它,
L = LastRow + 1
With Sh.Range("A1:A" & L).Offset(0, 4)
.Cells(1,1).FormulaArray = "=MAX(IF(A$2:A$" & L & "=A1,D$2:D$" & L & "))"
.FillDown
.Value = .Value
End With
使用Application.ConvertFormula
轻松处理A1与R1C1样式需要注意Row / Col Abs / Rel引用。