Excelmacros连接
在创buildExcelmacros时需要帮助。我有一个Excel工作表。Excel工作表不一致。 我打算制定统一和结构。
例如。
ABCD 1 test tester tester 2 hai test 3 Bye test tested 4 GN test tested Fine ABCD 1 test testertester 2 hai test 3 Bye testtested 4 GN testtestedFine
基本上我必须find放置元素的最后一个单元格,以便我可以编写我的CONCATENATE函数。
在这种情况下,它将是列D,因此我的连接函数将是= CONCATENATE(B1,C1,D1)我想结果是在B1,但不是一个问题,如果我不得不隐藏。
任何人都可以帮助我做到这一点?
你可以使用下面的VBA函数连接(连接)一个任意范围的单元格的值,并带有一个可选的分隔符。
Public Function Join(source As Range, Optional delimiter As String) Dim text As String Dim cell As Range: For Each cell In source.Cells If cell.Value = "" Then GoTo nextCell text = text & cell.Value & delimiter nextCell: Next cell If text <> "" And delimiter <> "" Then text = Mid(text, 1, Len(text) - Len(delimiter)) End If Join = text End Function
有关如何使用该函数的示例,请在电子表格的任意位置input= JOIN(A1:D1)。
= B1 C1&D1&
要么
亚当的function,我已经优化。
Function Join(source As Range, Optional delimiter As String) As String ' ' Join Macro ' Joins (concatenates) the values from an arbitrary range of cells, with an optional delimiter. ' 'optimized for strings ' check len is faster than checking for "" ' string Mid$ is faster than variant Mid ' nested ifs allows for short-circuit ' + is faster than & Dim sResult As String Dim oCell As Range For Each oCell In source.Cells If Len(oCell.Value) > 0 Then sResult = sResult + CStr(oCell.Value) + delimiter End If Next If Len(sResult) > 0 Then If Len(delimiter) > 0 Then sResult = Mid$(sResult, 1, Len(sResult) - Len(delimiter)) End If End If Join = sResult End Function