gpt4 book ai didi

excel - 选择具有相似值的单元格,然后按时间顺序重命名文本

转载 作者:行者123 更新时间:2023-12-04 21:29:08 24 4
gpt4 key购买 nike

enter image description here

我想自动重命名具有相同值的单元格。例如,在我选择的范围内,有 3 个单元格带有“stackoverflow”。所需的输出是选择这些单元格,然后将文本更改为类似这样的内容

堆栈溢出_1
堆栈溢出_2
stackoverflor_3

我已经完成了重命名部分,但我遇到的问题是如何在一个范围内选择类似的单元格。这是我到目前为止所拥有的。希望我可以为此创建一个 for 循环或执行 while 循环。

Sub rename()
num = WorksheetFunction.CountA(Selection)

Do While num <> 0

With Selection
.Cells(num, 1).Value = Selection.Cells(num, 1) & "_" & num
End With
num = num - 1
Loop
End Sub

最佳答案

这是一种方法:

Dim dict As Object, c As Range
Set dict = CreateObject("scripting.dictionary")
For Each c in Selection.Cells
v = c.Value
If Len(v) > 0 Then
'Is count > 1 or was it >1 at some point?
If dict.Exists(v) Or Application.CountIf(Selection, v) > 1 Then
dict(v) = dict(v) + 1
c.Value = v & "_" & dict(v)
End If
End If
Next c

关于excel - 选择具有相似值的单元格,然后按时间顺序重命名文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59368645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com