简体   繁体   中英

define dynamic range in vba

I have the following problem:

I am trying to set the range client equal to a dynamically changing range.

In column AI, starting from cell 131, I have a list of clients, which varies in length depending on the years I download data for. I would like to set the range named client equal to this variable range. My code so far is:

Sub range()

Dim client As range
Dim lastrow As Long
Dim startingcell As Long

Set startingcell = range("AI131")
lastrow = Cells(Row.Count, startingcell.Column).End(xlUp).Row
Set client = range(startingcell, Cells(lastrow, startingcells.Column))

End Sub

It is not working though, any suggestions?

Thank you

Looks like you've an error with dimensioning and use of variables not being correct.

This will utilize only As Range which should help resolve the issue:

Dim startcell As Range, endcell As Range, client As Range

Set startcell = Cells(131, "AI")
Set endcell = Cells(Rows.Count, "AI").End(xlUp)
Set client = Range(startcell, endcell)

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