简体   繁体   中英

Excel VBA Formula with Dynamic Range is not working

I have following formula in Excel, where I applying countif with dynamic range. When I apply this formula in macro, this is not giving value instead single quote (') is covering the range.

Cells(XRow, 3).Formula = "=COUNTIF( " & Range(Cells(6, 5), Cells(YRow - 1, 5)).Address(0, 0) & "  ,RC[-1])"

This is what I get in cell

=COUNTIF( 'E6':'E212',B6)

I am not sure why single quote occupy the cell 'E6', If I remove ' manually from cell, it works. Any help would be much appreciated.

You are mixing styles of references xlA1 and xlR1C1 :

Use this :

Dim Rg As Range
Set Rg = Cells(XRow, 3)
Rg.Formula = "=COUNTIF(" & Range(Cells(6, 5), Cells(YRow - 1, 5)).Address(0, 0) & "," & Rg.Offset(0, -1).Address(0, 0) & ")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM