Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Evaluate and merge specific cells based on format

0

Hello, I have a daily log that is generated and has a different amount of names each day.

What I am trying to accomplish is, merging the empy adjacent white cells.  I don't want to affect the grey cells nor the cells with the "L".  I used the format paint brush to reach my goal but this takes a lot of effort.  The workbook has a log for each day and I would like to learn how to automate this.

Answer
Discuss

Discussion

Hi Mark and welcome to the Forum

I can see two ways to do this (one including VBA, Excel's built in programming language, if you're not familiar with it). Either way, I'd prefer to split the working times (e.g. 11:00AM - 7:00PM) into two cells and likewise for the break. Trust that's okay for you.

One question, is the break time written after the "L" is placed on a row or does the "L" follows the time typed first in the column headed Break? Likewise does the grey shading (outside start and finish time) come from you typing start and finish times?
John_Ru (rep: 6142) Sep 28, '21 at 4:14 am
Hi John, unfortunately I am note sure how it is created.  I do not have access to the code that does the formatting 
MarkSki Sep 28, '21 at 6:56 am
Mark, see my Answer (and try the file)
John_Ru (rep: 6142) Sep 28, '21 at 7:11 am
Your question didn't say how the file originated so I assumed it was done manually. How do you receive the file/data then?
John_Ru (rep: 6142) Sep 28, '21 at 7:13 am
Do you have any knowledge/experience of VBA?
John_Ru (rep: 6142) Sep 28, '21 at 7:17 am
It is generated from data from our schedule program.  The formatting program is not accessible to me.   I have a bit of VBA knowledge, but am very interested in learning more.
MarkSki Sep 28, '21 at 8:59 am
Add to Discussion

Answers

0

Mark

(I've replaced my earlier answer with a VBA solution, given you receive the log file as per your example).

The revised file has a sub called MergeIfNoL in Module1. The code will run on the active worksheet of this file so you can pick a sheet and run it from VB Explorer. Or you might assign a key combination to it.

I've used two nested loops, one going down rows and the other going along columns (but in steps of two, to check pairs of values).

Per pair of cells, I've got an If statement (split over lines just for clarity) which tests if there's any text in them and they don't have the "non-work" shading (defined by the number for the cell's .Interior.Color). It only merges them if all four tests are met. It then moves on through the loops.

The code is commented so you can (hopefully) follow what is happening:

Option Explicit

Sub MergeIfNoL()

Dim StRw As Long, LstRw As Long
Dim StCol As Long, EndCol As Long
Dim Shd As Long, Rw As Long, Col As Long 'declare variable for shading and counters

StRw = 3 'set start row
LstRw = Range("A" & Rows.Count).End(xlUp).Row - 1 'determine last row before totals
StCol = 5 'set start column, E=5
EndCol = 40 'set last column, AN=40
Shd = 13882323 'set colour used for non-work shading

With ThisWorkbook.ActiveSheet

    For Rw = StRw To LstRw 'loop down rows

        For Col = StCol To EndCol - 1 Step 2 ' loop columns in pairs
            With .Cells(Rw, Col) ' test four conditions are met before merge
                If .Value = "" And .Interior.Color <> Shd _
                    And .Offset(0, 1).Value = "" _
                    And .Offset(0, 1).Interior.Color <> Shd Then

                    .Resize(1, 2).Merge 'merge if so
                End If
            End With
        Next Col
    Next Rw

End With

End Sub
Note that I tried the code on the three rightmost sheets in the file and it seemed to work fine (and very quickly!).

Hope this fixes your problem.

Discuss

Discussion

Hello John, first of all, thanks so much for helping me with this...I must be doing something wrong... I copied the code into the VB module and tried running it by selecting the macro, but nothing happen.  Sorry to be such a pain...
Mark
MarkSki Sep 28, '21 at 3:11 pm
So your file work perfectly...  I am only having issues trying to use the macro on my original file
MarkSki Sep 28, '21 at 3:27 pm
Do I need to copy the module in to the file each time I get a new file?
MarkSki Sep 28, '21 at 3:46 pm
Mark. I assumed that you added log sheets to a single file (judging by the dated sheets). Provided that file contains the module/ sub, you can clean up any new log sheets by running the sub. You can copy the module to your original file in VB Explorer.
John_Ru (rep: 6142) Sep 28, '21 at 4:30 pm
Another option is that in Don's tutorial .Make Your Macros Available in All Workbooks in Excel but you'd need to change the line. 
With ThisWorkbook.ActiveSheet
to 
With ActiveSheet
John_Ru (rep: 6142) Sep 28, '21 at 4:59 pm
Wow!  That is awesome...  Thank you so much for your help with this.  This is an amazing site.  I don't think I have ever gotten so much great help from any website before.  I also really appreciate your explinations of the code, I am learning a lot.
MarkSki Sep 28, '21 at 7:55 pm
Mark. Glad it worked for you and thank you for the kind comments. Now you need to follow the rules of the Forum (see link near top of page) and mark my  Answer as Selected - this guides other users to good solutions (and adds to my Reputation- the only reward I get from helping you, apart from thanks) 
John_Ru (rep: 6142) Sep 29, '21 at 1:33 am
Please remember to do it next time we help you...
John_Ru (rep: 6142) Sep 30, '21 at 7:15 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login