VBA在第一个空白字符中插入vbNewLine

时间:2015-11-18 11:30:38

标签: vba

我需要在50个第一个字符后遇到第一个空白字符后在单元格字符串值中插入vbNewLine吗?

例如: “在拥有合适技能的公司员工中,有很好的晋升机会,那么相关的职位就变得可用”应该成为

“拥有合适技能的公司员工(vbNewLine)  有良好的发展前景,然后有相关的职位“

2 个答案:

答案 0 :(得分:3)

由于您提出的问题非常好,这是一个解决方案

Sub solution()

    Dim test As String
    Dim pos As Integer

    test = "At company employees with the right skills have good prospects to be promoted then a relevant position becomes available"

    pos = InStr(51, test, " ") 'search for a space on or after the 51st character

    If (pos >= 51) Then
        'found a space
        test = Left(test, pos) & vbNewLine & Mid(test, pos + 1) 'miss out that space
    End If

    Debug.Print test


End Sub

答案 1 :(得分:3)

Replace function可以从特定点开始,并使用通常保留为默认值的参数替换字符的单个出现位置。

Dim str As String, i As Long
i = 50
str = "At company employees with the right skills have good prospects to be promoted then a relevant position becomes available"
str = Left(str, i - 1) & Replace(str, Chr(32), Chr(10), _
                                 Start:=i, Count:=1)
Debug.Print str

使用50作为起点,我收到的结果是,

At company employees with the right skills have good
prospects to be promoted then a relevant position becomes available