vba - 使用一行代码清除范围数组

标签 vba excel

我已经为范围创建了全局字符串常量。

Global Const Graph_Cl As String = "G2:G13"
Global Const Graph_Vl As String = "I2:I13"
Global Const Graph_VlS As String = "L2:L13"

我想使用一行代码清除这些范围。

With wkDataToUse
    .Range(Graph_Cl, Graph_Vl, Graph_VlS).ClearContents
End With

但我收到错误:

"Wrong no of arguments or invalid property assignment!"

我什至尝试过这个:

With wkDataToUse
    .Range(Array(Graph_Cl, Graph_Vl, Graph_VlS)).ClearContents
End With

或者

With wkDataToUse
    .Range([{Graph_Cl, Graph_Vl, Graph_VlS}]).ClearContents
End With

如何在一行代码中清除所有范围?

最佳答案

要传递多个范围地址,您需要逗号分隔的字符串格式:"a:b,c:d,e:f" 这样您就可以;

.Range(Graph_Cl & "," & Graph_Vl & "," & Graph_VlS).ClearContents

你也可以;

Union(.Range(Graph_Cl), .Range(Graph_Vl), .Range(Graph_VlS)).ClearContents

关于vba - 使用一行代码清除范围数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10563423/

相关文章:

Excel VBA 运行时错误 '424' : Object Required when trying to copy TextBox

html - 识别 XMLHTTP 响应中的 NextSibling

excel - 如何正确将秒转换为 hh :mm:ss in Excel

vba - Excel 按钮增长

excel - 带有日期的 Excel Open XML 的最小样式表?

excel - MacOS excel 中的这段代码相当于什么?

vba - Vba 中的 Http Post

vba - Excel VBA 按值赋值还是按引用赋值?

excel - 在不更改范围的情况下填写 VLOOKUP 公式

vba - Excel VBA - 在复制之前检查某个范围内的数据是否已存在