VBA to format Cells with Borders based on Column Value ( merged cells)

mtoure

New Member
Joined
Jul 26, 2019
Messages
3
Hello Everyone,
I know this issue has been addressed but none of the solutions really solve my problem; hope someone can help me.
I have a spreadsheet with a lot of data that i was able to manipulate with codes and the last part is to now format with borders and blank cells between to make it easy to read (Below is a copy of the data and also what i am trying to achieve). Columns A to I have merged cells and Column J to P do not. I would like to have solid borders around my rows based on the area of the merged cells in column E but do not want borders around the single cells in Col J to P and also would like to insert a blank row every time there is a change in Col E.
I have tried may codes that did not work and was ready to just settle with just inserting a blank row just when there is a change in Columns E but considering that the col has merged cells, it is not working; it is inserting rows and not taking into account merged areas.
Please see below images of the data as is, what i want and what i was able to achieve with the code below:

Raw Data


Book1
ABCDEFGHIJKLMNOP
1COCompanyResultWorkerEmployee IDGrossDed/TaxNetTotal Paid HoursNameAmountHoursNameAmountNameAmount
2NCNanny and CoryTempAnsadfas Cedwfsd12414300.00100.00200.00173.33Regular Salary200.00156.00Apple38.70SSP1,006.07
3Regular Salary100.0017.33Apple4.30MD235.29
4Orange342.90ST/ AZ394.32
5Eyes1.56
6NCNanny and CoryTempClosonb Jenkin77617500.00200.00300.00173.33Regular Salary500.00173.33Orange281.00SSP550.92
7MD128.84
8VVT1,048.26
9125.0025.00100.00Bonus125.00SSP77.50
10MD18.13
11VVT275.00
12
13NCNanny and CoryTempJohn Doe12515133.0057.0076.00173.33Regular Salary133.00173.3333P1,733.34SSP810.94
14Apple19.00MD189.65
15BUS120.00CT/ NY436.56
16Eyes8.22FAM10.00
17YSD2.60
18
Raw


What I want

Book1
ABCDEFGHIJKLMNOP
1COCompanyResultWorkerEmployee IDGrossDed/TaxNetTotal Paid HoursNameAmountHoursNameAmountNameAmount
2NCNanny and CoryTempAnsadfas Cedwfsd12414300.00100.00200.00173.33Regular Salary200.00156.00Apple38.70SSP1,006.07
3Regular Salary100.0017.33Apple4.30MD235.29
4Orange342.90ST/ AZ394.32
5Eyes1.56
6
7NCNanny and CoryTempClosonb Jenkin77617500.00200.00300.00173.33Regular Salary500.00173.33Orange281.00SSP550.92
8MD128.84
9VVT1,048.26
10125.0025.00100.00Bonus125.00SSP77.50
11MD18.13
12VVT275.00
13
14
15NCNanny and CoryTempJohn Doe12515133.0057.0076.00173.33Regular Salary133.00173.3333P1,733.34SSP810.94
16Apple19.00MD189.65
17BUS120.00CT/ NY436.56
18Eyes8.22FAM10.00
19YSD2.60
20
Raw (3)


What i Get with the code below:
Sub InsertLine()
Dim lrow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For lrow = Cells(Cells.Rows.Count, 5).End(xlUp).Row To 2 Step -1
If Cells(lrow, 5) <> Cells(lrow - 1, 5) Then
Rows(lrow).EntireRow.Insert
Rows(lrow).Interior.ColorIndex = 15
Rows(lrow).BorderAround _
ColorIndex:=1, Weight:=xlThin
End If
Next lrow
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub


Book1
ABCDEFGHIJKLMNOP
1COCompanyResultWorkerEmployee IDGrossDed/TaxNetTotal Paid HoursNameAmountHoursNameAmountNameAmount
2
3NCNanny and CoryTempAnsadfas Cedwfsd12414300.00100.00200.00173.33Regular Salary200.00156.00Apple38.70SSP1,006.07
4
5Regular Salary100.0017.33Apple4.30MD235.29
6Orange342.90ST/ AZ394.32
7Eyes1.56
8
9NCNanny and CoryTempClosonb Jenkin77617500.00200.00300.00173.33Regular Salary500.00173.33Orange281.00SSP550.92
10
11MD128.84
12VVT1,048.26
13125.0025.00100.00Bonus125.00SSP77.50
14MD18.13
15VVT275.00
16
17
18NCNanny and CoryTempJohn Doe12515133.0057.0076.00173.33Regular Salary133.00173.3333P1,733.34SSP810.94
19Apple19.00MD189.65
20BUS120.00CT/ NY436.56
21Eyes8.22FAM10.00
22YSD2.60
23
Raw (2)


Thank you for all your Help in advance
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this

Code:
Sub InsertLine()
  Dim ar As Range
  For Each ar In Range("A3", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants)
    ar.EntireRow.Insert
    ar.Offset(-1).EntireRow.Interior.ColorIndex = 15
    ar.Offset(-1).EntireRow.BorderAround ColorIndex:=1, Weight:=xlThin
  Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,961
Messages
6,175,652
Members
452,664
Latest member
alpserbetli

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