英文:
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
- 寻找最后一行。
- 编写一个公式来连接B列和C列。
- 硬编码公式结果。
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
英文:
- Find the last row.
- Write a formula to concatenate columns B and C.
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论