我需要通过搜索字符串来查找多行(通常是 156 行)。
示例字符串:'I30112' I 代表 9 月(字母表中的第 9 个字母),30 代表 9 月的第 30 天,112 代表 9 月 30 日的第 112 个单位。
我的用户会说请从 I30112 开始找到下一个 x 单位。这意味着我将搜索 I30112、I30113 等,直到找到 I30156。 I30156 之后的单位是 J01001。所以我需要从I30112到J01111找到。
如何进行循环以从工作表底部进行搜索,以找到对每个单元的最后引用?如果它们都井井有条,我可以找到一个,然后捕获下一个 156,但不幸的是,它们的顺序并不总是正确的。
谢谢!
- -编辑 - -
我正在尝试使用 ASC() 方法。但是,鉴于我的用户输入是一个变量,我很难获得正确的字符。目前我有:
Dim Month As String
Dim MonthChar As Integer
Month = Left(UserForm1.TextBox1.Value, 1)
MonthChar = Asc(Month)
但是,尽管 Month 是一个字符串,但我得到了一个错误。如果我切换到 Monthchar = Asc("Month") 那么它总是从 Month 中获取 M 而不是将其视为变量。
最佳答案
虽然您的问题没有说明一旦找到这些值后如何处理它们,将适当编码字符串的过滤集合收集到变体数组中,然后将它们插入 AutoFilter Method 的标准中。似乎是最方便的过程。
Sub filter_for_encode_string()
Dim str As String, enc As String, rw As Long
Dim dt As Date, num As Long, dy As Long, ndy As Long, mn As String, nmn As String
Dim v As Long, vFLTRs As Variant
enc = "I30112"
dt = DateSerial(Year(Date), Asc(Left(enc, 1)) - 64, Mid(enc, 2, 2))
mn = Chr(Month(dt) + 64)
dy = Day(dt)
num = Val(Right(enc, 3))
ndy = Day(dt + 1)
nmn = Chr(Month(dt + 1) + 64)
With Worksheets("Sheet4")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
ReDim vFLTRs(0)
For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
str = .Cells(rw, 1).Value2
If (Left(str, 1) = mn And Val(Mid(str, 2, 2)) = dy And Val(Right(str, 3)) >= num) Or _
(Left(str, 1) = nmn And Val(Mid(str, 2, 2)) = ndy And Val(Right(str, 3)) < num) Then
vFLTRs(UBound(vFLTRs)) = .Cells(rw, 1).Value2
ReDim Preserve vFLTRs(UBound(vFLTRs) + 1)
End If
Next rw
If UBound(vFLTRs) Then ReDim Preserve vFLTRs(UBound(vFLTRs) - 1)
.Columns(1).AutoFilter Field:=1, Criteria1:=(vFLTRs), _
Operator:=xlFilterValues, VisibleDropDown:=False
With .Resize(.Rows.Count - 1, 1).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
'do something with the filtered range
End If
End With
'.Columns(1).AutoFilter Field:=1
End With
End With
End Sub
当您从一个月或一年的最后一天开始时,要获得第二天的时间会有些麻烦。由于未指定年份,因此使用当前年份来确定 2 月 29 日是否是一个因素。
还有一些事情需要处理。
enc = "I30112"
. 你的叙述提到了“通常是 156”。下面找到任何给定编码月份和日期的最大“单位”代码。
=AGGREGATE(14, 6, RIGHT(A2:INDEX(A:A, MATCH("zzz",A:A )), 3)/(LEFT(A2:INDEX(A:A, MATCH("zzz",A:A )), 3)="I30"), 1)
关于excel - 如何遍历字母和数字序列 (I30112-J01111),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32898462/