Make cells look like buttons - vba

I am trying to make Excel cells look like buttons without actually inserting buttons.
For Each myCell In Range(BoardSize)
With myCell
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThick
.Borders.Color = RGB(110, 110, 110)
.Interior.Color = RGB(180, 180, 180)
End With
myCell.Borders(xlEdgeTop).Color = RGB(255, 255, 255)
myCell.Borders(xlEdgeLeft).Color = RGB(255, 255, 255)
Next myCell
It works for one cell:
but in a large range it looks like this:
What I want is something, without using actual command buttons, like:

For Each mycell In Range(BoardSize)
isblack = mycell.Row Mod 2 = 0 Xor mycell.Column Mod 2 = 0
With mycell
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThick
.Borders.Color = RGB(110, 110, 110)
.Interior.Color = RGB(180, 180, 180)
End With
If Not isblack Then
mycell.Borders(xlEdgeTop).Color = RGB(255, 255, 255)
mycell.Borders(xlEdgeLeft).Color = RGB(255, 255, 255)
End If
Next mycell
Another version with a minor artifact. It skipps odd rows and odd columns
Dim mycell As Range
For Each mycell In Range(BoardSize)
evenrow = mycell.Row Mod 2 = 0
evencol = mycell.Column Mod 2 = 0
isblack = evenrow Xor evencol
With mycell
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThick
.Borders.Color = RGB(110, 110, 110)
.Interior.Color = RGB(180, 180, 180)
End With
If Not isblack Then
mycell.Borders(xlEdgeTop).Color = RGB(255, 255, 255)
mycell.Borders(xlEdgeLeft).Color = RGB(255, 255, 255)
End If
If evenrow Or evencol Then mycell.Borders.Color = RGB(180, 180, 180)
If evencol And mycell.ColumnWidth <> 0.1 Then mycell.ColumnWidth = 0.1 Else mycell.ColumnWidth = 5
If evenrow And mycell.RowHeight <> 1 Then mycell.RowHeight = 1 Else mycell.RowHeight = 30
Next mycell

Related

Powerpoint table formatting; last row borders not styled

I'm trying to format the selected table with a colorless first row with a bottom border, interlined light grey rows, and the last row with top and bottom borders.
Everything seems to be working fine except with the last row's top and bottom borders not being styled correctly.
Can you help me fix the problem?
Thanks in advance!
Here's the code:
Sub FormatShape()
Dim oSlide As slide
Dim oShape As Shape
Dim oTable As Table
Dim oCell As cell
Dim iRow As Long
Dim iCol As Long
Set oSlide = Application.ActiveWindow.View.slide
Set oShape = ActiveWindow.Selection.ShapeRange(1)
RowTotal = True
If Not oShape.HasTable Then
MsgBox "Please select a table and try again."
Else
Set oTable = oShape.Table
For iRow = 1 To oTable.Rows.Count
For iCol = 1 To oTable.Columns.Count
With oTable.cell(iRow, iCol)
With .Shape.TextFrame.textRange
.Font.Name = "Graphik LCG"
.Font.size = 10
.Font.Color.RGB = vbBlack
.Font.Bold = True
End With
If iRow = 1 Then
With oTable.cell(iRow, iCol)
.Shape.Fill.ForeColor.RGB = vbWhite
With .Borders(ppBorderTop)
.ForeColor.RGB = vbWhite
.Visible = False
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderLeft)
.ForeColor.RGB = vbWhite
.Visible = False
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderBottom)
.Visible = True
.ForeColor.RGB = vblack
.Weight = 1
End With
With .Borders(ppBorderRight)
.Visible = False
.ForeColor.RGB = vbBlack
.Weight = 1
.Transparency = 1
End With
End With
Else
.Shape.TextFrame.textRange.Font.Bold = False
' check if odd number
If Not iRow Mod 2 <> 0 Then
.Shape.Fill.ForeColor.RGB = RGB(235, 235, 235)
Else
.Shape.Fill.ForeColor.RGB = vbWhite
End If
With oTable.cell(iRow, iCol)
With .Borders(ppBorderLeft) 'Left
.Visible = msoFalse
.ForeColor.RGB = vbWhite
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderBottom) 'Bottom
.Visible = msoTrue
.ForeColor.RGB = vbWhite
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderRight) 'Right
.Visible = msoFalse
.ForeColor.RGB = vbWhite
.Weight = 1
.Transparency = 1
End With
If iRow = oTable.Rows.Count - 1 Then
With .Borders(ppBorderBottom)
.Visible = True
.ForeColor.RGB = vblack
.Weight = 1
End With
End If
If iRow = oTable.Rows.Count Then
MsgBox "here"
With .Borders(ppBorderTop)
.Visible = True
.ForeColor.RGB = vblack
.Weight = 1
End With
With .Borders(ppBorderBottom)
.Visible = True
.ForeColor.RGB = vblack
.Weight = 1
End With
oTable.cell(iRow, iCol).Shape.TextFrame.textRange.Font.Bold = True
End If
End With
End If
End With
Next
Next
End If
End Sub
The best way to do this is to edit the presentation XML to create a custom table style. Then you would have a table where you could use the program interface to switch the header and total rows and the banding on and off, just like a real PowerPoint table.
Editing XML is very similar to editing HTML. Here are my articles on how to do this: OOXML Hacking: Custom Table Styles OOXML Hacking: Table Styles Complete OOXML Hacking: Default Table Text
But since you got started on doing this with VBA, let's finish the task. Your code had a bunch of mistakes, but the main issue with tables is that the top border of the bottom row doesn't just belong to the bottom row. It's also the bottom border of the row second from the bottom.
This code sets both the bottom border of the second last row, and the top border of the last row. It's working here:
Sub FormatTable()
Dim oShape As Shape
Dim oTable As Table
Dim oCell As Cell
Dim iRow As Long
Dim iCol As Long
Set oShape = ActiveWindow.Selection.ShapeRange(1)
RowTotal = True
If Not oShape.HasTable Then
MsgBox "Please select a table and try again."
Else
Set oTable = oShape.Table
For iRow = 1 To oTable.Rows.Count
For iCol = 1 To oTable.Columns.Count
With oTable.Cell(iRow, iCol)
With .Shape.TextFrame.TextRange
.Font.Name = "Graphik LCG"
.Font.Size = 10
.Font.Color.RGB = RGB(0, 0, 0)
.Font.Bold = True
End With
If iRow = 1 Then
'Format first row
With oTable.Cell(iRow, iCol)
.Shape.Fill.ForeColor.RGB = RGB(255, 255, 255)
With .Borders(ppBorderTop)
.ForeColor.RGB = RGB(255, 255, 255)
.Visible = False
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderLeft)
.ForeColor.RGB = RGB(255, 255, 255)
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderBottom)
.Visible = True
.ForeColor.RGB = RGB(0, 0, 0)
.Weight = 1
End With
With .Borders(ppBorderRight)
.Visible = False
.ForeColor.RGB = RGB(0, 0, 0)
.Weight = 1
.Transparency = 1
End With
End With
ElseIf iRow > 1 And iRow < (oTable.Rows.Count - 1) Then
'Format second to second-last rows
.Shape.TextFrame.TextRange.Font.Bold = False
' check if odd number
If Not iRow Mod 2 <> 0 Then
.Shape.Fill.ForeColor.RGB = RGB(235, 235, 235)
Else
.Shape.Fill.ForeColor.RGB = RGB(255, 255, 255)
End If
With oTable.Cell(iRow, iCol)
With .Borders(ppBorderLeft) 'Left
.Visible = msoFalse
.ForeColor.RGB = RGB(255, 255, 255)
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderBottom) 'Bottom
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 255, 255)
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderRight) 'Right
.Visible = msoFalse
.ForeColor.RGB = RGB(255, 255, 255)
.Weight = 1
.Transparency = 1
End With
End With
ElseIf iRow = (oTable.Rows.Count - 1) Then
'Apply different formatting to second-last row
.Shape.TextFrame.TextRange.Font.Bold = False
If Not iRow Mod 2 <> 0 Then
.Shape.Fill.ForeColor.RGB = RGB(235, 235, 235)
Else
.Shape.Fill.ForeColor.RGB = RGB(255, 255, 255)
End If
With oTable.Cell(iRow, iCol)
With .Borders(ppBorderLeft) 'Left
.Visible = msoFalse
.ForeColor.RGB = RGB(255, 255, 255)
.Weight = 1
.Transparency = 1
End With
With .Borders(ppBorderBottom) 'Bottom
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Weight = 1
.Transparency = 0
End With
With .Borders(ppBorderRight) 'Right
.Visible = msoFalse
.ForeColor.RGB = RGB(255, 255, 255)
.Weight = 1
.Transparency = 1
End With
End With
Else
'Format last row
.Shape.Fill.ForeColor.RGB = RGB(255, 255, 255)
With oTable.Cell(iRow, iCol)
With .Borders(ppBorderTop)
.Visible = True
.ForeColor.RGB = RGB(0, 0, 0)
.Weight = 1
End With
With .Borders(ppBorderBottom)
.Visible = True
.ForeColor.RGB = RGB(0, 0, 0)
.Weight = 1
End With
End With
oTable.Cell(iRow, iCol).Shape.TextFrame.TextRange.Font.Bold = True
End If
End With
Next iCol
Next iRow
End If
End Sub

Leave cell formatting intact

I have got this code:
With .Cells(i, 4)
If .Value > 0.8 Then
.Interior.Color = RGB(237, 67, 55) '<-- Red color
.Font.Color = vbWhite
ElseIf .Value > 0.6 Then
.Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
.Font.Color = vbWhite
ElseIf .Value2 = "---" Then
.Interior.Color = .Interior.Color
Else
.Interior.Color = RGB(50, 205, 50) '<-- Green color
.Font.Color = vbWhite
End If
End With
The worksheet is already formatted to have alternating row colours by default but pending on the values of the cell the interior cell colour needs to change pending on the value of the cell but, some cells don't have numerical data and just "---". When this is the case I want the original formatting to remain as it was before the code was run.
Basically, If the cell contains "---" don't assign any other interior colour, but keep the colour already assigned.
I tested this and it seemed to work on my test data, for some reason excel is treating "---" as a value larger than 0.8 so moving the check for "---" first will stop this.
With .Cells(i, 4)
If .Value = "---" Then
.Interior.Color = .Interior.Color
ElseIf .Value > 0.8 Then
.Interior.Color = RGB(237, 67, 55) '<-- Red color
.Font.Color = vbWhite
ElseIf .Value > 0.6 Then
.Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
.Font.Color = vbWhite
Else
.Interior.Color = RGB(50, 205, 50) '<-- Green color
.Font.Color = vbWhite
End If
End With

Percentage values not formatting correctly in Excel

I have got a list of percentage values that are really small (0.000% format), this represents the error percentage of routers. i want to format the cell color depending on the amount on the cell. if the amount is more than 0.050% it should be Red, if it is more than 0.005% is amber, everything else is green
here is the code that i have written:
With .Cells(i, 8)
If .NumberFormat <> "0.000%" Then
.NumberFormat = "0.000%"
If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100
If .Value2 = vbNullString Then
.Value = "---"
.HorizontalAlignment = xlRight
End If
Else
.Value = 0
End If
If .Value > 0.05 Then
.Interior.Color = RGB(237, 67, 55) '<-- Red color
.Font.Color = vbWhite
ElseIf .Value > 0.005 Then
.Interior.Color = RGB(255, 190, 0) '<-- Amber Colour
.Font.Color = vbWhite
Else
.Interior.Color = RGB(50, 205, 50) '<-- Green color
.Font.Color = vbWhite
End If
End With
but the colour format is not accurate, here is the list of some of the results:
0.034% <---green
0.845% <---amber
0.007% <---green
0.005% <---green
0.094% <---green
it should not be like that as the cell that contains 0.845% and is amber should be bright red!
The value stored is not a percentage. It is the decimal equivalent, meaning you must shift the decimal point two places to the left. So to compare 0.05% you must use 0.0005.
This should clean up the code for you to make it a bit faster too:
Sub Test()
Dim Cel As Range, Rng As Range
Set Rng = Range("H1:H" & Range("H1048576").End(xlUp).Row).SpecialCells(xlCellTypeConstants)
For Each Cel In Rng
If Trim(Cel.Value) = "" Then Cel.Value = "---": Cel.HorizontalAlignment = xlRight
If IsNumeric(Cel.Value) Then
Cel.Value = Cel.Value / 100
If Cel.Value > 0.0005 Then
Cel.Interior.Color = RGB(237, 67, 55): Cel.Font.Color = vbWhite
ElseIf Cel.Value > 0.00005 Then
Cel.Interior.Color = RGB(255, 190, 0): Cel.Font.Color = vbWhite
Else: Cel.Interior.Color = RGB(50, 205, 50): Cel.Font.Color = vbWhite
End If
End If
Next
With Range("H1:H" & Range("H1048576").End(xlUp).Row).SpecialCells(xlCellTypeBlanks)
.Value = "---"
.HorizontalAlignment = xlRight
End With
End Sub
I'm just after realizing Paul corrected your question....

Excel InStr Function opposite direction

I have this VBA code that I am using to compare two columns in my Excel spreadsheet, column B to column A. It then "highlights" the ones that are missing from column A but in column B.
What I can't figure out is how to reverse the procedure to search column B and highlight the ones in column A that are different.
Original Code:
For i = 2 To LastRow
For j = 2 To LastRow
If Report.Cells(i, 2).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
Report.Cells(i, 2).Interior.Color = xlNone 'Transparent background
Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
I have tried renaming the letters and switching the column values and got close but realized that it was using the values from the original search and just highlighting the corresponding cells in column A.
To answer your question:
For j = 2 To LastRow
For i = 2 To LastRow
If Report.Cells(j, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(i, 2).Value, Report.Cells(j, 1).Value, vbTextCompare) > 0 Then
Report.Cells(j, 1).Interior.Color = xlNone 'Transparent background
Report.Cells(j, 1).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(j, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(j, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next i
Next j
If you wanted to used conditional formatting which makes the color changes live you can replace both loop with:
With Report.Range("A2:A" & LastRow).FormatConditions
.Delete
With .Add(Type:=xlExpression, Formula1:="=And(iserror(Vlookup(A2,B:B,1,False)),A2<>"""")")
.Font.Color = RGB(255, 199, 206)
.Interior.Color = RGB(156, 0, 6)
End With
End With
With Report.Range("B2:B" & LastRow).FormatConditions
.Delete
With .Add(Type:=xlExpression, Formula1:="=And(iserror(Vlookup(B2,A:A,1,False)),B2<>"""")")
.Font.Color = RGB(255, 199, 206)
.Interior.Color = RGB(156, 0, 6)
End With
End With
Edit the issue was that the data in Column A had an extra space at the end thus making the instr to return false.
For j = 2 To LastRow
Report.Cells(j, 1).Value = Trim(Report.Cells(j, 1).Value)
For i = 2 To LastRow
Report.Cells(i, 2).Value = Trim(Report.Cells(i, 2).Value)
If Report.Cells(j, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(i, 2).Value, Report.Cells(j, 1).Value, vbTextCompare) > 0 Then
Report.Cells(j, 1).Interior.Color = xlNone 'Transparent background
Report.Cells(j, 1).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(j, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(j, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next i
Next j
By trimming the values, the instr returned true.
There are many ways to accomplish this.
You could use formulas, you could create dictionaries.
A Quick solution would be:
Dim stringCount As Integer
Dim myString As String
Dim col1Range As Range
Dim col2Range As Range
Set col1Range = Report.Range("A1")
Set col2Range = Report.Range("B1")
For i = 1 To LastRow
myString = col1Range.Offset(i).Value
If myString <> "" Then
stringCount = WorksheetFunction.CountIf(Range("B:B"), myString)
If (stringCount = 0) Then
col1Range.Offset(i).Interior.Color = RGB(156, 0, 6) 'Dark red background
col1Range.Offset(i).Font.Color = RGB(255, 199, 206) 'Light red font color
Else
col1Range.Offset(i).Interior.Color = xlNone 'Transparent background
col1Range.Offset(i).Font.Color = RGB(0, 0, 0) 'Black font color
End If
End If
Next i
For j = 1 To LastRow
myString = col2Range.Offset(j).Value
If myString <> "" Then
stringCount = WorksheetFunction.CountIf(Range("A:A"), myString)
If (stringCount = 0) Then
col2Range.Offset(j).Interior.Color = RGB(156, 0, 6) 'Dark red background
col2Range.Offset(j).Font.Color = RGB(255, 199, 206) 'Light red font color
Else
col2Range.Offset(j).Interior.Color = xlNone 'Transparent background
col2Range.Offset(j).Font.Color = RGB(0, 0, 0) 'Black font color
End If
End If
Next j

VBA multiple IF values

The below code works and changes the offset cells when north is entered, i would like to also have it change if its south, west or east but i can seem to find a way to add this.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("A7:A26")
Set rng = Range("A7:A26")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
For Each cell In rng.Cells
If cell.Value = "North" Then
cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
End If
Next
End If
End Sub
Try using a Select Case instead of If statements for this.
For Each cell In Rng.Cells
Select Case cell.Value
Case "North"
Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 255, 0)
Case "South"
Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 155, 0)
Case "East"
Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 55, 0)
Case "West"
Range(cell.Offset(0, 1), cell.Offset(0, 3)).Interior.Color = RGB(0, 0, 0)
End Select
Next
Give this a shot. Just update the color assignments as needed:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("A7:A26")
Set rng = Range("A7:A26")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
For Each cell In rng.Cells
If cell.Value = "North" Then
cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
ElseIf cell.Value = "South" Then
cell.Offset(0, 1).Interior.Color = RGB(255, 0, 0)
cell.Offset(0, 2).Interior.Color = RGB(255, 0, 0)
cell.Offset(0, 3).Interior.Color = RGB(255, 0, 0)
ElseIf cell.Value = "East" Then
cell.Offset(0, 1).Interior.Color = RGB(0, 0, 255)
cell.Offset(0, 2).Interior.Color = RGB(0, 0, 255)
cell.Offset(0, 3).Interior.Color = RGB(0, 0, 255)
ElseIf cell.Value = "West" Then
cell.Offset(0, 1).Interior.Color = RGB(0, 255, 255)
cell.Offset(0, 2).Interior.Color = RGB(0, 255, 255)
cell.Offset(0, 3).Interior.Color = RGB(0, 255, 255)
End If
Next
End If
End Sub
Consider:
If cell.Value = "North" Or cell.Value = "South" Then
cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
End If
or:
If cell.Value = "North" Then
cell.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 2).Interior.Color = RGB(0, 255, 0)
cell.Offset(0, 3).Interior.Color = RGB(0, 255, 0)
ElseIf cell.Value = "South"
cell.Offset(0, 1).Interior.Color = RGB(0, 0, 255)
cell.Offset(0, 2).Interior.Color = RGB(0, 0, 255)
cell.Offset(0, 3).Interior.Color = RGB(0, 0, 255)
End If