【问题标题】:Get CurrentRegion only in vertical direction仅在垂直方向获取 CurrentRegion
【发布时间】:2017-11-10 23:36:10
【问题描述】:

我想编写一个 UDF(用户定义函数,又名宏),将在每个绿色单元格中使用。在此函数/宏中,想要获取当前绿色单元格组旁边的框架单元格中最长字符串的长度。为了在宏中执行此操作,我需要确定一个范围,该范围代表当前单元格旁边的所有框架单元格。 (这个计算应该为一个绿色组中的每个单元格生成相同的范围对象,但组之间的对象不同。)您将如何获得此范围?

我的第一次尝试是这样的:

Range(Application.Caller.Offset(0, -1).End(xlUp),_
      Application.Caller.Offset(0, -1).End(xlDown))

但是这个

  • 不起作用
  • 如果调用方单元格是组的最上面或最下面的单元格,则会给出错误的范围。

我需要ActiveCell.Offset(0, -1).CurrentRegion 之类的东西,但仅限垂直方向。

【问题讨论】:

  • 在 UDF decisionmodels.com/calcsecretsj.htm 中使用 CurrentRegion 存在问题
  • 不清楚你如何设置绿色单元格的范围以及你想按顺序做什么。如果绿色单元格中有数据,或者是数字或字符,我们需要更多信息。

标签: vba excel


【解决方案1】:

试试这个:

Function findlongest()

Dim fullcolumn() As Variant
Dim lastrow As Long
Dim i As Long, j As Long, k As Long
Dim tmax As Long
tmax = 0
With Application.Caller
    lastrow = .Parent.Cells(.Parent.Rows.Count, .Column - 1).End(xlUp).Row
    fullcolumn = .Parent.Range(.Parent.Cells(1, .Column - 1), .Parent.Cells(lastrow, .Column - 1)).Value
    For j = .Row To 1 Step -1
        If fullcolumn(j, 1) = "" Then
            j = j + 1
            Exit For
        ElseIf j = 1 Then
            Exit For
        End If
    Next j
    For i = .Row To UBound(fullcolumn, 1)
        If fullcolumn(i, 1) = "" Then
            i = i - 1
            Exit For
        ElseIf i = UBound(fullcolumn, 1) Then
            Exit For
        End If
    Next i

    'to get the range
    Dim rng As Range
    Set rng = .Parent.Range(.Parent.Cells(j, .Column - 1), Parent.Cells(i, .Column - 1))
    'then do what you want with rng


    'but since you already have the values in an array use that instead.
    'It is quciker to iterate and array than the range.
    For k = j To i
        If Len(fullcolumn(k, 1)) > tmax Then tmax = Len(fullcolumn(k, 1))
    Next k
findlongest = tmax
End With
End Function

【讨论】:

  • Application.Caller.Parent 是什么?
  • 它确保我们在进行计算时查看的是正确的工作表,而不是当时的活动工作表。
  • 哦,谢谢。但是,实际上,我只需要以某种方式获得描述的 Range .. :\
  • 这完全符合您的要求。它在第一列的当前范围内找到最长的字符串。
  • @Greenberet 请参阅编辑以了解如何从代码中获取范围。我不推荐它,因为迭代数组比迭代范围要快得多。
【解决方案2】:

你是否在追求类似下面的代码:

Option Explicit

Sub GetLeftRange()

Dim myRng As Range

Set myRng = ActiveCell.Offset(, -1).CurrentRegion

Debug.Print myRng.Address

End Sub

注意ActiveCell 是您标记为绿色的单元格之一。

【讨论】:

  • 是的,但据我所知 CurrentRegion 在各个方向都有效,因此这将导致一个包含绿色单元格的范围。我只想将带框的单元格作为一个范围。
  • @Greenberet 你在绿色单元格中有值或文本吗?你想让它在左侧返回一列吗?#
  • 所有绿色单元格中的 UDF 本身都会产生值。我想要一个代表框架单元格的 Range。
【解决方案3】:

这是使用区域设置每个范围的示例。

Sub test()
    Dim Ws As Worksheet
    Dim rngDB As Range
    Dim rngA As Range, rng As Range

    Set Ws = ActiveSheet
    With Ws
        Set rngDB = .Range("a1", .Range("a" & Rows.Count).End(xlUp))

        Set rngA = rngDB.SpecialCells(xlCellTypeConstants, xlTextValues)
        For Each rng In rngA.Areas
            rng.Offset(, 1).Select '<~~ select is not required but is intended to be visualized
        Next rng
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2012-03-18
    • 2011-05-14
    相关资源
    最近更新 更多