如何在两列中连接数据,但在新列中只有一部分以粗体显示?

时间:2015-11-03 11:42:47

标签: excel vba excel-vba

我已经达到了下面的VBA代码但是我需要循环它以便继续为每一行进行,直到A列中没有值:

Sub BoldText()

Dim Part1Len, Part2Len, DividerLen As Integer
Dim Divider As String

Part1Len = Len(Range("B4"))
Part2Len = Len(Range("C4"))
Divider = " :"
DividerLen = Len(Divider)
Range("E4") = Range("B4") & Range("C4")

With Range("E4").Characters(Start:=1, Length:=Part1Len).Font
    .FontStyle = "Bold"
End With

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用For Each循环遍历您要评估的范围。

然后,您可以为循环的每一行使用行索引。

Sub BoldText()
    Dim Part1Len, Part2Len, DividerLen As Integer
    Dim Divider As String
    Dim c As Range, cRow As Long

    For Each c In Range("E4:E" & lrow(1))
        cRow = c.Row
        Part1Len = Len(Range("B" & cRow))
        Part2Len = Len(Range("C" & cRow))
        Divider = " :"
        DividerLen = Len(Divider)
        c.Value = Range("B" & cRow) & Range("C" & cRow)

        With c.Characters(Start:=1, Length:=Part1Len).Font
            .FontStyle = "Bold"
        End With
    Next c
End Sub

修改

我意识到你希望范围是动态的,我已经更新了上面的代码,你只需要包含这个功能:

Function lrow(ByVal colNum As Long) As Long
    lrow = Cells(Rows.Count, colNum).End(xlUp).Row
End Function

代码现在将循环,直到找到Column A中的最后一行。