Excel get range for named dynamic range (OFFSET)

2019-09-16 01:06发布

问题:

hi I have a data Validation for a dropdown cell on Sheet1, the List is a named dynamic range whose equation is: =OFFSET(Sheet2!$J$3,0,0,COUNTA(Sheet2!$J:$J)-2,1)

Works fine.

But now I want to get the Excel cell reference for this; in my example the range is sheet2!$j$3:$j$30. I need that string to put into a Range reference

I've done this by parsing but slows down my spreadsheet

回答1:

A VBA solution to the question you asked might be:

Dim x as String
With Worksheets("Sheet2")
    x = "'" & .Name & "'!" & .Range("J3", .Range("J3").Offset(Application.WorksheetFunction.CountA(.Columns("J")) - 2, 0)).Address
End With
'The value of variable x is now the address you asked for

but maybe what you are really trying to do is:

Dim rng As Range
With Worksheets("Sheet2")
    Set rng = .Range("J3:J" & .Range("J" & .Rows.Count).End(xlUp).Row)
End With
'rng will now be a Range object referring to everything in
' column J, starting at row 3 and ending at the last
' non-blank cell in column J


回答2:

You don't have to do anything special. The formula will return a valid range.