Option ExplicitPrivate Sub Command1_Click() Dim strTest As String strTest = "一个多行文本框中有很多内容," & vbCrLf & "现在要在某个位置插入字符串" strTest = InsertStr(strTest, "内容,", "插入的字符") Debug.Print strTest End Sub Private Function InsertStr(strSource As String, strFind As String, strInsert As String) As String Dim i As Long i = InStr(1, strSource, strFind) InsertStr = Left(strSource, i + Len(strFind) - 1) & strInsert & Right(strSource, Len(strSource) - i - Len(strFind) + 1) End Function
with text1 .sellength = 0 .seltext = "插入内容" end with
Private Sub Command1_Click() Dim s1 As String, s2 As String If Text1.SelStart > 0 Then s1 = Left(Text1.Text, Text1.SelStart) s2 = Mid(Text1.Text, Text1.SelStart + 1 + Text1.SelLength) Else s1 = "" s2 = Text1.Text End If Text1.Text = s1 & "插入的字符串" & s2 End Sub
晕,Left函数理解错误 Private Sub Command1_Click() Dim s1 As String, s2 As String s1 = Left(Text1.Text, Text1.SelStart) s2 = Mid(Text1.Text, Text1.SelStart + 1 + Text1.SelLength) Text1.Text = s1 & "插入的字符串" & s2 End Sub
再晕一次,基础超级不扎实 Tiger_Zhao(VB老鸟) 的简单有效 Private Sub Command1_Click() Text1.SelText = "aaa" End Sub
Dim strTest As String
strTest = "一个多行文本框中有很多内容," & vbCrLf & "现在要在某个位置插入字符串"
strTest = InsertStr(strTest, "内容,", "插入的字符")
Debug.Print strTest
End Sub
Private Function InsertStr(strSource As String, strFind As String, strInsert As String) As String
Dim i As Long
i = InStr(1, strSource, strFind)
InsertStr = Left(strSource, i + Len(strFind) - 1) & strInsert & Right(strSource, Len(strSource) - i - Len(strFind) + 1)
End Function
.sellength = 0
.seltext = "插入内容"
end with
Dim s1 As String, s2 As String
If Text1.SelStart > 0 Then
s1 = Left(Text1.Text, Text1.SelStart)
s2 = Mid(Text1.Text, Text1.SelStart + 1 + Text1.SelLength)
Else
s1 = ""
s2 = Text1.Text
End If
Text1.Text = s1 & "插入的字符串" & s2
End Sub
Private Sub Command1_Click()
Dim s1 As String, s2 As String
s1 = Left(Text1.Text, Text1.SelStart)
s2 = Mid(Text1.Text, Text1.SelStart + 1 + Text1.SelLength)
Text1.Text = s1 & "插入的字符串" & s2
End Sub
Tiger_Zhao(VB老鸟) 的简单有效
Private Sub Command1_Click()
Text1.SelText = "aaa"
End Sub