首页 > 解决方案 > 合并多个单元格的文本

问题描述

如何组合多个单元格的文本,例如将此 excel 公式转换为 VBA 代码单元格F3应该=Q3&"_"&A3&"_"&D3&"_"&(ROUND(M3/1000,1))&"k"

但我希望它每时每刻都在重复F99= Q99&"_"&A99&"_"&D99&"_"&(ROUND(M99/1000,1))&"k"

使用 VBA 代码:

For Each Cel In Range("A1:A100")   
If Cel.Value <> "" Then Cel.Offset(0, 5).Value = *Excel Formula NEED* "Q3&"_"&A3&"_"&D3&"_"&(ROUND(M3/1000,1))&"k""*

说如果 A 列中的值在同一行 E 列中插入文本

标签: excelvba

解决方案


我还认为一个公式(在空单元格的情况下稍作调整以返回“”)将是最好的。但是,即使您没有回答我的问题并且您想要 VBA 中的解决方案,请测试下一个代码:

Sub testConcatenate()
  Dim sh As Worksheet, cel As Range, lastRow As Long
  Set sh = ActiveSheet 'please use your sheet here
  lastRow = sh.Range("A" & Rows.count).End(xlUp).Row

  For Each cel In Range("A1:A" & lastRow)
    If cel.value <> "" Then
        cel.Offset(0, 5).value = Range("Q" & cel.Row).value & "_" & cel.value & "_"  & _
            Range("D" & cel.Row).value & "_" & Round(Range("M" & cel.Row).value / 1000, 1) & "k"
    End If
  Next
End Sub

您可能会在这里澄清""公式中的含义,“k”的含义以及最后一个字符“*”的用途。我没有用它...


推荐阅读