Excel VBA用循环连接单元格



双击C列最后一个空白单元格时,下面的代码会从它上面的单元格按顺序生成下一个数字。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As 
Boolean)
Dim lastrow As Long
lastrow = Cells(Cells.Rows.Count, "C").End(xlUp).Row + 1
If Not Intersect(Target, Range("C:C")) Is Nothing Then
If Target.Address = Cells(lastrow, "C").Address Then
Application.EnableEvents = False
Cancel = True
Cells(lastrow, "C") = Cells(lastrow - 1, "C") + 1
Application.EnableEvents = True
End If
End If
End Sub

我想做的是为D列中同一行的单元格添加一个简单的连接,在新生成的数字前面加上字母"TLD"。我已经在这个网站上尝试了几个例子,但我没有取得太大的成功,因为我不太知道如何将它包含在这个代码中,或者是否应该包含它?

因此,例如,Cell C2=300000 Cell D2应该读取TLD300000。我不想使用公式,因为我不知道随着时间的推移会使用多少行。有什么想法吗?谢谢Paul

添加一行

….
Cells(lastrow, "C") = Cells(lastrow - 1, "C") + 1
Cells(lastrow, "D") = "TLD" & Cells(lastrow - 1, "C") '<--- added line

尝试以下

Dim c As Range
For Each c In Range("C2:C" & Lastrow)
If c.Value <> "" Then
c.Value = c.Value & "TLD"
End If
Next c

最新更新