VBA分割单词代码尝试

这篇博客介绍了如何使用VBA在Excel中创建一个宏,将文本框中的输入字符串通过查找空格并替换为'--',实现单词分割。用户在输入文本框中输入内容,程序会将内容在输出文本框中显示,空格被标识为'--',以展示单词间的分隔。
摘要由CSDN通过智能技术生成
 

在EXCEL中添加2个文本框的控件,一个为输入,另一个输出 。中间的空格替换成 "--",表示识别成功。


Sub Split()

    Dim str_input As String
    str_input = Worksheets("sheet1").TextBox_Input.Text
   
    Call split_word(str_input)
   
    Worksheets("sheet1").TextBox_output.Text = str_input

End Sub


Sub split_word(ByRef str_input As String)
   
    Dim str_output As String
    Dim one_word As String
    Dim space_pos As Integer
   
    space_pos = 0
    str_input = Trim(str_input)
    While (Len(str_input) > 0)
        space_pos = InStr(str_input, " ")
       
        If space_pos = 0 Then
            str_output = str_output & " -- " & str_input
            str_input = ""
        Else
            one_word = Mid(str_input, 1, space_pos - 1)
            str_output = str_output & " -- " & one_word
            str_input = Trim(Mid(str_input, space_pos))
        End If
    Wend
   
    str_input = str_output
End Sub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值