EXCEL VBA 字符串比较
想写一个函数,就是比较两个字符串是否相等。字符串相等的条件是仅字符串长度一直且组成字母一致。例如:ABC不等于ABDC也不等于BC,但是ABCD=BCDA=ACBD=BD...
想写一个函数,就是比较两个字符串是否相等。字符串相等的条件是仅字符串长度一直且组成字母一致。例如:ABC不等于ABDC 也不等于BC,但是ABCD=BCDA=ACBD=BDAC……
谢谢 展开
谢谢 展开
3个回答
展开全部
那AAB与BBA是相等还是不等,区分大小吗?
Function 判断(A$, B$)
Set da = CreateObject("scripting.dictionary")
Set Db = CreateObject("scripting.dictionary")
If Len(A) = Len(B) Then
For i = 1 To Len(A)
da(Mid(A, i, 1)) = da(Mid(A, i, 1)) + 1
Next
For i = 1 To Len(B)
Db(Mid(B, i, 1)) = Db(Mid(B, i, 1)) + 1
Next
For i = 1 To Len(A)
If da(Mid(A, i, 1)) = Db(Mid(A, i, 1)) Then kk = kk + 1
Next
If kk = Len(A) Then
判断 = "相等"
Else
判断 = "不相等"
End If
Else
判断 = "不相等"
End If
Set da = Nothing
Set Db = Nothing
End Function
四种代码四种结果,看你要哪一种
展开全部
Sub 比较相同()
m = InputBox("请输入第一个数:")
p = InputBox("请输入第二个数:")
n = p
If Len(m) = Len(n) Then
For i = 1 To Len(m)
For J = 1 To Len(n)
If Mid(m, i, 1) = Mid(n, J, 1) Then
Sum = Sum + 1
n = Left(n, i - 1) & Right(n, Len(n) - 1)
End If
Next
Next
If Sum = Len(m) Then MsgBox m & "=" & p Else MsgBox m & "不等于" & p
Else
MsgBox m & "不等于" & p
End If
End Sub
代码己OK啦
m = InputBox("请输入第一个数:")
p = InputBox("请输入第二个数:")
n = p
If Len(m) = Len(n) Then
For i = 1 To Len(m)
For J = 1 To Len(n)
If Mid(m, i, 1) = Mid(n, J, 1) Then
Sum = Sum + 1
n = Left(n, i - 1) & Right(n, Len(n) - 1)
End If
Next
Next
If Sum = Len(m) Then MsgBox m & "=" & p Else MsgBox m & "不等于" & p
Else
MsgBox m & "不等于" & p
End If
End Sub
代码己OK啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以下代码VBA中建立新模块,复制进去
bijiao函数中x和y分别表示你要比较的两个单元格中的字符串
Function bijiao(x As Range, y As Range)
a = Len(x.Value)
b = Len(y.Value)
c = 1
If a = b Then
For i = 1 To a
For d = c To b
If Left(x.Value, i) = Left(y.Value, d) Then
c = c + 1
GoTo x
Else
bijiao = "不相同"
Exit Function
End If
Next d
x: Next i
bijiao = "相同"
Else
bijiao = "不相同"
End If
End Function
bijiao函数中x和y分别表示你要比较的两个单元格中的字符串
Function bijiao(x As Range, y As Range)
a = Len(x.Value)
b = Len(y.Value)
c = 1
If a = b Then
For i = 1 To a
For d = c To b
If Left(x.Value, i) = Left(y.Value, d) Then
c = c + 1
GoTo x
Else
bijiao = "不相同"
Exit Function
End If
Next d
x: Next i
bijiao = "相同"
Else
bijiao = "不相同"
End If
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询