excel 从指定的“预测”列获取所用单元格中的最新值

dzhpxtsq  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(170)

我想读取列“预测”(BM)到列D的最新值,我写了下面的代码,但它没有给我正确的值。例如,我有列AV 21 - 0.5的最新值作为我的结果。

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column <> 9 Then Exit Sub
    Dim foundCell As Range:  Set foundCell = Cells(Target.Row, "I").End(xlToLeft)
    Target.Offset(, 1).Value = foundCell.Value
    Target.Offset(, 2).Value = Cells(1, foundCell.Column).Value
End Sub
Public Sub LoopCells()

Dim c As Range
Dim rngSearch As Range
Dim rngFind As Range
 Set rngFind = ActiveSheet.Range("A1:K15")

For Each c In rngFind
    If c.Value = "Forecast" Then
      MsgBox "FindMe found at " & c.Address
      
      Set rngSearch = ActiveSheet.Range("A1:" & c.Address)
      
      'Set LastCell = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
      
      ' MsgBox LastCell
     Dim d As Range

For Each d In rngSearch 'Range("strFirstAddress:A1").Cells
  If d.Value > 0 Then
    
    MsgBox foundCell
   
    
    Exit For
  End If
Next

MsgBox foundCell
MsgBox "The first value greater than zero is in cell " & firstAddress & _
       "; - it has value " & firstValue
    End If
Next c

End Sub

zfciruhq

zfciruhq1#

下面是一个可能有帮助的快速示例(基于beforeDoubleClick,但应说明如何利用foundCell):

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column <> 9 Then Exit Sub
    Dim foundCell As Range:  Set foundCell = Cells(Target.Row, "I").End(xlToLeft)
    Target.Offset(, 1).Value = foundCell.Value
    Target.Offset(, 2).Value = Cells(1, foundCell.Column).Value
End Sub

测试信息:

请注意,“I”是第9列,我只是想快速检查一下,如果我不小心双击了其他地方,是否不想开始运行。

相关问题