英文:
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.
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
'Remove "O"
rgString = Replace(rg, "O", "")
'Choose delimiter
If InStr(rgString, "-") Then
delim = "-"
ElseIf InStr(rgString, ",") Then
delim = ","
Else
delim = ""
End If
'Split range text
arr = Split(rgString, delim)
'Check if each value is within range
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
答案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("-",A1)),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,"O",""),"-"))),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,"O",""),","))))
Or
=LET(DELIM,IF(ISNUMBER(FIND("-",A1)),"-",","),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(A1,"O",""),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("-",PROGRAMTEXT)),"-",","),
MAX(NUMBERVALUE(TEXTSPLIT(SUBSTITUTE(PROGRAMTEXT,"O",""),DELIM)))))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论