【问题标题】:Using Range(Cell1 value, Cell2 value) - VBA使用范围(单元格 1 值,单元格 2 值) - VBA
【发布时间】:2020-02-09 03:16:15
【问题描述】:

我试图通过两个不同单元格的内容来定义一个范围,每个单元格都包含间接单元格地址。我不确定这是否可能,但这里有一个例子:

  • 单元格X100 包含值$A$1
  • 单元格Y200 包含值$C$5

有什么方法可以使用Range() 和单元格X100Y200 到达Range("$A$1:$C$5")

我尝试过使用 Cells.Address,但我无法确定应用程序的正确格式。任何帮助表示赞赏!

谢谢

编辑

谢谢汤姆!我还有一个问题要问你。 X100 单元格在我的情况下实际上是可变的,我使用以下公式找到它:

Cells.Find("ID").Offset(1,0).Address

有没有办法将这种公式合并到 Range 应用程序中?或者在包含这个公式的电子表格中定义一个静态单元格会更容易吗?

非常感谢

编辑 2

你来了!我将 r 和 x 调暗为范围并将它们设置如下:

r = Cells.Find("ID").Offset(1,0).Address

x = Cells.Find("Description of initiative").offset(1,0).end(xldown).Offset(0,cells.Find("ID").Column-cells.Find("Description of initiative").Column).address

我知道它们很复杂,但我将它们打印出来,它们以 $A$1 格式返回正确的单元格。

希望这可以澄清!非常感谢您的帮助。

【问题讨论】:

  • 不要使用找到的单元格的地址。 Dim r as Range,然后是 Set r = Cells.Find("ID").Offset(1)。现在r是你想要的范围(只要查找成功)。
  • 我已经尝试过了,但是如何将它写成 Range()?我试过 Range(r&":"&x) 和 Range(r,x) 但都不管用。
  • 你能edit问你如何得到rx吗?
  • 绝对!抱歉耽搁了,我马上去。

标签: excel vba range cell


【解决方案1】:

你是说

Range(Range("X100").Value2 & ":" & Range("Y200").Value2)

【讨论】:

  • 是的!好的,这是有道理的。如果你不介意的话,再给你一个!
【解决方案2】:

与其使用地址,不如使用Range 对象。

我不确定我是否完全理解您的设置,但您可能正在寻找类似的内容。

Sub Test()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim startCell As Range
    Set startCell = ws.Cells.Find(What:="ID") '<--- you should specify the other parameters of Find

    Dim endCell As Range
    Set endCell = ws.Cells.Find(What:="Description of initiative") '<--- again, specify parameters of Find

    If startCell Is Nothing Then Exit Sub '<--- Find was unsuccessful
    If endCell Is Nothing Then Exit Sub '<--- Find was unsuccessful

    Set startCell = startCell.Offset(1, 0)

    Dim columnOffset As Long
    columnOffset = startCell.Column - endCell.Column

    Set endCell = endCell.Offset(1).End(xlDown)
    Set endCell = endCell.Offset(, columnOffset) '<--- there's a simpler way to do this, this just gets you back to startCell.Column, but preserving your logic

    Dim myRange As Range
    Set myRange = ws.Range(startCell, endCell)

End Sub

这是获取endCell 而不是偏移量的更简单方法。

Sub Test()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim startCell As Range
    Set startCell = ws.Cells.Find(What:="ID") '<--- you should specify the other parameters of Find

    Dim endCell As Range
    Set endCell = ws.Cells.Find(What:="Description of initiative") '<--- again, specify parameters of Find

    If startCell Is Nothing Then Exit Sub '<--- Find was unsuccessful
    If endCell Is Nothing Then Exit Sub '<--- Find was unsuccessful

    Set startCell = startCell.Offset(1, 0)

    Dim lastRow As Long
    lastRow = endCell.Offset(1).End(xlDown).Row
    Set endCell = ws.Cells(lastRow, startCell.Column)

    Dim myRange As Range
    Set myRange = ws.Range(startCell, endCell)

End Sub

【讨论】:

  • 有效!哦,伙计,非常感谢,我花了很多时间在这上面。你是个圣人——感谢你花时间为一个随机的人解决这个问题。我正在为后代保存此代码。度过美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多