首页 > 解决方案 > 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”。我在这个网站上尝试了几个例子,但我没有取得太大的成功,因为我不太清楚如何将它包含在这个代码中,或者确实应该包含它?

因此,例如,单元格 C2 = 300000 单元格 D2 应读取 TLD300000。我不想使用公式,因为我不知道随着时间的推移将使用多少行。有任何想法吗?谢谢保罗

标签: excelvbafor-loopconcatenation

解决方案


添加一行

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

推荐阅读