excel - 在有或没有合并单元格的情况下可靠地获取 Excel 中的最后一列

标签 excel vba

我最近遇到了一个问题,我的 get_lcol函数返回 A1作为 A1:D1 中的单元格被合并了。我调整了我的函数来解决这个问题,但后来我有一些其他数据与单元格合并在 A1:D1 中。但 G 中的另一列我的函数返回 D1所以我再次调整它。问题是我不相信它仍然可以处理所有数据类型,因为它只检查第 1 行中的合并单元格。
看看下面的数据,我怎样才能可靠地让函数返回 D4无论我将合并的行移动到哪里和/或我没有预见到的任何其他问题?
当前功能:

Public Sub Test_LCol()
 Debug.Print Get_lCol(ActiveSheet)
End Sub
Public Function Get_lCol(WS As Worksheet) As Integer
 Dim sEmpty As Boolean
 On Error Resume Next
 sEmpty = IsWorksheetEmpty(Worksheets(WS.Name))
 If sEmpty = False Then
  Get_lCol = WS.Cells.Find(What:="*", after:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
  If IsMerged(Cells(1, Get_lCol)) = True Then
   If Get_lCol < Cells(1, Get_lCol).MergeArea.Columns.Count Then
    Get_lCol = Cells(1, Get_lCol).MergeArea.Columns.Count
   End If
  End If
 Else
  Get_lCol = 1
 End If
End Function
更新:
试试这个带函数的数据:
enter image description here
enter image description here

最佳答案

这是经典的“查找最后一个单元格”问题的一个转折点
陈述目标:

  • 找到包含数据的最右边单元格的列号
  • 考虑超出包含数据的其他单元格的合并单元格区域。如果合并区域的最右侧列超出其他数据,则返回该列。
  • 排除格式化但为空的单元格和合并区域

  • 该方法:
  • 使用 Range.Find 定位最后一个数据单元
  • 如果 Used Range 的最后一列 = Found last data cell column,则返回
  • 否则,从 Used Range 的最后一列循环回到找到的数据单元格列
  • 测试该列 (.Count > 0) 中的数据,如果为 true,则返回
  • 测试该列中的合并单元格 (IsNull(.MergeCells))
  • 如果找到,循环查找合并区域
  • 测试合并区域最左边的单元格中的数据
  • 如果找到返回搜索栏



  • 笔记
  • 这可能仍然容易受到其他“最后数据”问题的影响,例如自动筛选、隐藏行/列等。我还没有测试过这些情况。
  • 具有将合并单元格的搜索限制在最右列的优点

  • Function MyLastCol(ws As Worksheet) As Long
        Dim ur As Range
        Dim lastcell As Range
        Dim col As Long
        Dim urCol As Range
        Dim urCell As Range
        
        Set ur = ws.UsedRange
        
        Set lastcell = ws.Cells.Find("*", ws.Cells(1, 1), xlFormulas, , xlByColumns, xlPrevious)
        
        For col = ur.Columns.Count To lastcell.Column - ur.Column + 2 Step -1
            Set urCol = ur.Columns(col)
            If Application.CountA(urCol) > 0 Then
                MyLastCol = urCol.Column
                Exit Function
            End If
            If IsNull(urCol.MergeCells) Then
                For Each urCell In urCol.Cells
                    If urCell.MergeCells Then
                        If Not IsEmpty(urCell.MergeArea.Cells(1, 1)) Then
                            MyLastCol = urCol.Column
                            Exit Function
                        End If
                    End If
                Next
            End If
        Next
        MyLastCol = lastcell.Column
    End Function
    

    关于excel - 在有或没有合并单元格的情况下可靠地获取 Excel 中的最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70009244/

    相关文章:

    vba - Excel VBA : On Error Goto statement not working inside For-Loop

    excel - 为什么在用户表单中的字段之间切换时 Tab 键有时不起作用?

    c# - ASP.NET MVC - 从 MemoryStream 下载 Excel 文件(损坏的文件)

    excel - 如何从excel中包含任何值(文件路径)的列中添加多个附件

    vba - 为什么这段多行代码中存在VBA语法错误?

    excel - 用户定义函数的自动填充

    html - VBA 中的网页抓取,某些 HTML 信息无法引用它

    Excel 输入和输出到同一单元格

    vba - 如何将 String 变量的值插入到将出现在电子邮件正文中的某些文本中?

    Excel 'If' 公式如何检查值是否小于 x 但大于 y?