仿**格子,我自己也做一个
代码如下
'====文本插入=========
Public Sub MyInsert(control As Office.IRibbonControl)
Select Case control.Id
Case "Insert_btn1" '"批量插入到开头"
On Error Resume Next
Dim SelectRngs As Excel.Range = xlapp.Selection
If IsNothing(SelectRngs) Then
MsgBox(Prompt:="你没有选择,我退了哦", Title:="哆哆提示")
Return
End If
Dim s As String = xlapp.InputBox(Prompt:="输入要插入的文本", Title:="请输入", Default:="文本", Type:=2)
If s = "False" Then Return
Dim useRng As Excel.Range = xlapp.ActiveSheet.UsedRange
Dim Rngs As Excel.Range = xlapp.Intersect(useRng, SelectRngs)
For Each rng1 As Excel.Range In Rngs
If Not String.IsNullOrEmpty(rng1.Text) Then
Dim tmpS As String = rng1.Text
rng1.Value = s + tmpS
End If
Next
On Error GoTo 0
Case "Insert_btn2" '"批量插入到中间"
On Error Resume Next
Dim SelectRngs As Excel.Range = xlapp.Selection
If IsNothing(SelectRngs) Then
MsgBox(Prompt:="你没有选择,我退了哦", Title:="哆哆提示")
Return
End If
Dim s As String = xlapp.InputBox(Prompt:="输入‘位置、文本’;注意用顿号‘、’隔开" + vbCrLf + "例如:3、文本", Title:="请输入", Default:="3、文本", Type:=2)
If s = "False" Then Return
Dim pattern As String = "[\p{P}\p{S}]+" ' 匹配所有标点和符号
Dim replacement As String = "、" ' 替换为全角顿号
Dim tempArr() As String = Split(Regex.Replace(s, pattern, replacement), "、")
Dim outInt As Integer = 1
If tempArr.Length < 2 OrElse Integer.TryParse(tempArr(0), outInt) = False Then
MsgBox(Prompt:="输入有误,我退了哦", Title:="哆哆提示")
Return
End If
Dim s1 As Integer = Integer.Parse(tempArr(0))
Dim s2 As String = tempArr(1)
Dim useRng As Excel.Range = xlapp.ActiveSheet.UsedRange
Dim Rngs As Excel.Range = xlapp.Intersect(useRng, SelectRngs)
For Each rng1 As Excel.Range In Rngs
If Not String.IsNullOrEmpty(rng1.Text) Then
Dim tmpS As String = rng1.Text
rng1.Value = tmpS.Insert(s1, s2)
End If
Next
On Error GoTo 0
Case "Insert_btn3" '"批量插入到末尾"
On Error Resume Next
Dim SelectRngs As Excel.Range = xlapp.Selection
If IsNothing(SelectRngs) Then
MsgBox(Prompt:="你没有选择,我退了哦", Title:="哆哆提示")
Return
End If
Dim s As String = xlapp.InputBox(Prompt:="输入要插入的文本", Title:="请输入", Default:="文本", Type:=2)
If s = "False" Then Return
Dim useRng As Excel.Range = xlapp.ActiveSheet.UsedRange
Dim Rngs As Excel.Range = xlapp.Intersect(useRng, SelectRngs)
For Each rng1 As Excel.Range In Rngs
If Not String.IsNullOrEmpty(rng1.Text) Then
Dim tmpS As String = rng1.Value.ToString()
rng1.Value = tmpS + s
End If
Next
On Error GoTo 0
Case Else
End Select
End Sub