Merge Cells for Unique Date

slhangen

New Member
Joined
Jun 21, 2012
Messages
41
I would like to be able to automate the following solution using macro or VBA:
I need to assign a supervisor for each unique date. Date may have more than one activity. Was wondering if it is possible to merge supervisor cells when multiple activities occur on same date. Not most elegant solution and I have no idea how to do it except manually, which would take way too much time.

Any help is greatly appreciated.

Capture.JPG
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
try this on a copy of your sheet. Guessing that it is col G that you want to merge. If not, change G. Seems to work with limited testing.
VBA Code:
Sub MergeCells()
Dim i As Integer, n As Integer, LRow As Long

On Error GoTo errHandler
Application.EnableEvents = False
LRow = Cells(Rows.count, "A").End(xlUp).Row
i = 1 'start loop with i as 1

Do Until i >= LRow 'stop when i = count of last empty row in col A
   n = i 'sync n with i
   If IsEmpty(Cells(i + 1, 1)) Then 'check if cell below i is empty. If so, it needs to merge.
      Do Until Not IsEmpty(Cells(n + 1, 1)) 'get last empty cell before next date
         n = n + 1 'n now equals that cell number in A
         If n >= LRow Then 'make sure inner loop can't exceed coun of used cells in A
           Exit Do
           Exit Sub
         End If
      Loop
      Range("G" & i & ":" & "G" & n).Merge 'merge G from i to n
       i = n 'sync i to n because n rows have been merged
   End If
   i = i + 1
Loop

exitHere:
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere
 
Upvote 0
Solution
Come to think of it, the starting position possibly should be something other than the beginning each time? If it never slows down then no issue. Not sure what you'd use for figuring out the starting point though - can't be the last merge because anybody could merge in the column anywhere. Maybe a column not in use yet. Something to think about if the rows get into the tens of thousands.
 
Upvote 0
My post missed the End Sub line - sorry about that. It was just a copy/paste error.
The message pretty much tells you what the problem is. The blue highlight even shows you where to put it.
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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