有没有一个函数或VBA可以读取单个单元格内的多个字符串?

huangapple go评论59阅读模式
英文:

Is there a function or VBA that can read multiple strings within a single cell?

问题

I have a table that has production numbers for a machine I run. They all start with the letter O and there are 4 different production types, e.g., O0000, O1000, O2000, and O3000. I have a table to the side that tells me what the next production number is for each type.

Sometimes, I need a couple production numbers for one project. I've used simple MID and RIGHT functions to split the production number into two columns and a MAXIFS function to determine the largest production number per type to determine what my next production number is.

The only problem is that I am planning to have 2+ production numbers in future projects. So, is there a function (or VBA code) that can read each 5 character string beginning with O in a cell and accurately determine the largest value without needing to create new columns? I prefer to see all the production numbers for the project in one consistent column for readability.

The last row is an example of a project with 3 production numbers. Ideally, the next Lathe production number should read O0124.

英文:

I have a table that has production numbers for a machine I run. They all start with the letter O and there are 4 different production types, e.g., O0000, O1000, O2000, and O3000. I have a table to the side that tells me what the next production number is for each type.

Sometimes, I need a couple production numbers for one project. I've used simple MID and RIGHT functions to split the production number into two columns and a MAXIFS function to determine the largest production number per type to determine what my next production number is.

有没有一个函数或VBA可以读取单个单元格内的多个字符串?

The only problem is that I am planning to have 2+ production numbers in future projects. So, is there a function (or VBA code) that can read each 5 character string beginning with O in a cell and accurately determine the largest value without needing to create new columns ? I prefer to see all the production numbers for the project in one consistent column for readability.

The last row is an example of a project with 3 production numbers. Ideally, the next Lathe production number should read O0124.

If you have any questions, let me know and I'll try my best to answer.

答案1

得分: 1

以下是代码的翻译部分:

无法找到使用工作表公式使其工作的方法,也许其他人可以修改我的另一个答案。以下是使用自定义函数的解决方案。

该函数名为 `LARGEPROG`,接受三个参数:包含您想要搜索的文本的范围,您想要找到的最小值和您想要找到的最大值。

如果您想要获取 O2000 的值,您可以使用 `=LARGEPROG(A1:A5,2000,3000)`

    Function LARGEPROG(prog_text As Range, minimum_value As Long, maximum_value As Long) As Long
    
    Dim rg As Range
    Dim rgString As String
    Dim MaxValue As Long
    Dim vValue As Variant
    
    For Each rg In prog_text
        
        '移除 "O"
        rgString = Replace(rg, "O", "")
        
        '选择分隔符
        If InStr(rgString, "-") Then
            delim = "-"
        ElseIf InStr(rgString, ",") Then
            delim = ","
        Else
            delim = ""
        End If
        
        '拆分范围文本
        arr = Split(rgString, delim)
        
        '检查每个值是否在范围内
        For Each vValue In arr
            If vValue >= minimum_value And vValue < maximum_value And vValue > MaxValue Then MaxValue = vValue
        Next vValue
    
    Next rg
    
    LARGEPROG = MaxValue
    
    End Function
英文:

Couldn't find a way to make it work with worksheet formulas, maybe someone else can revise my other answer. Here is a solution with a custom function.

The function is named LARGEPROG and takes 3 arguments: the range containing the text you want to search, the minimum value you want to find, and the maximum value you want to find.

If you want the O2000's you would use =LARGEPROG(A1:A5,2000,3000)

Function LARGEPROG(prog_text As Range, minimum_value As Long, maximum_value As Long) As Long

Dim rg As Range
Dim rgString As String
Dim MaxValue As Long
Dim vValue As Variant

For Each rg In prog_text
    
    &#39;Remove &quot;O&quot;
    rgString = Replace(rg, &quot;O&quot;, &quot;&quot;)
    
    &#39;Choose delimiter
    If InStr(rgString, &quot;-&quot;) Then
        delim = &quot;-&quot;
    ElseIf InStr(rgString, &quot;,&quot;) Then
        delim = &quot;,&quot;
    Else
        delim = &quot;&quot;
    End If
    
    &#39;Split range text
    arr = Split(rgString, delim)
    
    &#39;Check if each value is within range
    For Each vValue In arr
        If vValue &gt;= minimum_value And vValue &lt; maximum_value And vValue &gt; MaxValue Then MaxValue = vValue
    Next vValue

Next rg

LARGEPROG = MaxValue

End Function

答案2

得分: 0

SUBSTITUTE会将字母 "O" 替换掉。

TEXTSPLIT会按照 "-" 或 "," 分割字符串。

NUMBERVALUE会将文本转化为数字。

MAX会选择最高的数字。

然后您需要两个不同的公式,取决于您的文本是由 "-" 还是 "," 分隔的。或者使用 LET 来确定要使用哪个分隔符:

=IF(ISNUMBER(FIND("-",A1)),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,"O",""),"-"))),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,"O",""),","))))

或者

=LET(DELIM,IF(ISNUMBER(FIND("-",A1)),"-"," ,"),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,"O",""),DELIM))))

编辑:
如果您需要在单个单元格中从范围中获取最大值,这将会更复杂:

=MAX(BYROW(A1:A2,LAMBDA(PROGRAMTEXT,LET(DELIM,IF(ISNUMBER(FIND("-",PROGRAMTEXT)),"-"," ,"),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(PROGRAMTEXT,"O",""),DELIM)))))))
英文:

SUBSTITUTE will get rid of the letter "O".

TEXTSPLIT will split the string by the "-" or ",".

NUMBERVALUE will turn the text into a number.

MAX will pick the highest number.

Then you need 2 different formulas, depending on whether your text is separated by "-" or ",". Or use LET to determine which delimiter to use:

=IF(ISNUMBER(FIND(&quot;-&quot;,A1)),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,&quot;O&quot;,&quot;&quot;),&quot;-&quot;))),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,&quot;O&quot;,&quot;&quot;),&quot;,&quot;))))

Or

=LET(DELIM,IF(ISNUMBER(FIND(&quot;-&quot;,A1)),&quot;-&quot;,&quot;,&quot;),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,&quot;O&quot;,&quot;&quot;),DELIM))))

Edit:
More complicated if you need the maximum value from the range in a single cell but this should do it:

=MAX(BYROW(A1:A2,LAMBDA(PROGRAMTEXT,LET(DELIM,IF(ISNUMBER(FIND(&quot;-&quot;,PROGRAMTEXT)),&quot;-&quot;,&quot;,&quot;),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(PROGRAMTEXT,&quot;O&quot;,&quot;&quot;),DELIM)))))))

huangapple
  • 本文由 发表于 2023年5月10日 21:20:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定