将单元格A2和B2连接在单元格C2中,然后向下填充整列。

huangapple go评论62阅读模式
英文:

How to concatenate cell A2 & B2 in cell C2 then down the column?

问题

以下是翻译好的部分:

"下面的代码适用于指定的单元格,它将列A和列B中的姓和名合并到列C中。

如何让它在一系列单元格中重复,其中最后一行可能会改变?

以下代码适用于A2和B2。我希望它能在A3和B3等单元格中重复,并且在每次报告生成时,最后一行会发生变化。

Sub ConcatenateStrings()

    Dim StringOne As String
    Dim StringTwo As String
    Dim StringThree As String
    StringOne = Range("A2").Value
    StringTwo = Range("B2").Value
    Range("C2").Value = StringOne & ", " & StringTwo

End Sub

如果需要更多帮助,请告诉我。

英文:

The code below works for specified cells where I combine LastName, FirstName from columns A & B into column C.

How can I get it to repeat for a range of cells where the last row may change?

The following code works for A2 & B2. I want it to repeat for A3 & B3 and so on down the rows with the last row changing as each report comes out.

Sub ConcatenateStrings()

    Dim StringOne As String
    Dim StringTwo As String
    Dim StringThree As String
    StringOne = Range("A2").Value
    StringTwo = Range("B2").Value
    Range("C2").Value = StringOne & ", " & StringTwo

End Sub

答案1

得分: 1

  1. 寻找最后一行。
  2. 编写一个公式来连接B列和C列。
  3. 硬编码公式结果。
Sub Tester()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        
    With ws.Range("C2:C" & lastRow)
        .Formula = "=A2&""""&B2"
        .Value = .Value
    End With
End Sub
英文:
  1. Find the last row.
  2. Write a formula to concatenate columns B and C.
  3. Hard-code the formula results.
Sub Tester()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        
    With ws.Range("C2:C" & lastRow)
        .Formula = "=A2&"", ""&B2"
        .Value = .Value
    End With
End Sub

huangapple
  • 本文由 发表于 2023年2月14日 03:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定