Excel公式或VBA用于在日期列后查找下一个带通配符值的单元格。

huangapple go评论110阅读模式
英文:

Excel Formula or VBA to find next cell in a row with a wildcard value AFTER date column

问题

我正在尝试创建一个显示学生下一个计划事件的列。我有一些可行的代码,但我需要为每一行单独复制粘贴并在添加或删除学生时进行更新。如果我有一个公式或代码可以覆盖整行而不需要不断更新,那将是巨大的帮助。

这是我们追踪器的简化版本。出于保密原因,我不得不删除一些信息。我希望B4:B6列显示在B3列跟踪的当前日期之后的下一个事件。

Excel公式或VBA用于在日期列后查找下一个带通配符值的单元格。

英文:

I am trying to make a column that shows a student's next planned event. I have some code that works but I would have to copy paste it for every single line offset and update it every time a student was added or removed. If I had a formula or code that would cover the row that wouldn't need constant updating, that'd be a huge help.

Here is a simplified version of our tracker. I had to erase info for confidentiality reasons. I want B4:B6 column to display the next event after the current date tracked by column in B3.

Excel公式或VBA用于在日期列后查找下一个带通配符值的单元格。

答案1

得分: 1

这是公式:

=IFERROR(INDEX(4:4,1,MIN(IF("&"&D4:K4<>"",IF(D$2:K$2>TODAY(),COLUMN(D4:K4),999999),999998))),"")
请将其键入C4并按Ctrl+Shift+Enter,因为这是一个数组公式。
根据需要修改以获取单元格值的正确部分。

英文:

Here is the formula:

  1. =IFERROR(INDEX(4:4,1,MIN(IF(&quot;&quot;&amp;D4:K4&lt;&gt;&quot;&quot;,IF(D$2:K$2&gt;TODAY(),COLUMN(D4:K4),999999),999998))),&quot;&quot;)

Type it in C4 and press Ctrl+Shift+Enter, because is an array formula.
Adapt it get the right part of the cell value if is needed.

答案2

得分: 0

根据您提供的示例数据,此代码可以复制您期望的结果。

  1. Sub demo()
  2. Dim lRowCnt As Long, iColCnt As Long
  3. Dim sDate As Date, iColStart As Long
  4. Dim arr, res()
  5. arr = ActiveSheet.UsedRange.Value
  6. lRowCnt = UBound(arr)
  7. iColCnt = UBound(arr, 2)
  8. sDate = #7/4/2023#
  9. &#39;sDate = Date
  10. If lRowCnt &gt; 3 And iColCnt &gt; 2 Then
  11. iColCnt = UBound(arr, 2)
  12. ReDim res(1 To lRowCnt - 3, 1 To 1)
  13. For j = 3 To iColCnt
  14. If arr(2, j) &gt; sDate Then
  15. iColStart = j
  16. Exit For
  17. End If
  18. Next
  19. If iColStart &gt; 0 Then
  20. For i = 4 To lRowCnt
  21. For j = iColStart To iColCnt
  22. If Len(Trim(arr(i, j))) &gt; 0 Then
  23. res(i - 3, 1) = Trim(Split(arr(i, j), &quot;,&quot;)(1))
  24. End If
  25. Next
  26. Next
  27. End If
  28. End If
  29. [B4].Resize(lRowCnt - 3, 1).Value = res
  30. End Sub

希望这对您有所帮助。

英文:

Based on the sample data you provided, this code can replicate your expected outcome.

  1. Sub demo()
  2. Dim lRowCnt As Long, iColCnt As Long
  3. Dim sDate As Date, iColStart As Long
  4. Dim arr, res()
  5. arr = ActiveSheet.UsedRange.Value
  6. lRowCnt = UBound(arr)
  7. iColCnt = UBound(arr, 2)
  8. sDate = #7/4/2023#
  9. &#39;sDate = Date
  10. If lRowCnt &gt; 3 And iColCnt &gt; 2 Then
  11. iColCnt = UBound(arr, 2)
  12. ReDim res(1 To lRowCnt - 3, 1 To 1)
  13. For j = 3 To iColCnt
  14. If arr(2, j) &gt; sDate Then
  15. iColStart = j
  16. Exit For
  17. End If
  18. Next
  19. If iColStart &gt; 0 Then
  20. For i = 4 To lRowCnt
  21. For j = iColStart To iColCnt
  22. If Len(Trim(arr(i, j))) &gt; 0 Then
  23. res(i - 3, 1) = Trim(Split(arr(i, j), &quot;,&quot;)(1))
  24. End If
  25. Next
  26. Next
  27. End If
  28. End If
  29. [B4].Resize(lRowCnt - 3, 1).Value = res
  30. End Sub

huangapple
  • 本文由 发表于 2023年7月28日 02:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定