【问题标题】:Find previous cell address (to the left) in active row with different value than active cell value在活动行中查找与活动单元格值不同的值的前一个单元格地址(左侧)
【发布时间】:2020-10-02 06:48:06
【问题描述】:

我已经尝试找到一个 VBA 解决方案来查找具有与所选单元格不同的值的前一个单元格(位于同一行)。因此,如果选定的单元格是例如 [N6](如我的图片),那么我的搜索范围应该是 ("A6:N6"),我需要从中找到具有不同单元格值的最后一个单元格(这将是单元格 [ L6] 在我的图片中,因为它是与单元格 [N6] 具有不同值的前一个单元格。搜索应从末尾 (N6,M6,L6...) 向后开始,直到找到第一个匹配项(第一个不同的单元格值) .当找到第一个匹配时然后选择它。我有数百列,所以我的图片只是为了说明原理。我用Private Sub Worksheet_SelectionChange(ByVal Target As Range)执行我的vba代码所以当用户用鼠标选择一个单元格时。我得到了所需的单元格为{=ADDRESS(6;MATCH(2;1/(A6:O6<>"D")))} 但我需要一个 VBA 解决方案来解决我的问题。我当前的 VBA 解决方案将我带到单元格 [I6] 而不是 [L6] 并且我不知道如何编辑我的代码以找到正确的单元格(在我的示例图片中为 [L6])。

Dim rngSel As String, rngStart As String
Dim rngActiveStart As Range
rngSel = ActiveCell.Address(0, 0)
rngStart = Cells(ActiveCell.Row, 1).Address(0, 0)
Set rngActiveStart = Range(rngStart & ":" & rngSel)

Dim c
    For Each c In rngActiveStart.Cells        
        If c <> Target.Value And c.Offset(0, 1) = Target.Value Then
                c.Select
                MsgBox "Previous different cell: " & c.Address(0, 0)
            Exit For
        End If
    Next

【问题讨论】:

  • 您需要选择一个答案,以便主题显示为已回答。

标签: excel vba


【解决方案1】:

使用 selection_Change

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim r As Long, col As Long, x
    Dim v As String
    
    r = Target.Row
    v = Target.Value
    Application.EnableEvents = False

    For x = Target.Column To 1 Step -1

        With Me
            If .Cells(r, x) <> v Then
                .Cells(r, x).Select
                Exit For
            End If
        End With

    Next x

    Application.EnableEvents = True


End Sub

【讨论】:

  • 您需要在.Select 之后添加Exit For,否则这将始终检查直到列A,因此在此示例中的结果将是选择A6 而不是所需的L6,因为A6 是最后一个单元格检查它与我们开始的N6不同。而EnableEventsBoolean 而不是Integer 你应该在VBA 中使用True/False True-1 检查Debug.Print Int(True),如果你在这里使用数字你有一个从Integer 到的不必要的隐式类型转换Boolean。最后x As Long.
  • v 应该是Variant,以防这些单元格中没有字符串但其他值类型(不必要的类型转换可能会导致误报)。或者踢v,直接使用.Cells(r, x) &lt;&gt; Target。 • 最后,如果您想要完美的解决方案,请使用错误处理打开EnableEvents,以防出现任何错误,否则您可能会遇到事件停止的奇怪情况(以防万一这是一个好的做法)。跨度>
  • 我试过了,但它总是给我点击的单元格:Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim r As Long, col As Long, x As Long Dim v As Variant r = Target.Row v = Target.Value Application.EnableEvents = False For x = Target.Column To 1 Step -1 With Me If .Cells(r, x) &lt;&gt; v Then .Cells(r, x).Select Exit For End With Next x Application.EnableEvents = True MsgBox "Previous different cell: " &amp; Selection.Address(0, 0) End Sub
  • @Pᴇʜ-thanks peh,没有考虑不等于目标的其他单元格。
  • 我还以为真/1和假/0是一样的。
【解决方案2】:

您需要一个For i = max To min Step -1 循环来向后/向左循环:

Public Sub MoveLeftUntilChange()
    
    Dim SelRange As Range 'remember the selected range 'N6
    Set SelRange = Selection
    
    Dim iCol As Long
    For iCol = SelRange.Column To 1 Step -1 'move columns leftwards
        With SelRange.Parent.Cells(SelRange.Row, iCol) 'this is the current row/column to test againts the remembered range N6
            If .Value <> SelRange.Value Then 'if change found select and exit
                .Select
                Exit For
            End If
        End With
    Next iCol
    
End Sub

【讨论】:

  • 当我点击一个单元格时,这段代码对我没有任何作用。
  • 该代码只是一个示例,而不是复制粘贴解决方案。需要你的一些努力。您需要将其放入Worksheet_SelectionChange 并将Selection 替换为Target
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 2020-06-07
相关资源
最近更新 更多