簡體   English   中英

Excel VBA連接帶有循環的單元格

[英]Excel VBA Concatenate cell with loop

我的下面代碼從雙擊上方的單元格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。 我不想使用公式,因為我不知道隨着時間的推移會使用多少行。 有任何想法嗎? 謝謝保羅

加一行

        ….
        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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM