VBA to merge cells

forest1959

New Member
Joined
Mar 16, 2010
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Searched the web for an answer to this but finding it hard to put into words what i need so i will do my best to explain.

If i have the number "2" in cell "A1", I want some VB that will merge cells B1 and C1 (2 columns)
then
If I have the number "4" in cell "A2", I want some VB that will merge cells B2, C2, D2 and E2 (4 columns)

and so on till the loop comes across an empty cell in column A

There will be no physical data in any other row other than "A"

In summary, I would like some VBA to merge the amount of cells from column B onward's depending on the number inputted in column A

I hope this explains enough

Regards
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
depends on what you consider a 'merge', merging cells loses the data in all other cells but top left
 
Upvote 0
There would be no data to lose, the cells i need merging would all be blank
 
Upvote 0
this will do the trick:
Code:
Sub Macro1()
    Dim MergeCells As Range
    For Each c In [A2:A10]
        Set MergeCells = c.Offset(0, 1)
        Range(MergeCells, MergeCells.Offset(0, c.Value-1)).Merge
    Next c
End Sub
but i am curious . .why?
 
Last edited:
Upvote 0
It certainly will, thank you for your patience

as for why...

its a capacity tracker, each task is given an allocated time (days)

There will be a list of tasks (100+) like in the picture below

https://drive.google.com/open?id=1WLaMU1akAPEGfYQHdXhCKxzzcqrgRonH

and we will drag and drop these now into an operatives schedule to see where we have any extra capacity

https://drive.google.com/open?id=1Ji9t8FUgpHNFUC3hB8JdF9tlvzLlLf9p

Again, i hope that makes sense.

Thanks again for your help

regards
Paul
 
Upvote 0
cheeky question, but would there be a line i could add to insert a border around the merged cells?
 
Upvote 0
you're welcome
thanks for the background info, that must be a bit of a puzzle . .
Gr. Henk
 
Upvote 0
sorry I missed the extra question
try and record an macro doing that and apply formatting before merging I guess
 
Upvote 0
Cheers, I managed it with conditional formatting
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,185
Members
448,872
Latest member
lcaw

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