Forum Discussion
VBA: Select multiple Columns till the last row to add a background color.
- Aug 15, 2022
Found it. https://www.exceldome.com/solutions/color-non-blank-cells/
Didn't have to dim declare TextCell
For Each TextCell In Selection If TextCell <> "" Then TextCell.Font.Italic = True TextCell.Interior.Pattern = xlSolid TextCell.Interior.PatternColorIndex = xlAutomatic TextCell.Interior.ThemeColor = xlThemeColorAccent4 TextCell.Interior.TintAndShade = 0.399975585192419 TextCell.Interior.PatternTintAndShade = 0 End If Next
Does it have to be with VBA?
This is a bit more cumbersome and slows down the workbook.
Why not use conditional formatting? or with filter by color?
Here is a small correction in your code (inserted file).
We don't know if this helps, but... who knows 🙂
I know I don't know anything (Socrates)
- SandeeepAug 15, 2022Brass Contributor
Yep, needs to be VBA.
The point is to beautify my data.I get 5 excel files, every day in the same fashion.
excepts values are different. Row count ranging from 15-15kI just want them to all look nicer, and have my eyes dart/go to where it's immediately needed.
And I don't wanna keep doing conditional formatting 27 columns 5-7 times a day 25 days a month, with multiple clicks or spend lots of time.
Just one quick macro and I'm done.Btw, can you help me with the condition formatting of column D, labeled 'REASON'.
I need for the selection to identify if the cell is empty or not.
If it is not empty, then change the formatting (BGcolor and font)I tried using a IF THEN, for each. with ISEMPTY and ISTEXT.
it doesn't seem to do it.As for all the other questions, I guess if it's too complex, I'll copy-paste the same code for each colored blocks. (makes the code twice as long, but at least it works)
Here is the code, I currently have, that applies a BG color to the selected range, but again doesn't apply a different formatting to Non-Empty cells in the same selected range.
Range("L2:M2").Resize(Lastrow).Select ' Where Lastrow is the last populated row in ColA With Selection.Interior ' BG color all the selected cells .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent4 .TintAndShade = 0.599993896298105 .PatternTintAndShade = 0 End With ' The if statement to find which of the cells in the range is Non-Empty and add ' different formatting, i.e making it gold and italic With Selection If IsEmpty(Selection.Value) = True Then .Font.Italic = True .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent4 .TintAndShade = 0.399975585192419 .PatternTintAndShade = 0 End If End With ' Adding Borders to this selection With Selection ' Adding Borders .Borders.Weight = xlThin .Borders(xlInsideVertical).LineStyle = xlNone .Borders(xlInsideHorizontal).LineStyle = xlNone End With
- SandeeepAug 15, 2022Brass Contributor
Found it. https://www.exceldome.com/solutions/color-non-blank-cells/
Didn't have to dim declare TextCell
For Each TextCell In Selection If TextCell <> "" Then TextCell.Font.Italic = True TextCell.Interior.Pattern = xlSolid TextCell.Interior.PatternColorIndex = xlAutomatic TextCell.Interior.ThemeColor = xlThemeColorAccent4 TextCell.Interior.TintAndShade = 0.399975585192419 TextCell.Interior.PatternTintAndShade = 0 End If Next