简体   繁体   中英

Referencing a named offset address within a range function in VBA Excel

I'm trying to call a range between a fixed cell and a dynamic cell (which i can reference using a defined name and an offset). Im not too sure how the syntax should go. Currently I have:

Range("B21:Range("Anchor").Offset(-1,1)")
'(i know this is incorrect syntax, but just to show you the logic)

Where B21 is the fixed cell. And "Anchor" is the Defined name which has a dynamic cell address.

For some background: I'm trying to call all cells within a column of a table, however the table has an "add rows" function, so the last cell of the column is always changing. My "Anchor" is below the position the last row will be.

Im not very good with VBA language, so please bear with me.

You can do it like this:

Dim ws as worksheet, rng as range

set ws = activesheet

With ws
    Set rng = .Range(.Range("B21"), .Range("Anchor").Offset(-1,1))
end with

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