gpt4 book ai didi

vba - 用逗号作为分隔符分割字符串

转载 作者:行者123 更新时间:2023-12-04 20:41:06 24 4
gpt4 key购买 nike

我需要一个文本框,您可以在其中输入姓名,然后将姓名分为名字和姓氏,并将其显示在 2 个文本框中。它必须支持“FIRST LAST”和“LAST, FIRST”形式的条目。

我已经完成了第一个最后一个。

这就是我最后的,第一个:

Private Sub inputText_Change()

End Sub

Private Sub Label1_Click()

End Sub

Private Sub retrieveinput_Click()
FullName = inputText.Text
Dim NameArray() As String

If FullName.Contains(",") Then
NameArray = Split(FullName, ",")
First = NameArray(1)
Last = NameArray(0)
Else
NameArray = Split(FullName)
First = NameArray(0)
Last = NameArray(1)
End If

TextBox2.Text = First
TextBox3.Text = Last

End Sub

这给出了一个错误,上面写着“需要对象”
If FullName.Contains(",") Then

最佳答案

VBA代码:

Option Explicit

Private Sub retrieveinput_Click()
Dim nameArray As Variant, full As String, first As String, last As String

full = inputText.Text

If Len(full) > 0 Then 'if not empty

nameArray = Split(full, ",") 'attempt to split on comma

If UBound(nameArray) < 1 Then 'if comma doesn't exist (1 element in array)

nameArray = Split(full) 'attempt to split by space (" ")

If UBound(nameArray) > 0 Then 'if at least one space exists
first = nameArray(0) 'FIRST_1 FIRST_2 LAST doesn't work
last = nameArray(1) 'multiple last names won't work either
Else
first = full 'one word only
End If
Else 'comma exists so last name is first in array
first = Trim(nameArray(1)) 'remove first space after comma
last = Trim(nameArray(0))
End If
End If
TextBox2.Text = first
TextBox3.Text = last

'Debug.Print "First Name: """ & first & """, Last Name: """ & last & """"

'Input 1: "" -> First Name: "", Last Name: ""
'Input 2: "FIRST LAST" -> First Name: "FIRST", Last Name: "LAST"
'Input 3: "LAST, FIRST" -> First Name: "FIRST", Last Name: "LAST"
'Input 4: "FIRST1 FIRTS2 LAST1 LAST2" -> First Name: "FIRST1", Last Name: "FIRTS2"
'Input 5: "FIRST" -> First Name: "FIRST", Last Name: ""
End Sub

关于vba - 用逗号作为分隔符分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33180900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com