Merge Cells Automatically using VBA

Sefty

Board Regular
Joined
Apr 5, 2022
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hello

I'm looking for something to automatically merge text of the text in a cell. This can either be immediately or by using VBA and running a macro to do all of them at once. Example for data merge data football, and I'm looking to merge by using VBA to data volly, basketball, and tenis.
Thanks....
1658283471276.png
 
Tried Peter's code on the data copied from XL2BB. Blank cells in column A aren't recognized as blank. (I'm not sure if this issue comes from XL2BB, or from his spreadsheet.) After making "those looks blank cells" blank by the Delete key, Peter's code works perfectly.
 
Upvote 0

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.
For syntax errors the code usually turns red and the yellow line with arrow will not progress to the line in question like you have shown.

Instead of a Syntax error, are you sure that you are not getting an error like this instead?

View attachment 70203

From your sample, it appears that many of the "blank" cells are not blank (empty) but contain space characters. Could that be your problem?

yes alright I get error information like in the picture.
Wow that's right, there is a space in an empty cell. Now syntax can be used and it works.
Thank you very much....
 
Upvote 0
I get error information like in the picture.
Always try to give the exact error message as well as the line that it occurs on. :)

Wow that's right, there is a space in an empty cell.
Not sure how they are getting there but the code could be adapted to remove them first if required.

Glad it is resolved anyway. (y)
 
Upvote 0
I would duplicate the values rather than merge personally, you will see what I mean if you ever try to pivot the data.
 
Upvote 0
Hello,
sorry if i have case to merge cells automatically using VBA with the form of data as below, I want to merge data for cells N and the result is like in cells M.
Thanks...
1659335220324.png
 
Upvote 0
I asked two questions & you answered one of them. ;)
 
Upvote 0
I asked two questions & you answered one of them. ;)
Sorry I missed, result coloumn N is simply text, because I don't know if there is a formulas to merge. I want to merge data for cells N and the result is like in cells O with VBA.
 
Upvote 0
Sorry I missed, result coloumn N is simply text,
Thanks.

Please remember XL2BB for sample data. ;)

Try this

VBA Code:
Sub MergeSports_v3()
  Dim rA As Range
  
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace(Replace("if(#=%,"""",#)", "#", .Address), "%", .Offset(-1).Address))
    For Each rA In Range(.Address).SpecialCells(xlBlanks).Areas
      rA.Offset(-1).Resize(rA.Rows.Count + 1).MergeCells = True
    Next rA
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,453
Members
452,514
Latest member
cjkelly15

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