简体   繁体   English

如何使用Excel VBA将A列的值连接到B列

[英]How to concatenate the Column A values to Column B Using Excel VBA

Hi i tried to concatenate the values of Column A to Column B with text "Color-" But Column A contains the multiple color values. 嗨,我试图用文本“ Color-”将A列的值连接到B列,但是A列包含多个颜色值。 i want Each Color value should concatenate with the Value "Color-" in the Column B. I tried this Formula =CONCATENATE("Color-",A2) But its not Work In my case. 我希望每个颜色值都应与列B中的“颜色-”值连接。我尝试过此公式= CONCATENATE(“颜色-”,A2)但在我的情况下不起作用。 Please see the Image For Your Reference, Can Anybody Help on this. 请参阅图像以供参考,任何人都可以帮忙。 在此处输入图片说明

If you were wanting to use VBA, you can create a function which you can then use within the worksheet. 如果要使用VBA,则可以创建一个函数,然后可以在工作表中使用该函数。

Public Function addString(ByVal list As String, ByVal prefix As String) As String

    Dim i As Long
    Dim splitWords() As String
    Dim joinedWords As String

    splitWords = Split(list, ",")

    For i = LBound(splitWords) To UBound(splitWords)

        If i < UBound(splitWords) Then
            joinedWords = joinedWords & prefix & "-" & splitWords(i) & ","
        Else
            joinedWords = joinedWords & prefix & "-" & splitWords(i)
        End If

    Next i

    addString = joinedWords
End Function

You can then type =addString() in the worksheet. 然后,您可以在工作表中键入=addString() The function will take 2 arguments - the list of colours which are delimited by a comma, and the string which you wish to prefix to each colour in the list. 该函数将使用2个参数-用逗号分隔的颜色列表,以及希望在列表中每种颜色之前添加的字符串。

Example: =addString($A$1,A2) 示例: =addString($A$1,A2)

You can then drag this function down the column. 然后,您可以将该功能向下拖动到列中。 There's probably a way to do this using Array formulas; 可能有一种使用Array公式执行此操作的方法; personally I prefer creating custom functions. 我个人更喜欢创建自定义函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM