VBA: Concatenate data with and without colon from respective rows in B and C cell in all excel sheet

uglyphoto

New Member
Joined
Jul 24, 2017
Messages
11
VBA: Concatenate data with and without colon from respective rows in B and C cell in all excel sheet
From D data can be in any cell with and without colon symbol. The empty cells can be anywhere.

Example mock data is below:

Excel 2010
BCDEFGHIJ
Get data with colonGet data without colon
:1#a;1#a;:
0:1 (b:a)14#140:1(b:a)#
5a5a

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

</tbody>
vba3
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Heres a basic one using fixed ranges based on your example:

Code:
Dim arr, arr2

arr = Range("D2:J5")
ReDim arr2(1 To UBound(arr, 1), 1 To 2)

For i = LBound(arr, 1) To UBound(arr, 1)
    For j = LBound(arr, 2) To UBound(arr, 2)
        If InStr(arr(i, j), ":") > 0 Then
            a = a & arr(i, j)
        Else
            b = b & arr(i, j)
        End If
    Next
    arr2(i, 1) = a: arr2(i, 2) = b
    a = vbNullString: b = vbNullString
Next

Range("B2:C5") = arr2
 
Upvote 0
Thank you for your answer.
But I got the wrong result in row 3.
This is mock data actually. So, can I increase the range to anything?
 
Upvote 0
You can change it to whatever you want. Is the 'wrong' result in the cell that is 0:1? If so excel may be seeing that as a number hence the colon doesnt really exist.
 
Upvote 0
Yes, it is 0:1 and its next cell in the same row showing the same problem.
Please help me with the problem.
Thank you again.
 
Upvote 0
I cant as im not seeing the problem. If i copy what you have and paste it into my workbook and run the macro i get your result.
 
Upvote 0
I have added two more symbols i.e, "|" and "~". Can you please modify the range and code.

Excel 2010
CDEFGHIJKLM
Get data with |Get data with ~Get data with colonGet data without colon
|~1:1#a;1#~1a;:|
0:1 (b:a)14#140:1(b:a)#
x|x|1~1~1~1~x|x|
5a5a

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet1
 
Upvote 0
If @ can be added before all output with above code the result data will not change.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top