【问题标题】:concatenation nonblank value vba连接非空白值vba
【发布时间】:2016-07-18 18:08:29
【问题描述】:

我只需要为唯一 id 连接 2 列中的行。 Jeeped 帮我写了下面的代码

Option Explicit
Sub qwewreq()
    Dim rw As Long
    With Worksheets("Sheet3")
        For rw = .Cells(.Rows.Count, "A").End(xlUp).Row - 1 To 2 Step -1
            If .Cells(rw, "A").Value2 = .Cells(rw + 1, "A").Value2 Then
                .Cells(rw, "B") = .Cells(rw, "B").Value2 &Chr(10) & .Cells(rw + 1, "B").Value2
                .Cells(rw, "C") = .Cells(rw, "C").Value2 &Chr(10) & .Cells(rw + 1, "C").Value2
                .Rows(rw + 1).EntireRow.Delete
            End If
        Next rw
    End With
End Sub

我尝试在每个非空白值之后添加一个符号。上面的代码在每个单元格后添加符号。是否有可能以某种方式修改此代码,以便只能在非空白之后添加 Chr(10)?

谢谢!这有帮助!

【问题讨论】:

    标签: vba concatenation


    【解决方案1】:

    如果 B 列为空,则不包含换行符。 你可以通过几种方式做到这一点。 一种方法是内联 iif。

    Option Explicit
    Sub qwewreq()
        Dim rw As Long
        With Worksheets("Sheet3")
            For rw = .Cells(.Rows.Count, "A").End(xlUp).Row - 1 To 2 Step -1
                If .Cells(rw, "A").Value2 = .Cells(rw + 1, "A").Value2 Then
                    .Cells(rw, "B") = .Cells(rw, "B").Value2 & iif(len(.Cells(rw, "B").Value2)>0,Chr(10),"") & .Cells(rw + 1, "B").Value2
                    .Cells(rw, "C") = .Cells(rw, "C").Value2 & iif(len(.Cells(rw, "C").Value2)>0,Chr(10),"") & .Cells(rw + 1, "C").Value2
                    .Rows(rw + 1).EntireRow.Delete
                End If
            Next rw
        End With
    End Sub
    

    另一种方式,有点长但更容易阅读:

    Option Explicit
    Sub qwewreq()
        Dim rw As Long
        With Worksheets("Sheet3")
            For rw = .Cells(.Rows.Count, "A").End(xlUp).Row - 1 To 2 Step -1
                If .Cells(rw, "A").Value2 = .Cells(rw + 1, "A").Value2 Then
                    if len(.Cells(rw, "B").Value2) > 0 then
                        .Cells(rw, "B") = .Cells(rw, "B").Value2 &Chr(10) & .Cells(rw + 1, "B").Value2
                    else
                        .Cells(rw, "B") = .Cells(rw + 1, "B").Value2
                    end if
                    if len(.Cells(rw, "C").Value2) > 0 then
                        .Cells(rw, "C") = .Cells(rw, "C").Value2 &Chr(10) & .Cells(rw + 1, "C").Value2
                    else    
                        .Cells(rw, "C") = .Cells(rw + 1, "C").Value2
                    end if
                    .Rows(rw + 1).EntireRow.Delete
                End If
            Next rw
        End With
    End Sub
    

    【讨论】:

      【解决方案2】:

      你的问题不是很清楚。希望我理解它足以回答:

      Option Explicit
      Sub qwewreq()
      Dim rw As Long
      With Worksheets("Sheet1")
          For rw = .Cells(.Rows.Count, "A").End(xlUp).Row - 1 To 2 Step -1
              If .Cells(rw, "A").Value2 = .Cells(rw + 1, "A").Value2 Then
                  If .Cells(rw + 1, "B").Value2 <> "" Then
                  .Cells(rw, "B") = .Cells(rw, "B").Value2 & Chr(10) & .Cells(rw + 1, "B").Value2
                  Else
                  .Cells(rw, "B") = .Cells(rw, "B").Value2
                  End If
                  If .Cells(rw + 1, "C").Value2 <> "" Then
                  .Cells(rw, "C") = .Cells(rw, "C").Value2 & Chr(10) & .Cells(rw + 1, "C").Value2
                  Else
                  .Cells(rw, "C") = .Cells(rw, "C").Value2 & Chr(10)
                  End If
                  .Rows(rw + 1).EntireRow.Delete
              End If
          Next rw
      End With
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 2023-04-09
        • 2022-07-28
        相关资源
        最近更新 更多