excel - 根据另一个单元格值将单元格值分组到范围
解决方案
尝试:
Option Explicit
Sub test()
Dim LastRowA As Long, i As Long, StartPoint As Long, EndPoint As Long, y As Long, LastRowD As Long
Dim arr As Variant
Dim Char1 As String, Char2 As String
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row of Column A
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set the range with data
arr = .Range("A1:A" & LastRowA)
StartPoint = 1
'Loop the range with data
For i = LBound(arr) To UBound(arr)
If StartPoint <= i Then
Char1 = Mid(arr(i, 1), 3, 1)
'Loop to find match
For y = i + 1 To UBound(arr)
Char2 = Mid(arr(y, 1), 3, 1)
If Char1 <> Char2 Then
EndPoint = y - 1
Exit For
End If
Next y
LastRowD = .Cells(.Rows.Count, "D").End(xlUp).Row
.Range("D" & LastRowD + 1).Value = Right(.Range("A" & StartPoint).Value, (Len(.Range("A" & StartPoint).Value) - 1)) & " - " & Right(.Range("A" & EndPoint).Value, (Len(.Range("A" & EndPoint).Value) - 1))
StartPoint = y
End If
Next i
End With
End Sub
结果:
推荐阅读
- java - 在 if 语句中重新调整值
- python - 循环遍历数据帧时替代 Iterrows()
- python - 如何根据 Python 中的组突出显示/更改标签的颜色?
- c# - OData - 限制要在 $filter 中使用的属性
- laravel - 显示特定用户的记录 - Laravel-admin 面板
- php - 使用 simplexml 解析 RSS2 提要并且缺少 media->description 中的逗号。如何让他们回来?
- c++ - 使用“in”关键字传递整数顶点属性的问题
- scala - SBT 多项目构建:针对不同子项目的 2 个不同版本的 sbt play 插件
- python - 当另一个线程将数据添加到字典中时,您可以遍历字典吗?
- python - 我在 heroku 上托管的 discord.py 机器人无法上网