[PYTHON] Efficient programming by making string concatenation convenient with VBA

background

--There are "&" and "CONCATENATE ()" in the method of string concatenation in Excel. --However, both methods require you to specify each cell individually. - =A1&A2&A3 - =CONCATENATE(A1,A2,A3) ――I can't select a range even though I have a function (what is the function really for?) --There is a site that describes VBA that makes this range selectable. ――In addition to simple string concatenation, in my case, I thought that it would be more convenient to be able to concatenate strings in the following cases, so I created a function with reference to the above site. --I've been using functions for over 5 years now, but it's very convenient and I use them quite often.

No language Purpose String concatenation method
1 SQL Mass selection of columns Delimiter "comma"
2 SQL Mass generation of CASE statements Delimiter "space"
3 Markdown Create table Delimiter "pipe"
4 R, Python Define header Delimiter "comma" quote "single quote"

Function code

How to create a function (How to register in Excel)

--There are many articles on the net, so please check them yourself.

Example of use

Delimiter "comma"

Excel

A B C
1 dt
2 product
3 price

function

=ConcatenateRangeText_comma(B1:B3)

result

dt,product,price

Delimiter "space"

Excel

A B C D E F
1 ,SUM(CASE WHEN product = 'car' THEN 1 ELSE 0 END) AS product_car
2 ,SUM(CASE WHEN product = 'bike' THEN 1 ELSE 0 END) AS product_bike

function

=ConcatenateRangeText_space(B1:E1)
=ConcatenateRangeText_space(B2:E2)

result

,SUM(CASE WHEN product = 'car' THEN 1 ELSE 0 END) AS product_car
,SUM(CASE WHEN product = 'bike' THEN 1 ELSE 0 END) AS product_bike

Delimiter "pipe"

Excel

A B C D
1 2 SQL
2 3 Markdown
3 4 R,Python

function

=ConcatenateRangeText_separator(A1:D1,"|")
=ConcatenateRangeText_separator(A2:D2,"|")
=ConcatenateRangeText_separator(A3:D3,"|")

result

|2|SQL|
|3|Markdown|
|4|R,Python|

Delimiter "comma" quote "single quote"

Excel

A B C
1 dt
2 product
3 price

function

=ConcatenateRangeText_singlequote_comma(B1:B3)

result

'dt','product','price'

Other

――We have some code similar to this.

reference

Recommended Posts

Efficient programming by making string concatenation convenient with VBA
Making sound by programming Part 2