如果具有动态范围,则连接 + 计数
Concatenate + Count if with a dynamic range
我需要将其写在第一列 (A2) 的第二行,然后自动填充直到电子表格的末尾。
=CONCATENATE(D1;".";COUNTIF($D:D1, D1))
第一个 "D1" 需要修复,因为第二行的代码必须是“=CONCATENATE(D1;".";COUNTIF($D:D2, D2))
”
数据量很大,Excel不会用公式来处理,所以我需要用VBA来处理。
我想做什么:
我有一个出现不止一次的名字列表。我需要用它到目前为止出现的次数来写这个名字。例如:
Miriam -- Miriam.1
Maria -- Maria.1
Thiago -- Thiago.1
Maria -- Maria.2
Cloe -- Cloe.1
Maria -- Maria.3
有人能帮帮我吗?
假设 A2 = "=CONCATENATE(D2;".";COUNTIF($D$1:D2, D2))"
和 A3 = "=CONCATENATE(D3;".";COUNTIF($D$1:D3, D3))"=11=]
试试这个,几分钟后 100k 运行s
18 分钟后 1048576 仍在 运行ning
如果您迫切需要数据,您可以 运行 分批处理
lastrow = 100000,因为 i =2 到 lastrow
lastrow = 200000,因为 i =100001 到 lastrow
等
我认为如果有足够的时间,它将 运行 一下子全部
我在 64 位 Office 2016 上,附带 4gb 内存。
Sub test()
Dim namerange As Range
Dim countrange As Range
Dim i As Double
Dim lastrow As Double
lastrow = 50000
Set namerange = Sheet1.Range("D1:D" & 1048576)
Set countrange = Sheet1.Range("A1:A" & 1048576)
For i = 2 To lastrow
countrange(i, 1) = namerange(i, 1) & "." & _
Application.WorksheetFunction.CountIf(namerange.Range("A1:A" & i), namerange(i, 1))
Next i
Sheet1.Range("A1").Resize(countrange.Rows.Count, 1).Cells.Value = countrange.Cells.Value
End Sub
在 90 秒内运行完整列表,
依赖于 E 列为空,这将破坏 E 列中的所有数据
Option Explicit
Sub test()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim temprange As Range
Dim i As Double
Dim nameCounter As Integer
Dim lastrow As Double
lastrow = 1048576
nameCounter = 1
Set temprange = Sheet1.Range("A1:D" & lastrow)
For i = 2 To lastrow
temprange(i, 5) = i
Next i
temprange.Range(Cells(1, 1), Cells(lastrow, 5)).Sort Key1:=Cells(1, 4), Order1:=xlAscending, _
Key2:=Cells(1, 5), Order1:=xlAscending, Header:=xlYes
For i = 2 To lastrow
If temprange(i, 4) <> temprange(i - 1, 4) Then
nameCounter = 1
Else
nameCounter = nameCounter + 1
End If
temprange(i, 1) = temprange(i, 4) & "." & nameCounter
Next i
temprange.Range(Cells(1, 1), Cells(lastrow, 5)).Sort Key1:=Cells(1, 5), Order1:=xlAscending, Header:=xlYes
temprange.Columns(5).Clear
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
我需要将其写在第一列 (A2) 的第二行,然后自动填充直到电子表格的末尾。
=CONCATENATE(D1;".";COUNTIF($D:D1, D1))
第一个 "D1" 需要修复,因为第二行的代码必须是“=CONCATENATE(D1;".";COUNTIF($D:D2, D2))
”
数据量很大,Excel不会用公式来处理,所以我需要用VBA来处理。
我想做什么:
我有一个出现不止一次的名字列表。我需要用它到目前为止出现的次数来写这个名字。例如:
Miriam -- Miriam.1
Maria -- Maria.1
Thiago -- Thiago.1
Maria -- Maria.2
Cloe -- Cloe.1
Maria -- Maria.3
有人能帮帮我吗?
假设 A2 = "=CONCATENATE(D2;".";COUNTIF($D$1:D2, D2))" 和 A3 = "=CONCATENATE(D3;".";COUNTIF($D$1:D3, D3))"=11=]
试试这个,几分钟后 100k 运行s 18 分钟后 1048576 仍在 运行ning
如果您迫切需要数据,您可以 运行 分批处理
lastrow = 100000,因为 i =2 到 lastrow
lastrow = 200000,因为 i =100001 到 lastrow
等 我认为如果有足够的时间,它将 运行 一下子全部
我在 64 位 Office 2016 上,附带 4gb 内存。
Sub test()
Dim namerange As Range
Dim countrange As Range
Dim i As Double
Dim lastrow As Double
lastrow = 50000
Set namerange = Sheet1.Range("D1:D" & 1048576)
Set countrange = Sheet1.Range("A1:A" & 1048576)
For i = 2 To lastrow
countrange(i, 1) = namerange(i, 1) & "." & _
Application.WorksheetFunction.CountIf(namerange.Range("A1:A" & i), namerange(i, 1))
Next i
Sheet1.Range("A1").Resize(countrange.Rows.Count, 1).Cells.Value = countrange.Cells.Value
End Sub
在 90 秒内运行完整列表, 依赖于 E 列为空,这将破坏 E 列中的所有数据
Option Explicit
Sub test()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim temprange As Range
Dim i As Double
Dim nameCounter As Integer
Dim lastrow As Double
lastrow = 1048576
nameCounter = 1
Set temprange = Sheet1.Range("A1:D" & lastrow)
For i = 2 To lastrow
temprange(i, 5) = i
Next i
temprange.Range(Cells(1, 1), Cells(lastrow, 5)).Sort Key1:=Cells(1, 4), Order1:=xlAscending, _
Key2:=Cells(1, 5), Order1:=xlAscending, Header:=xlYes
For i = 2 To lastrow
If temprange(i, 4) <> temprange(i - 1, 4) Then
nameCounter = 1
Else
nameCounter = nameCounter + 1
End If
temprange(i, 1) = temprange(i, 4) & "." & nameCounter
Next i
temprange.Range(Cells(1, 1), Cells(lastrow, 5)).Sort Key1:=Cells(1, 5), Order1:=xlAscending, Header:=xlYes
temprange.Columns(5).Clear
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub