需要找到文本和数字组合的最大值
Need to find max value with text & number combination
我在 excel sheet 的列中有一些数据。 (文本-数字组合)喜欢:
AAA-32
BBB-54
AAA-221
CCC-05
DDD-212
文本的长度始终为 3 个字符,并且始终后跟一个“-”,然后是一个数字。
我需要为 AAA、BBB、CCC ..& 其他具有最大数字的文本找到最大值。
AAA 的最大值为“AAA-221”,其他字符(BBB、CCC..)也同样如此
如何使用 VBA 实现它?
`Data is present in column A
'lRow = Last Row
`tchar contains no. of unique characters, here 4 (AAA,BBB,CCC,DDD)
Dim i as Integer, tchar as Integer, tArray() as String, rng as Range
'Copying unique characters from col A into col Z. (Ignore Cell A1, Z1)
Range("A2:A" & lRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("Z2"), Unique:=True
'To get tchar
Range("Z2").Select
Range(Selection, Selection.End(xlDown)).Select
tchar = Selection.Rows.Count
'Range with unique characters
Set rng = Range("Z2", "Z" & lRow)
'Re-Define array
ReDim tArray(tchar)
'Getting unique characters into array
For i = 1 To tchar
tArray(i) = rng.Item(i).Value
Next i
'Separated Text & Numbers into col B & C
'Col B has text (AAA,BBB...)
'Col C has numbers (32, 54, 221 ..)
'Now to use tArray() to traverse in col B & get max value from col C
'**I am stuck here**
没有 VBA 你可以使用 数组公式:
=MAX(IF(LEFT(A1:A5,3)="AAA",--MID(A1:A5,5,99),""))
所以 VBA:
Sub dural()
Dim v As Variant
v = Evaluate("MAX(IF(LEFT(A1:A5,3)=""AAA"",--MID(A1:A5,5,99),""""))")
MsgBox v
End Sub
对每个前缀重复或循环。
该任务看起来像是使用字典对象的经典案例
Option Explicit
Sub SelectMax()
Dim Key As String, Value As Long, x As Variant, a As Variant, cnt As Long
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
With ThisWorkbook.Worksheets("Sheet1")
For Each x In Intersect(.Columns("A"), .UsedRange)
a = Split(x, "-")
Key = a(0): Value = Val(a(1))
If dict.exists(Key) Then
If Value > dict(Key) Then dict(Key) = Value
Else
dict.Add Key, Value
End If
Next
' output the result
Range("B1") = "Max" 'header
cnt = 1
For Each x In dict.Keys
Range("B1").Offset(cnt) = x & "-" & dict(x)
cnt = cnt + 1
Next
End With
End Sub
我在 excel sheet 的列中有一些数据。 (文本-数字组合)喜欢:
AAA-32
BBB-54
AAA-221
CCC-05
DDD-212
文本的长度始终为 3 个字符,并且始终后跟一个“-”,然后是一个数字。
我需要为 AAA、BBB、CCC ..& 其他具有最大数字的文本找到最大值。
AAA 的最大值为“AAA-221”,其他字符(BBB、CCC..)也同样如此
如何使用 VBA 实现它?
`Data is present in column A
'lRow = Last Row
`tchar contains no. of unique characters, here 4 (AAA,BBB,CCC,DDD)
Dim i as Integer, tchar as Integer, tArray() as String, rng as Range
'Copying unique characters from col A into col Z. (Ignore Cell A1, Z1)
Range("A2:A" & lRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("Z2"), Unique:=True
'To get tchar
Range("Z2").Select
Range(Selection, Selection.End(xlDown)).Select
tchar = Selection.Rows.Count
'Range with unique characters
Set rng = Range("Z2", "Z" & lRow)
'Re-Define array
ReDim tArray(tchar)
'Getting unique characters into array
For i = 1 To tchar
tArray(i) = rng.Item(i).Value
Next i
'Separated Text & Numbers into col B & C
'Col B has text (AAA,BBB...)
'Col C has numbers (32, 54, 221 ..)
'Now to use tArray() to traverse in col B & get max value from col C
'**I am stuck here**
没有 VBA 你可以使用 数组公式:
=MAX(IF(LEFT(A1:A5,3)="AAA",--MID(A1:A5,5,99),""))
所以 VBA:
Sub dural()
Dim v As Variant
v = Evaluate("MAX(IF(LEFT(A1:A5,3)=""AAA"",--MID(A1:A5,5,99),""""))")
MsgBox v
End Sub
对每个前缀重复或循环。
该任务看起来像是使用字典对象的经典案例
Option Explicit
Sub SelectMax()
Dim Key As String, Value As Long, x As Variant, a As Variant, cnt As Long
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
With ThisWorkbook.Worksheets("Sheet1")
For Each x In Intersect(.Columns("A"), .UsedRange)
a = Split(x, "-")
Key = a(0): Value = Val(a(1))
If dict.exists(Key) Then
If Value > dict(Key) Then dict(Key) = Value
Else
dict.Add Key, Value
End If
Next
' output the result
Range("B1") = "Max" 'header
cnt = 1
For Each x In dict.Keys
Range("B1").Offset(cnt) = x & "-" & dict(x)
cnt = cnt + 1
Next
End With
End Sub