Excel按字母/数字将单元格拆分为两个单独的单元格
假设你有两张工作表,在第一张工作表中有很多不同的数据。在D列中,有一个数字(粗体)。数字的第一部分是2或3个字母,然后是后面的两个数字(数字的数量可以变化),例如HTG5342355或PO23455,并非列D中的每个单元格都有这样的数字,它可以在D3中,但也可以在D6、D7、D20中。。。(但始终在D列中) 如何将前2或3个字母作为一个单元格复制到第二个工作表中,将数字作为紧挨着它的另一个单元格复制到第二个工作表中 编辑: 只是想为这个问题添加一些信息: 在D列中,还有其他数据,所以看起来像这样:Excel按字母/数字将单元格拆分为两个单独的单元格,excel,excel-formula,vba,Excel,Excel Formula,Vba,假设你有两张工作表,在第一张工作表中有很多不同的数据。在D列中,有一个数字(粗体)。数字的第一部分是2或3个字母,然后是后面的两个数字(数字的数量可以变化),例如HTG5342355或PO23455,并非列D中的每个单元格都有这样的数字,它可以在D3中,但也可以在D6、D7、D20中。。。(但始终在D列中) 如何将前2或3个字母作为一个单元格复制到第二个工作表中,将数字作为紧挨着它的另一个单元格复制到第二个工作表中 编辑: 只是想为这个问题添加一些信息: 在D列中,还有其他数据,所以看起来像这样
**HTG5342355**
another text
**PO23455**
**BT3452342**
something
something else
**NN23355**
function Split_Letters_And_Digits(input_str as String) as String()
dim re as RegExp
set re = new RegExp
dim ans(2) as String
with re
.global = True
.ignoreCase = True
.multiline = False
.pattern = "([A-Za-z]{2,3})([0-9]*)"
' This pattern will match 2 or 3 upper or lower case letters
' and any number of digits after that.
' Each group is enclosed in parentheses; this will allow you
' to get each group separatedly
End With
' Check if the input string matches the pattern
if re.test(input_str) then
ans(1) = re.replace(input_str, "$1")
ans(2) = re.replace(input_str, "$2")
' Those "$1" and "$2" are special tokens that enable you to get
' the first and second piece of the pattern; that's the reason
' for those parentheses in the pattern
else
' If the input doesn't match the pattern, exit the function
exit function
end if
Split_Letters_And_Digits = ans
end function
只有粗体的数字需要拆分,其他内容与另一张工作表无关您可以编写一个大而难看的复杂公式,但是,尽管这可能有些过分,我还是会使用正则表达式。供进一步参考
VBA支持正则表达式,但您必须在项目中启用它们:
Tools
菜单中,单击References
Microsoft VBScript正则表达式5.5
,并启用复选标记**HTG5342355**
another text
**PO23455**
**BT3452342**
something
something else
**NN23355**
function Split_Letters_And_Digits(input_str as String) as String()
dim re as RegExp
set re = new RegExp
dim ans(2) as String
with re
.global = True
.ignoreCase = True
.multiline = False
.pattern = "([A-Za-z]{2,3})([0-9]*)"
' This pattern will match 2 or 3 upper or lower case letters
' and any number of digits after that.
' Each group is enclosed in parentheses; this will allow you
' to get each group separatedly
End With
' Check if the input string matches the pattern
if re.test(input_str) then
ans(1) = re.replace(input_str, "$1")
ans(2) = re.replace(input_str, "$2")
' Those "$1" and "$2" are special tokens that enable you to get
' the first and second piece of the pattern; that's the reason
' for those parentheses in the pattern
else
' If the input doesn't match the pattern, exit the function
exit function
end if
Split_Letters_And_Digits = ans
end function
这是一个数组函数。要使用它,请选择一个双单元格区域,然后写入拆分字母和数字(D3),然后按Ctrl+Shift+Enter
您可以编写正则表达式来匹配更复杂的模式(使用普通公式可能无法拆分的模式),因此值得学习如何使用它们
希望这对你有帮助
Dim cellvalue as string, textlength as integer, foundnumber as boolean, _
y as integer, x as integer
with thisworkbook.sheet1
do until .cells(y,4).value=vbnullstring
cellvalue=.cells(y,4).value
textlength=len(cellvalue)
if textlength > 1 then
foundnumber=false
for x= 2 to textlength
if foundnumber=false then
if isnumber(mid(cellvalue,x,1)) then
foundnumber=true
thisworkbook.sheet2.cells(y,4).value=left(cellvalue,x-1)
thisworkbook.sheet2.cells(y,5).value=mid(cellvalue,x)
end if
end if
next x
end if
y=y+1
loop
end with
我相信这会起作用,尽管我时间有点紧,还没有测试过。它可能需要更多的测试来确保单元格中的数据是您想要复制的类型,但希望它能帮助您开始。我可以稍后再回来评论。带公式(根据需要抄写):
你的数据在表2中!D1,将以下公式放在要返回零件的位置: 对于开头的字母:
=LEFT(Sheet2!D1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},Sheet2!D1&"0123456789"))-1)
对于末尾的数字:
=MID(Sheet2!D1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},Sheet2!D1&"0123456789")),99)
使用
mid()
函数拆分字符串。为此,我无法使其工作,每当我添加=拆分字母和数字(D3)时,它表示预期的结束函数,我已将结束子函数更改为结束函数,但我仍然无法使其工作:(@JaapHoogberg很抱歉输入错误。在过程结束时,您应该编写end Function
。它应该可以工作。我建议您调试此代码(放置一些断点并逐行执行)为了查看stepHi在那里一步一步地做了什么,函数现在执行,但是在第一个选定的单元格中没有显示任何内容,在第二个单元格中,我得到了前2/3个字母(而不是数字)(如果我取消了文本的绑定,如果它保持粗体,它会变得非常随机),但是还有一个问题,因为我有其他数据(文本)(与新工作表无关)例如,在D3和D7之间,或者在D8和D10之间,我的答案中有代码,我给你的链接和一些研究,我认为你可以让它工作。如果你希望我为你做所有的工作,那么你是在错误的地方。非常感谢你提供的信息:),我刚刚发布了调查结果。正在努力使其生效。如果我取得一些进展,将发布更多:)