首页 > 解决方案 > .Formula 中从范围到字符串

问题描述

我可能会觉得这有点奇怪,但我选择了单元格“E2”并一直到最后一个单元格。

我还找到了包含“小计”一词的单元格的位置并返回地址。我希望该单元格旁边的单元格包含一个公式,该公式仅当“F”列中的相应单元格在单元格中输入“是”字样时,才会将所有单元格从“E2”向下加到第一个空单元格.

它一直有效到实际公式,但我不确定我是否错过了语法错误或某种错误。


Sheets(TextBox1.Value).Select

Dim LastClass As Variant
Range("E2").Select
Selection.End(xlDown).Select
LastClass = Cells(Selection.Row, 
Selection.Column).Address(0, 0)

' Finds "subtotal"
Set Subtotal = Sheets(TextBox1.Value).Range("D:D")
Set Subtotal2 = Subtotal.Find("Subtotal")
SubAddress = Cells(Subtotal2.Row, Subtotal2.Column).Address(0, 0)
RR = Range(Subtotal2.Address).Row - 2
Dim RRs As String
RRs = CStr(RR)
SubAddress.Offset(1, 0).Formula = "=SUMIF(F2:F" & RRs & ",""Yes"",E2:" & LastClass & ")"

标签: stringvbaexcelexcel-formulaoffset

解决方案


除了让它尝试将公式放在一行而不是一列(这没什么大不了的)之外,我还试图从单元格字符串而不是从该字符串表示的范围偏移。我所要做的就是放进SubAddressRange

Sheets(TextBox1.Value).Select

Dim LastClass As Variant
Range("E2").Select
Selection.End(xlDown).Select
LastClass = Cells(Selection.Row, Selection.Column).Address(0, 0)

' Finds "subtotal"
Set Subtotal = Sheets(TextBox1.Value).Range("D:D")
Set Subtotal2 = Subtotal.Find("Subtotal")
SubAddress = Cells(Subtotal2.Row, Subtotal2.Column).Address(0, 0)
RR = Range(Subtotal2.Address).Row - 2
Dim RRs As String
RRs = CStr(RR)
Range(SubAddress).Offset(0, 1).Formula = "=SUMIF(F2:F" & RRs & ",""Yes"",E2:" & LastClass & ")"

推荐阅读