Bold the entire row based on a cell's font - excel

I have a set of data in columns A to Z, if any cells in Column F is bolded, shall call to bold the entire row.
For example, F3 and F80 is bolded. A3:Z3 and A80:Z80 shall be bolded. My code only works until bolding cells in column F, can't proceed to bold the entire row.
Sub Bold()
Dim CheckRange As Range
Dim cell As Range
With ActiveSheet
Set CheckRange = .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
End With
With CheckRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual, formula1:="1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
For Each cell In CheckRange
If cell(cell.Row, 6).Font.Bold = True Then
cell.EntireRow.Font.Bold = True
End If
Next
End Sub
Any help is much appreciated.

May be using a formula for the conditional formatting is better
Sub Bold()
With ActiveSheet.UsedRange
.FormatConditions.Delete
.Range("A1:Z" & .Cells(Rows.Count, 6).End(xlUp).Row).FormatConditions.Add Type:=xlExpression, Formula1:="=$F1>=1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
End With
End Sub
Or to adhere to your code you can use offset and resize in the loop
Sub Bold()
Dim checkRange As Range, cell As Range
With ActiveSheet
Set checkRange = .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
End With
With checkRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
End With
For Each cell In checkRange
If cell.DisplayFormat.Font.Bold = True Then
cell.Offset(, -5).Resize(1, 26).Font.Bold = True
End If
Next cell
End Sub

The code sample is missing an end with after the first one to run.
Other than that, the issue is here: If cell(cell.Row, 6).Font.Bold
cell is already a Range type reference to the cell you need, so you don't need to look up anything, in fact doing that causes it to point elsewhere with the cell function: for example this is from the watch window, note the value difference:
Watch : : cell.Address : "$F$2" : String : Module1.Bold
Watch : : cell(cell.Row, 6).Address : "$K$3" : Variant/String : Module1.Bold
This is the full code:
Sub Bold()
Dim CheckRange As Range
Dim cell As Range
With ActiveSheet
Set CheckRange = .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row)
End With
With CheckRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="1000000"
With .FormatConditions(1)
.Font.Bold = True
.StopIfTrue = False
End With
End With
For Each cell In CheckRange
If cell.Font.Bold = True Then
cell.EntireRow.Font.Bold = True
End If
Next
End Sub

Related

Conditional Formatting Excel VBA

Hi, every day i have to update an excel file. This includes formatting column B. (see picture above). I haven't found VBA code yet, to geht this kind of formatting via VBA. in the picture you see a subset of formatting rules, there are more. But there is only those three colors, which I have the hex code.
yellow
#9C5700
red
#9C0006
green
#006100
' (1) Highlight defined good as green values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=2")
.Interior.ColorIndex = 6
.StopIfTrue = False
End With
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "in Anfrage")
.Interior.ColorIndex = 6
.StopIfTrue = False
End With
' (2) Highlight defined ok as yellow values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=1")
.Interior.ColorIndex = 4
.StopIfTrue = False
End With
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "ok")
.Interior.ColorIndex = 4
.StopIfTrue = False
End With
' (2) Highlight defined bad as red values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=3")
.Interior.ColorIndex = 3
.StopIfTrue = False
End With
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "kritisch")
.Interior.ColorIndex = 3
.StopIfTrue = False
End With
End Sub
I used this code, but i would like to use the hex colors. How do I use those?
Per this Article:
You can assign the color codes to any Color property of any object in either their decimal or hex representation. Precede the Hex value with the &H prefix
However for some reason VBA does swap the first two characters with the last two characters of a hex code, so your Yellow 9C7500 would go into VBA as 00759C
So, instead of .Interior.ColorIndex, use .Interior.Color and put in your hex codes with &H at the start.
Example:
' (1) Highlight defined good as green values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=2")
.Interior.Color = &H006100
.StopIfTrue = False
End With
You can use .Color instead of .ColorIndex and you can also use RGB() to more easily set the value
so change your code to
.Interior.Color = RGB(&H9C,&H57,&H00)
Please, try the next code. Formatting the whole column will consume a lot of Excel resources, slows down the process of formulas update and it useless. The above code format only the B:B column having data:
Sub SetFormatRngMultiple_Cond()
Dim ws As Worksheet, lastR As Long, rngF As Range
Set ws = ActiveSheet
lastR = ws.Range("B" & ws.rows.count).End(xlUp).row
Set rngF = ws.Range("B2:B" & lastR)
With rngF
'first condition:
With .FormatConditions
.Delete
.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=2"
End With
With .FormatConditions(.FormatConditions.count)
.Font.Color = 1137094
.Interior.Color = vbYellow
.SetFirstPriority: .StopIfTrue = False
End With
'second condition:
With .FormatConditions
.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=1"
End With
With .FormatConditions(.FormatConditions.count)
.Font.Color = 5287936
.Interior.Color = 11854022
.StopIfTrue = False
End With
'third condition:
With .FormatConditions
.Add Type:=xlTextString, String:="OK", TextOperator:=xlContains
End With
With .FormatConditions(.FormatConditions.count)
.Font.Color = 5287936
.Interior.Color = 11854022
.StopIfTrue = False
End With
'fourth condition:
With .FormatConditions
.Add Type:=xlTextString, String:="kritish", TextOperator:=xlContains
End With
With .FormatConditions(.FormatConditions.count)
.Font.Color = vbRed
.Interior.Color = 14083324
.StopIfTrue = False
End With
End With
End Sub

Loop Vlookup "for each cell in range" from another worksheet

I'm trying to loop Vlookup using "for each cell in range" from another worksheet but all lookup_value is getting results even though it doesn't MATCH with the data from table_array.
Here's a sample table & code.
table_array
When IN button was clicked, I want the quantity from each cell of table_array to add the quantity in corresponding lookup_value cells.
Private Sub cmdIN_Click()
Dim rng As Range, cell As Range
Set rng = Sheet2.Range("prodDesc")
If MsgBox("Are you sure you want to update the stock IN quantity?", vbQuestion + vbYesNo, "Fresh Herbs & Spices") = vbNo Then
Cancel = True
End If
Application.EnableEvents = False
Application.ScreenUpdating = False
Sheet2.Unprotect Password:=""
On Error Resume Next 'on error run code on next line
For Each cell In rng
cell.Offset(, 12).Value = [VLookup(prodDesc, descTable, 2, False)] + cell.Offset(, 12).Value
Next cell
Sheet2.Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
Cells(Rows.Count, "C").End(xlUp).Offset(3).Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
However the result is NOT as expected, because all cells in lookup_value worksheets are getting same result with or without MATCH.
lookup_value
By the way, prodDesc and descTable are dynamic table as range.
dynamic tables
After doing some research & asking questions,
Here's a working code:
Private Sub cmdIN_Click()
Dim cell As Range, qty
If MsgBox("Are you sure you want to update the stock IN quantity?", vbQuestion + vbYesNo, "Fresh Herbs & Spices") = vbYes Then
Application.EnableEvents = False
Application.ScreenUpdating = False
Sheet2.Unprotect Password:=""
For Each cell In Sheet2.Range("prodDesc")
qty = Application.VLookup(cell.Value, Range("descTable"), 2, False)
If Not IsError(qty) Then cell.Offset(, 13).Value = qty + cell.Offset(, 13).Value
Next cell
Sheet2.Protect Password:="", DrawingObjects:=True, Contents:=True, Scenarios:=True
Cells(Rows.Count, "C").End(xlUp).Offset(3).Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub

Looking for improving the speed of a macro working with a loop and converting date to other format

My code below works but it is very slow… This code in fact consists to convert the date in column C and D of my sheet (called "Test") from format day.month.year to format day/month/year (For example please see the picture below, the lines 1-2-3-4-5 have been already converted whereas the other lines from line 1183 have not been converted yet).
I am looking for a solution to improve the speed of this macro because if I have a lot of lines to convert in column C and D, the macro is really really slow…
If by chance someone know how to improve the speed of this macro, that would be really fantastic.
Sub convertdatrighteuropeanformat()
Dim cell As Range
Call selectallmylinesctrlshiftdown
Application.ScreenUpdating = False
For Each cell In Selection
With cell
.NumberFormat = "#"
.Value = Format(.Value, "dd/mm/yyyy")
End With
Next cell
Selection.Replace What:="/", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.ScreenUpdating = True
End Sub
Sub selectallmylinesctrlshiftdown()
With Sheets("Test")
.Range(.Range("D2"), .Range("E2").End(xlDown)).Select
End With
End Sub
Instead of a loop, refer to the entire Range (previously Selection) at once inside the With block. This is combined into one sub, although there is nothing wrong with your decision to declare the range with a stand alone procedure.
Option Explicit
Sub convert()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Test")
Dim LRow As Long, MyCell As Range, MyRange As Range
LRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
Set MyRange = ws.Range("D2:E" & LRow)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With MyRange
.Value = Format(.Value, "dd/mm/yyyy")
.Replace "/", ".", xlPart, xlByRows
.NumberFormat = "#"
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Insert New Row using `With` instead of `.Select`

I'm having trouble trying to use with instead of .select when writing code to insert a new row. I have been told multiple times that .select is not to be used as it is much slower.
My macro creates a new row but deletes the contents in the row below and copies the formatting of the row above which never happened when I was using .select. This also means that the increasing number in cell B11 is not correct as it starts again from 1 due to the cleared contents below.
Sub New_Entry()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim rng As Range
Set rng = Range("B11:AB11")
With rng
.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End With
Application.CutCopyMode = False
With rng
.ClearContents
.Interior.ColorIndex = xlNone
.Borders.LineStyle = xlContinuous
End With
With Range("B11")
.Value = Range("B12") + 1
End With
With rng
.Font.Bold = False
.Font.ColorIndex = xlAutomatic
.Font.TintAndShade = 0
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Any help would be appreciated, thanks!
As far as I can see you have two questions:
1. Why does this delete the content?
That is because your With rng takes Set rng = Range("B11:AB11") and does .ClearContents to it at the same time as it inserts a row.
You can check this by switching around the order of your code.
All With statements with the same condition always run at the same time.
2. Why is the formatting copied?
The format isn't actually copied, you are formatting every line you create with .Borders.LineStyle = xlContinuous.
This should work:
Sub New_Entry()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim rng As Range
Set rng = Range("B11:AB11")
With rng
'.ClearContents
.Interior.ColorIndex = xlNone
'.Borders.LineStyle = xlContinuous
'End With
'With rng
.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End With
Application.CutCopyMode = False
With Range("B11")
.Value = Range("B12") + 1
End With
With rng
.Font.Bold = False
.Font.ColorIndex = xlAutomatic
.Font.TintAndShade = 0
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
.Select is slower because it runs code line by line.
The range "rng" seems to shift down after the insert. Here is the route I would take:
Sub New_Entry()
Application.ScreenUpdating = False
Application.EnableEvents = False
Range("B11:AB11").Insert Shift:=xlDown
Range("B11").Value = Range("B12") + 1
With Range("B11:AB11")
.Interior.ColorIndex = xlNone
.Borders.LineStyle = xlContinuous
.Font.Bold = False
.Font.ColorIndex = xlAutomatic
.Font.TintAndShade = 0
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Paste cell values rather than formulas

I am searching multiple sheets for a string, once this string is found it copies it to the MergedData sheet.
The problem i am having is it is coping the formulas rather that the value. I have been searching for hours and cannot seem to find a solution.
Here is the code that i am using at the moment.
Any assistance you can provide is greatly appreciated!
Thanks Aarron
Private Sub CommandButton1_Click()
Dim FirstAddress As String, WhatFor As String
Dim Cell As Range, Sheet As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
.CutCopyMode = False
End With
WhatFor = Sheets("SUB CON PAYMENT FORM").Range("L9")
Worksheets("MergedData").Cells.Clear
If WhatFor = Empty Then Exit Sub
For Each Sheet In Sheets
If Sheet.Name <> "SUB CON PAYMENT FORM" And Sheet.Name <> "MergedData" _
And Sheet.Name <> "Details" Then
With Sheet.Columns(1)
Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Cell.EntireRow.Copy ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count)_
.End(xlUp).Offset(1, 0)
Set Cell = .FindNext(Cell)
Loop Until Cell Is Nothing Or Cell.Address = FirstAddress
End If
End With
End If
Next Sheet
Set Cell = Nothing
End Sub
With a little help from others all i had to do was replace.
Cell.EntireRow.Copy ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
With this:
Cell.EntireRow.Copy
ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlValues
Don't use the range.copy method with a destination, but use only copy and use PasteSpecial with XlPasteType = xlPasteValues
Range.PasteSpecial Method (Excel)

Resources