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

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

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

  1. Option Explicit
  2. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  3. If Target.Column <> 9 Then Exit Sub
  4. Dim foundCell As Range: Set foundCell = Cells(Target.Row, "I").End(xlToLeft)
  5. Target.Offset(, 1).Value = foundCell.Value
  6. Target.Offset(, 2).Value = Cells(1, foundCell.Column).Value
  7. End Sub
  8. Public Sub LoopCells()
  9. Dim c As Range
  10. Dim rngSearch As Range
  11. Dim rngFind As Range
  12. Set rngFind = ActiveSheet.Range("A1:K15")
  13. For Each c In rngFind
  14. If c.Value = "Forecast" Then
  15. MsgBox "FindMe found at " & c.Address
  16. Set rngSearch = ActiveSheet.Range("A1:" & c.Address)
  17. 'Set LastCell = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
  18. ' MsgBox LastCell
  19. Dim d As Range
  20. For Each d In rngSearch 'Range("strFirstAddress:A1").Cells
  21. If d.Value > 0 Then
  22. MsgBox foundCell
  23. Exit For
  24. End If
  25. Next
  26. MsgBox foundCell
  27. MsgBox "The first value greater than zero is in cell " & firstAddress & _
  28. "; - it has value " & firstValue
  29. End If
  30. Next c
  31. End Sub

zfciruhq

zfciruhq1#

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

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

测试信息:

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

相关问题