【问题标题】:VBA Excel search column for last changing value最后更改值的 VBA Excel 搜索列
【发布时间】:2017-03-23 08:16:34
【问题描述】:

我有一个专栏:U。此列的值从 U10U500

我需要得到的是列的最后一个变化值,如果它没有变化,那么一个文本“False”或其他东西,如果最后一个变化的值是一个空单元格,那么忽略它..

Column U  
11  
11   
5  
11  
11  
21  

例如这里的结果应该是 21。

我尝试过比较两行并使用条件格式,但范围如此之大,为每一行做所有这些有点太多了。

有人知道这样做的好方法吗?

【问题讨论】:

    标签: vba excel compare


    【解决方案1】:

    应该这样做......

    Sub test()
        Dim LastRow As Long, i As Long
    
        With Worksheets("Sheet1") 'your sheet name
            LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row 'find last used row in column U
    
            For i = LastRow To 2 Step -1 'loop from last row to row 2 backwards (row 1 can not be compared with row before)
                If .Cells(i, "U").Value <> .Cells(i - 1, "U").Value Then 'compare row i with row before. If it changes then ...
                    MsgBox "Last row is: " & .Cells(i, "U").Address & vbCrLf & _
                        "Value is: " & .Cells(i, "U").Value
                    Exit For 'stop if last changing row is found
                End If
            Next i
        End With
    End Sub
    

    它从 U 列中最后使用的行循环到第一行,并检查当前行是否与之前的行不同。如果是这样,它就会停止。

    【讨论】:

    • 太棒了,非常感谢!现在我得到了正确的值,但我的下一步失败了。要不要帮忙,也可能是小事
    • 只是忘了提到,在你的情况下,它应该是 For i = LastRow To 11 Step -1 只检查 U10 行
    • 我现在使用=INDEX(QueryResult!$L$10:$L$500,MATCH(QueryResult!D2,QueryResult!$U$10:$U$500,0)) 来搜索我从您的代码中得到的值。问题是 U 列中的值可以多次出现。我还需要在示例中获取最后 21 个 ..
    【解决方案2】:

    我不确定你想要的输出。

    IF(AND(RC[-1]<>R[-1]C[-1],ROW(RC[-1])>500,R[-1]C[-1]<>""),RC[-1],"")
    

    在单元格 V10:V500 中尝试此公式

    【讨论】:

      【解决方案3】:

      试试这个宏。 首先运行 AnalyseBefore 子程序,当您要检查值是否已更改时,运行 AfterAnalyse 子程序。 如果您希望范围是动态的,请使用我评论过的代码并将 iCount 包含在您的范围计算中

      Sub AnalyseBefore()
      Dim iCount
      Range("U10").Select
      iOvalue = Range("U500").Value
      'iCount = Selection.Rows.Count
      Range("Z1").Value = iOvalue    
      End Sub
      
      Sub AnalyseAfter()
      Dim iCount
      Range("U10").Select
      iNValue = Range("U500").Value
      Range("Z2").Value = iNValue
      iOvalue = Range("Z1").Value
      If (iOvalue = iNValue) Then
      Range("U500").Value = "FALSE"
      End If    
      End Sub 
      

      【讨论】:

        猜你喜欢
        • 2015-01-12
        • 2017-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多