【问题标题】:Dynamic Range Excel VBA: can you stock a cell range and then call it again with range function?动态范围 Excel VBA:您可以存储一个单元格范围,然后使用范围函数再次调用它吗?
【发布时间】:2019-11-18 22:28:07
【问题描述】:

我正在尝试使用 for 循环在我的电子表格中的一系列范围周围绘制边框,但范围的大小存储在变量中。这是我的代码:

    For i = 0 To 19
        Dim top_left, bottom_right As Range
        Set top_left = Range("G4").Offset(0, 2 * i)
        Set bottom_right = Range("H" & size).Offset(0, 2 * i)
        Worksheets(3).Range(top_left & ":" & bottom_left).BorderAround ColorIndex:=1
    Next

size 是从单元格 G4 向下​​开始的范围的垂直长度,范围的宽度是 2 个单元格(因此是 For 循环偏移量中的 2*i)。

错误在这一行:

    Worksheets(3).Range(top_left & ":" & bottom_left).BorderAround ColorIndex:=1

我尝试将范围转换为地址或字符串,但它不起作用。有什么想法吗?

【问题讨论】:

  • Worksheets(3).Range(top_left.Address & ":" & bottom_left.Address)....Worksheets(3).Range(top_left, bottom_left)...
  • 尝试使用Range(top_left, bottom_left)
  • 除了你Set bottom_right,但正在尝试使用bottom_left。在模块顶部添加Option Explicit

标签: excel vba variables range


【解决方案1】:

首先,您在错误的行中写入了“bottom_left”,但“bottom_left”不存在。我想你想写“bottom_right”,不是吗?

其次,“top_left”和“bottom_right”不代表像“A1”这样的地址,它们包含值。您不能将它们与“Range(”一起使用。

试试这个:

        Worksheets(3).Range(top_left.Address & ":" & bottom_right.Address).BorderAround ColorIndex:=1

【讨论】:

  • 谢谢!事实上,bottom_left 是一个错字,我的意思是 bottom_right! . 带有“:”的地址和两个范围之间的逗号都可以。 :D
猜你喜欢
  • 1970-01-01
  • 2012-08-17
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多