我正在尝试创建动态范围来对数据库进行排序,并且在此代码的最后一行出现类型不匹配错误。我猜它来自我用变量替换值的地方。任何人都有关于如何修复语法的任何提示?
Sub Fake_Code()
Dim Row_Limit2 As Long
Dim Row_Limit1 As Long
Dim Current_Sheet As String
Dim r As Range
Dim r2 As Range
Cells.Find(What:="Title", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Name = "Range1"
Set r = Range("Range1")
Cells.Find(What:="Country", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Name = "Range2"
Set r2 = Range("Range2")
Row_Limit1 = Worksheets(" Branded").Range("X:X").Cells.SpecialCells(xlCellTypeConstants).Count
Row_Limit2 = Worksheets(" Branded").Range("Y:Y").Cells.SpecialCells(xlCellTypeConstants).Count
'I need to make all of the below dynamic based on the variables above
Worksheets(" Branded").Range("A1").Formula = "=COUNTIFS([" & r & "]: [" & r2 & "],RC2,R21C22:R71C22,R2C)"
End Sub
答案 0 :(得分:1)
您的专线正在构建公式,但是将范围传递给它而不仅仅是范围的地址,请使用.Address(ReferenceStyle:=xlR1C1
。一旦那样,删除方括号: -
Worksheets(" Branded").Range("A1").Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ":" & r2.Address(ReferenceStyle:=xlR1C1) & ",RC2,R21C22:R71C22,R2C)"
它显示为#value
但我们没有完整的数据集可供测试。
答案 1 :(得分:0)
我意识到我在整条线上犯了一个错误。通过上面的回答,我发现它需要。
Worksheets(" Branded").Range("A1").Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"