简体   繁体   English

在 vba 中定义动态范围

[英]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.在 AI 列中,从单元格 131 开始,我有一个客户列表,其长度取决于我下载数据的年份。 I would like to set the range named client equal to this variable range.我想将名为 client 的范围设置为等于此变量范围。 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:这将仅使用As Range来帮助解决问题:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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