Posted 13 December 2022, 9:24 pm EST
- Updated 13 December 2022, 9:29 pm EST
Hello Yukinori,
As per the developers, This behavior is intended. Because Microsoft Excel looks in all cells in the worksheet of the specified Range if the Range is a cell.
Sub Macro1()
Cells.ClearContents
Range(“A1”).FormulaR1C1 = “=1+2”
Range(“A2”).FormulaR1C1 = “=2+3”
Set a3FindFormulas = Range(“A3”).SpecialCells(xlCellTypeFormulas)
MsgBox a3FindFormulas.Address
End Sub
f you want to check whether a range has formula cells, you need to use Range.Count and Range.HasFormula first.
If Range.Count =1 then use Range.HasFormula . Otherwise, use Range.SpecialCells.
private static IRange FindFormulaCellsExact(IRange lookIn)
{
if (lookIn == null)
{
return lookIn;
}
if (lookIn.Count < 2)
{
return lookIn.HasFormula ? lookIn : null;
}
return lookIn.SpecialCells(SpecialCellType.Formulas);
}
Regards,
Prabhat Sharma.