Build a Comma Delimited String

后端 未结 3 1503
迷失自我
迷失自我 2020-12-01 19:27

I want to build a comma delimited string from Range A1:A400.

What is the best way of doing this? Should I use a For loop?

3条回答
  •  无人及你
    2020-12-01 20:05

    The laziest way is

    s = join(Application.WorksheetFunction.Transpose([a1:a400]), ",")
    

    This works because .Value property of a multicell range returns a 2D array, and Join expects 1D array, and Transpose is trying to be too helpful, so when it detects a 2D array with just one column, it converts it to a 1D array.

    In production it is advised to use at least a little bit less lazy option,

    s = join(Application.WorksheetFunction.Transpose(Worksheets(someIndex).Range("A1:A400").Value), ",")
    

    otherwise the active sheet will always be used.

提交回复
热议问题