宏?Private Sub Command1_Click() MsgBox GetStr("点击帖子右上的(管理)链接") End SubFunction GetStr(tmpS As String) As String Dim FirstPos As Integer Dim LastPos As Integer tmpS = Trim(tmpS) If tmpS = "" Or IsNull(tmpS) Then GetStr = "": Exit Function FirstPos = InStr(1, tmpS, "(") LastPos = InStr(1, tmpS, ")") If LastPos <= FirstPos Then GetStr = "": Exit Function GetStr = Mid(tmpS, FirstPos + 1, LastPos - FirstPos - 1) End Function
'下面代码可以放到宏里执行Private Sub Worksheet_SelectionChange(ByVal Target As Range) Sheets(1).Cells(2, 1) = GetStr(Sheets(1).Cells(1, 1)) End SubFunction GetStr(tmpS As String) As String Dim FirstPos As Integer Dim LastPos As Integer tmpS = Trim(tmpS) If tmpS = "" Or IsNull(tmpS) Then GetStr = "": Exit Function FirstPos = InStr(1, tmpS, "(") LastPos = InStr(1, tmpS, ")") If LastPos <= FirstPos Then GetStr = "": Exit Function GetStr = Mid(tmpS, FirstPos + 1, LastPos - FirstPos - 1) End Function
我是这样写的Sub Command1() Dim mycell As Range For Each mycell In Selection Dim tmpS As String Dim str1 As String Dim FirstPos As Long Dim LastPos As Integer
tmpS = mycell.Value tmpS = Trim(tmpS) FirstPos = InStr(1, tmpS, "("): LastPos = InStr(1, tmpS, ")"): If tmpS = "" Or IsNull(tmpS) Then MsgBox "tt": mycell.Value = "" ElseIf LastPos <= FirstPos Then MsgBox "yy": mycell.Value = "" else mycell.Value = "" str1 = Mid(tmpS, FirstPos + 1, LastPos - FirstPos - 1) mycell.Value = str1 End If Next End Sub 因为刚接触VBA,不太会用函数,改的简单点了,见笑了
MsgBox GetStr("点击帖子右上的(管理)链接")
End SubFunction GetStr(tmpS As String) As String
Dim FirstPos As Integer
Dim LastPos As Integer
tmpS = Trim(tmpS)
If tmpS = "" Or IsNull(tmpS) Then GetStr = "": Exit Function
FirstPos = InStr(1, tmpS, "(")
LastPos = InStr(1, tmpS, ")")
If LastPos <= FirstPos Then GetStr = "": Exit Function
GetStr = Mid(tmpS, FirstPos + 1, LastPos - FirstPos - 1)
End Function
Sheets(1).Cells(2, 1) = GetStr(Sheets(1).Cells(1, 1))
End SubFunction GetStr(tmpS As String) As String
Dim FirstPos As Integer
Dim LastPos As Integer
tmpS = Trim(tmpS)
If tmpS = "" Or IsNull(tmpS) Then GetStr = "": Exit Function
FirstPos = InStr(1, tmpS, "(")
LastPos = InStr(1, tmpS, ")")
If LastPos <= FirstPos Then GetStr = "": Exit Function
GetStr = Mid(tmpS, FirstPos + 1, LastPos - FirstPos - 1)
End Function
Dim mycell As Range
For Each mycell In Selection
Dim tmpS As String
Dim str1 As String
Dim FirstPos As Long
Dim LastPos As Integer
tmpS = mycell.Value
tmpS = Trim(tmpS)
FirstPos = InStr(1, tmpS, "("):
LastPos = InStr(1, tmpS, ")"):
If tmpS = "" Or IsNull(tmpS) Then
MsgBox "tt": mycell.Value = ""
ElseIf LastPos <= FirstPos Then
MsgBox "yy": mycell.Value = ""
else
mycell.Value = ""
str1 = Mid(tmpS, FirstPos + 1, LastPos - FirstPos - 1)
mycell.Value = str1
End If
Next
End Sub
因为刚接触VBA,不太会用函数,改的简单点了,见笑了