楼主 lrlxxqxa |
先看下问题:如何将C4:C13的数据用函数提取并用"/"合并在B2单元格中?
一、函数解法:
方法1:在D4输入- =IF(C5<>"",C4&"/"&D5,C4&D5)
下拉填充
方法2:先用&连接区域列内各个单元格与"/",再利用SUBSTITUTE清除多余的"/";此处利用了REPT和COUNT的配合来统计多余的"/"的个数- =SUBSTITUTE(C4&"/"&C5&"/"&C6&"/"&C7&"/"&C8&"/"&C9&"/"&C10&"/"&C11&"/"&C12&"/"&C13,REPT("/",10-COUNT(C4:C13)),)
方法3:思路与解二相通,细节处理是先利用了空格来连接多个单元格,然后利用TRIM的特性(去除字符串首尾空格,各字符间只留一个空格)来去除首尾空格并保留中间的每一个空格,再把空格替换为需要的字符(此题中为"/")- =SUBSTITUTE(TRIM(C4&" "&C5&" "&C6&" "&C7&" "&C8&" "&C9&" "&C10&" "&C11&" "&C12&" "&C13)," ","/")
方法4:先构建辅助列将源数据列中的数值转化为文本格式,然后利用PHONETIC的文本连接功能(先连接行后连接列,忽略数值)实现要求效果
此处引发的一点思考也与大家分享一下:见附件中的“讨论区”
在构建辅助列的时候,我们可以利用=TEXT(A2,"[>0]0.0;;;")来保留源格式中小数点后第二个0,也可以用=OFFSET($A$1,ROW(A1),)&"/"实现;但二者的结果还是不能被PHONETIC处理,于是想到用=TEXT(OFFSET($A$1,ROW(A1),),"[>0]0.0;;;")&"/"处理,可也不满足PHONETIC的参数要求是REFERENCE的要求;再想到INDIRECT,用=INDIRECT(TEXT({21;31;41;51;61;71;81;91;101;111},"r00c0"),)得到的结果是{100;200;300;400.5;500.6;800.9;0;0;0;0},又成了数值,所以套用PHONETIC函数后得到的是{"";"";"";"";"";"";"";"";"";""};至此搁置。我们再来看看其他解法。
二、VBA解法:
解1:自定义函数- Function joinrng(rng As Range) As String
- Application.Volatile
- Dim rng0 As Range, str1 As String, i As Long, j As Long
- For Each rng0 In rng.Areas
- arr = rng0.Value
- For i = 1 To UBound(arr, 1)
- For j = 1 To UBound(arr, 2)
- If arr(i, j) <> "" Then str1 = str1 & "/" & arr(i, j)
- Next j, i
- Next
- joinrng = Mid(str1, 2)
- End Function
解2:- Function abc(rng As Range) As String
- Dim rng1 As Range
- For Each rng1 In rng
- If Len(rng1) > 0 Then abc = abc & "/" & rng1
- Next
- abc = Mid(abc, 2)
- End Function
解3:
- Sub test()
- Dim rng As Range, str As String
- For Each rng In [C4:C13]
- If Len(rng.Value) <> 0 Then
- str = str & "/" & CStr(rng.Value)
- End If
- Next
- [B2] = Right(str, Len(str) - 1)
- End Sub
解4:- Function test(rng As Range)
- arr = rng.Value
- Dim arrr()
- a = 1
- For Each rngg In arr
- If rngg <> "" Then
- ReDim Preserve arrr(1 To a)
- arrr(a) = rngg
- End If
- a = a + 1
- Next
- test = Join(arrr, "/")
- End Function
三、基础操作实现:
在查找内容中按CTRL+ENTER或者ALT+10(小键盘) 提取并合并.rar |