How to add a bottom border along row that has merged cells with VBA formatConditons - excel

I have a sheet where in column B the cells are merged with the row below. In the rest of the columns the rows are not merged.
I want to add a VBA code that draws a line across the bottom of the entire row along the bottom of the merged cells. It's as if I wanted to draw a bottom border every other row for all columns (except B where each merged cell would have the bottom border). I have tried using the following code but the border is not drawn under the merged cells
Sub FormatTest()
With Sheets("Test")
With .Range("$B:$Z")
.FormatConditions.Add xlExpression, Formula1:="=mod(row(),2)=0"
With .FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
.FormatConditions(1).StopIfTrue = False
End With
End With
End Sub
Here is an example of what i want to achieve
I want to achieve this with conditional formatting because the number of rows will change from time to time and i don't want to have borders on empty cells.
The photo is just an example because there are many rows and on different sheets I will have a different number of columns so I just want to apply it to the whole row... Can anyone help?

You can try using something along these lines:
(Just might need to tinker with the row = 1 to get the correct starting position)
Dim row As Long
Dim lastRow As Long
Dim lCol As Long
Dim letter As String
With ThisWorkbook.Worksheets("your sheet name")
' clear previous borders
.Range("A5:ZZ30000").Borders.Linestyle = xlNone
' add new borders
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' checks row 1 for last col
letter = Split(.Cells(1, lCol).Address, "$")(1)
For row = 1 To lastRow+1 Step 2
With .Range("B" & row & letter & row).Borders(xlBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
Next
End With

Related

How to put borders around a range of merged and non-merged cells

I need a macro that puts a border in a range of cells from A to F, based on the formatting in the A column. In the A column, cells can be merged with multiple cells below it, or can be just a single cell. I have written a VBA code to put a border around the non-blank cells in column A, but dont know how to expand it to the other 5 columns (from B to F). See the pictures to better understand what I have written and what I need.
How the Data looks like:Data
What my code does:WhatMyCodeDoes
What I want it to do:WhatIWantItToDo
My code:
Sub Borders()
Dim linestyle As Variant, line As Variant
Range("A1").Select
Do While ActiveCell.Address <> Range("A65536").End(xlUp).Offset(1, 0).Address
If ActiveCell.Value > 0 Then
linestyle = Array(xlDiagonalDown, xlDiagonalUp, xlInsideVertical, xlInsideHorizontal)
line = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
For I = 0 To UBound(linestyle)
Selection.Borders(linestyle(I)).linestyle = xlNone
With Selection.Borders(line(I))
.linestyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Next I
Else
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
You may change only two lines and will be OK.
'Range("A1").Select
Range("A1").Resize(1, 6).Select
' ...
'ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Resize(1, 6).Select

How to check for duplicates, highlight duplicates, and count the highlighted duplicates in a single column?

I want to highlight and count the number of duplicates in a single concatenated column.
I have it as two separate subs right now and there really isn't much more to say, this isn't that hard of a problem I'm confident of that but I have been working on it for days with absolutely no progress. It has to be done in a VBA and it cannot highlight blank cells in the column. The concatenations are done through a formula in the workbook. Please help me, I m dying,
Sub Duplicate_Check()
Dim ws As Worksheet
Set ws = Sheet1
Worksheets("Master Checklist").Activate
Columns("H:H").Select
Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Interior
.ColorIndex = 40
.TintAndShade = 0
End With
'Sheet2.Range(“L2").Value = Application.WorksheetFunction.Countif(Columns("H:H")), cell.Font.Color = "-16383844")
'Range(“B10?).Value = Application.WorksheetFunction.Countif(Range(“A2:A8?), “>” & 50
End Sub
Sub CountDupes()
Dim countofDupes As Long
Dim rng As Range
Dim myCell As Range
countofDupes = 0
Set rng = Range("H2").End(xlDown)
For Each myCell In rng
If myCell.Interior.ColorIndex = 40 Then
countofDupes = countofDupes + 1
Debug.Print countofDupes
End If
Next myCell
End Sub
I don't encounter any error messages but if I Debug.Print countofDupes I get nothing returned, which it obviously not what I want. Any advice?

Highlight cell if greater than today

I'm trying to highlight cells that have a date greater than today's date.
Column H is formatted as Date.
I have the following:
Sub Test()
Dim lrow As Long
lrow = Cells(Rows.Count, 1).End(xlUp).Row
Columns("H:H").EntireColumn.AutoFit
If Range("H2:H" & lrow).Value > Date Then Cell.Interior.Color = vbYellow
End Sub
I get a "Type Mismatch" error.
Range("H2:H" & lrow).Value will be a 2D array (the Value of a Range is always a 2D array if more than a single cell is involved); you're getting a type mismatch error because you can't compare a 2D array to a Date; if you can't use a conditional formatting, you need to compare the individual array subscripts.
Last thing you want to do is to iterate each individual cells (otherwise your next question will be "how do I make this loop run faster?"). Get that array into a Variant, and iterate that array - since it's only 1 column, make it a 1D array with Application.Transpose:
Dim values As Variant
values = Application.Transpose(Range("H2:H" & lastRow).Value)
Dim i As Long, current As Long
For i = LBound(values) To UBound(values)
current = i + 1 'array would be 1-based, so to start at row 2 we need to offset by 1
If values(i) > Date Then
ActiveSheet.Cells(current, 8).Interior.Color = vbYellow
End If
Next
That way you only hit the worksheet when you have to.
In response to #MatthieuGuindon's suggestion to #CharlesPL's answer, here's some code that does the conditional formatting. I've set it so it highlights dates that are after the day you run it as a bright yellow.
Option Explicit
Sub setCondFormat()
Dim lrow As Long
lrow = ActiveSheet.Range("H" & ActiveSheet.Rows.Count).End(xlUp).Row
With Range("H2:H" & lrow)
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=H2>TODAY()"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
End With
End With
End Sub
Use conditional formatting! As the name suggests, this is build for that!
Microsoft blog post on date conditional formating
I would recommend iterating over the range of cells and testing each cell individually. Please see below.
Dim rng As Range, cell As Range
Set rng = Range("H:H")
For Each cell In rng
If cell.Value > Date Then cell.Interior.Color = vbYellow
Next cell

Macro to find duplicates rows

I am looking for a macro to find duplicate rows in a spreadsheet. So far I have come up with this set of code:
Application.ScreenUpdating = False
For Each cell In ActiveSheet.UsedRange.Columns("A").Cells
For Each cell2 In ActiveSheet.UsedRange.Columns("A").Cells 'Loop through entire column A for each iteration in nested for loop
If Cells(y, 1).Value = Cells(z, 1).Value Then 'Duplicate value found
For icol = 1 To 19
If Cells(y, icol).Value = Cells(z, icol).Value Then 'If cell value in current row matches, highlight red
Cells(z, icol).Interior.ColorIndex = 3
End If
Next icol
End If
z = z + 1
Next cell2
y = y + 1 'Next cell
z = y + 1 'Next cell (y+1)
Next cell
Application.ScreenUpdating = True
I have approached this with nested foor loops. The macro is supposed to look for a duplicate value in column A. If found the macro then loops through that row to check if the entire row matches. Every matching cell in this row is then highlighted red. This seems to work fine in small scale when the number of rows isn't too big. However when applying this macro to a spreadsheet with 7000+ rows Excel freeze up and crashes. I suspect this has to do with the nested foor loops. Is there a faster and more practical approach to this?
Try conditional formatting instead of hard-coding the red cell fills.
Option Explicit
Sub dupeRed()
Dim lr As Long, lc As Long
With Worksheets("sheet1")
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
With .Range(.Cells(2, "A"), .Cells(lr, lc))
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND(COUNTIF($A$1:$A1, $A2), A2=INDEX(A:A, MATCH($A2, $A:$A, 0)))"
.FormatConditions(.FormatConditions.Count).Interior.Color = vbRed
End With
End With
End Sub

Finding Last Row With Data With Formatted Cells Below

I need to identify, (by highlighting), when there is data missing from a certain column. In other words, I have a column of data specifying a country. Above this column there are blanks and below this column there are blanks. The topmost row of the data stays the same (the data always starts at row 4), but the bottom is variable. Also, due to the way this data is output, there seems to be 3 or so rows of blank but formatted cells at the bottom of the table which excel recognizes as 'used'. Here is my code thus far:
With ThisWorkbook.ActiveSheet
LastRowCountry = .Range("H" & .Rows.Count).End(xlUp).Row
End With
The Piece of code that is specific to my goal is:
'Search for blank Geo tags
With ThisWorkbook.ActiveSheet
If IsEmpty(Cells(LastRowCountry, "H")) = True Then
'Highlight Columns
With Range(Cells(4, "H"), Cells(LastRow, "H")).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End If
End With
In this form it fails to recognize any blanks in the column and never highlights. Before this I preceded the code with:
With ActiveSheet.Cells.SpecialCells(xlLastCell)
LastRow = .Row
LastCol = .Column
End With
which always highlighted the column (I assume because it was detecting the blank but formatted cells hanging off the bottom of the table. Thanks in advance to anyone who takes this on.
Steve
I have tried this here and it works
Sub CheckForEmptyCells(Byref sh as Worksheet, ByRef col as string)
Dim lastR&
lastR = sh.Range(col & Rows.Count).End(xlUp).Row
Dim r As Range: Set r = Range(col & "5:" & col & lastR)
If Not r.Find("") Is Nothing Then
With r.Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End If
End Sub
Note: this routine checks only if the given column has empty cells in between its own first and last cells. If the goal is to check the whole column (last cell of the column might itself be empty), then you should use instead: lastR = sh.UsedRange.Rows.Count

Resources