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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Not sure what this means:
merge text of the text in a cell.

Merge what text into what cell?

Show us a example of what you have now and then show us what you want it to look out after the merge.
 
Upvote 0
1658292490739.png


it looks like this before and after the results, merge cells b8-b11, b12-b16 in the before column so that it can be automated with VBA macros
Thanks...
Not sure what this means:
merge text of the text in a cell.

Merge what text into what cell?

Show us a example of what you have now and then show us what you want it to look out after the merge.
 
Upvote 0
Try this with a copy of your data

VBA Code:
Sub MergeSports()
  Dim rA As Range
  
  For Each rA In Range("B3:B" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
    With rA.Offset(-1).Resize(rA.Rows.Count + 1)
      .MergeCells = True
      .VerticalAlignment = xlTop
    End With
  Next rA
End Sub
 
Upvote 0
Solution
Try this with a copy of your data

VBA Code:
Sub MergeSports()
  Dim rA As Range
 
  For Each rA In Range("B3:B" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
    With rA.Offset(-1).Resize(rA.Rows.Count + 1)
      .MergeCells = True
      .VerticalAlignment = xlTop
    End With
  Next rA
End Sub
Thank you so much, code successful...
 
Upvote 0
Hello,
Sorry, I need to help, I found problem to merge cells using VBA for other case. if the data is like this why syntax eror.
Thanks....
1658894155338.png


VBA Code:
Sub MergeSports()
  Dim rA As Range
  
  For Each rA In Range("A2:A" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
    With rA.Offset(-1).Resize(rA.Rows.Count + 1)
      .MergeCells = True
      .VerticalAlignment = xlTop
    End With
  Next rA
End Sub
 
Upvote 0
Where is the syntax error?

Also, if you want us to test code with a particular data layout, please give it to us in a form that we can copy: XL2BB
1658895849801.png

as above, the error statement, and the following data
Thank you for help....


Book3
ABCD
1CategoryTimeCountry
2AA05.00Amerika
3 Indonesia
4 Malaysia
5 06.00Singapura
6 Vietnam
7 Cina
8BB05.00Amerika
9 Indonesia
10 Malaysia
11 06.00Singapura
12 Vietnam
13CC04.00Cina
14DD10.00Singapura
15Vietnam
16Cina
17
Sheet1
 
Upvote 0
as above, the error statement
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?

1658896907170.png


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

Forum statistics

Threads
1,224,750
Messages
6,180,740
Members
452,996
Latest member
nelsonsix66

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