简体   繁体   English

在 VBA 中查找包含特定范围内的值的最后一列

[英]Find the Last Column which contains values in a Specific Range in VBA

I have 3 Normal Data Ranges from A3:O43, Q3:AE43, AG3:AU43, and all these cells are filled with formulas.我有来自 A3:O43、Q3:AE43、AG3:AU43 的 3 个正常数据范围,所有这些单元格都填充了公式。

Now I would like to identify the last column of each range based on Values using FIND or any other way.现在我想使用 FIND 或任何其他方式根据值识别每个范围的最后一列。 I have tried using the below code, but I'm not able to get the column numbers properly.我尝试使用下面的代码,但无法正确获取列号。

With UBMonthlyYTDSht
    Set Rowss = .Columns("B:B").Find("*", After:=.Range("B1"), searchdirection:=xlPrevious, LookIn:=xlValues)
    If Not Rowss Is Nothing Then Lrows = Rowss.Row
End With

Lcols = UBMonthlyYTDSht.Cells(1, Columns.Count).End(xlToLeft).Column

With UBMonthlyYTDSht
    Set Colss = .Rows(Lrows).Find("*", After:=.Cells(Lrows, Lcols), searchdirection:=xlPrevious, LookIn:=xlValues)
    If Not Colss Is Nothing Then Lcols = Colss.Column
End With

Appreciate your help!!感谢你的帮助!!

Reference Top Left Non-Blank Range (of Range)参考左上角非空白范围(范围)

  • An empty cell is also a blank cell but not necessarily the other way around.空单元格也是空白单元格,但不一定相反。
  • A cell containing ="" or ' is blank, but it's not empty (hence the use of the xlValues parameter).包含=""'的单元格是空白的,但它不是空的(因此使用xlValues参数)。

Limitation局限性

  • No merged cells ( Find method specific).没有合并的单元格(特定于Find方法)。
  • No filters ( Find method specific).没有过滤器(特定于Find方法)。
  • No hidden cells (rows, columns) ( xlValues parameter specific).没有隐藏单元格(行、列)(特定xlValues参数)。

The Function Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to the range from the first cell
'               of a range to its last cell with a non-blank row or column.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefTopLeftNonBlank( _
    ByVal rg As Range) _
As Range
    If rg Is Nothing Then Exit Function
    
    Dim Last As Long: Last = rg.Row + rg.Rows.Count - 1 ' worksheet row
    Dim lCell As Range
    Set lCell = rg.Find("*", , xlValues, , xlByRows, xlPrevious)
    If lCell Is Nothing Then Exit Function
    
    With rg.Resize(rg.Rows.Count - Last + lCell.Row)
        Last = .Column + .Columns.Count - 1 ' worksheet column
        Set lCell = .Find("*", , xlValues, , xlByColumns, xlPrevious)
        Set RefTopLeftNonBlank _
            = .Resize(, .Columns.Count - Last + lCell.Column)
    End With
    
End Function

Usage用法

Sub RefTopLeftNonBlankTEST()
    Dim ws As Worksheet: Set ws = ActiveSheet ' be more specific
    Dim rg As Range: Set rg = ws.Range("A3:O43")
    Dim tlrg As Range: Set tlrg = RefTopLeftNonBlank(rg)
    If Not tlrg Is Nothing Then
        Debug.Print rg.Address(0, 0), tlrg.Address(0, 0)
        rg.Interior.Color = xlNone    
        tlrg.Interior.Color = vbYellow
    End If 
End Sub

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

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