测试字符串中的换行符VBA

时间:2016-08-01 02:41:27

标签: excel-vba vba excel

我想在用户输入时更改Excel用户窗体中文本框的填充颜色,或者按Enter键插入换行符。如何在VBA中测试换行的存在?

由于

1 个答案:

答案 0 :(得分:1)

Private Sub TextBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
    If Action = fmActionPaste Then

        TextBox1.BackColor = vbGreen

        On Error Resume Next

        Data.GetFromClipboard

        TextBox1.Text = TextBox1.Text & vbCrLf & Data.GetText

        On Error GoTo 0
    End If
End Sub

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 13 Then

        TextBox1.BackColor = vbGreen

        TextBox1.Text = TextBox1.Text & vbCrLf
    End If
End Sub