繁体   English   中英

VBA instr在左功能中使用的功能?

[英]VBA instr Function used in Left function?

我有的:

A列具有此数据24 / 25,25 / 26,最大为100/101,B列具有此数据24,25,26,最大为101。使用Left函数,我正在检查Left(24/25, 2)在B列...到达100/101时,我的回报是10。

我想要的是:

Left(100 / 101,2)然后它返回了100我该怎么办,我知道如果我们给3,那么它将返回,但是当我们给三个,则24 /也会来。 给我任何建议。

工作表公式版本:

您要在单元格中查找“ /”的位置,然后删除1。这将为Left提供“ /”之前的字符数。 因此,如果数据在A2中,则将以下内容放在B2中:

=IFERROR(LEFT(A2,FIND("/",A2)-1),"")

VBA版本(用户定义的函数)可能类似于:

正如您提到的使用Instr

Option Explicit
Public Sub TEST()

 Debug.Print GetLeft([A2].Text)
End Sub

Public Function GetLeft(ByVal aString As String) As Variant
    If InStr(aString, "/") > 0 Then
        GetLeft = Left$(aString, InStr(aString, "/") - 1)
    Else
        GetLeft = CVErr(xlErrNA)
    End If
End Function

您可以使用分割功能

Dim str As String
str = "24/25,25/26"

Dim arr As Variant
arr = Split(str, "/")

结果是

'arr(0) is "24"
'arr(1) is "25,25"
'arr(2) is "26"

使用Like

If Range("A4").Value2 & "/" Like "*" & range("B4").Value2 & "/*" Then

我在每边的右侧添加了一个“ /”,以避免10匹配100或101。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM