Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS XL Plugin
Last modified Mon, 15th Mar 2026.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /XL/Conditional formatting
Download this example: Conditional formatting.zip
Project "Conditional formatting.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits DesktopWindow
Control Button1 Inherits DesktopButton
ControlInstance Button1 Inherits DesktopButton
EventHandler Sub Pressed()
Test1
End EventHandler
End Control
Control Button2 Inherits DesktopButton
ControlInstance Button2 Inherits DesktopButton
EventHandler Sub Pressed()
Test2
End EventHandler
End Control
Control Button3 Inherits DesktopButton
ControlInstance Button3 Inherits DesktopButton
EventHandler Sub Pressed()
Test3
End EventHandler
End Control
Control Button4 Inherits DesktopButton
ControlInstance Button4 Inherits DesktopButton
EventHandler Sub Pressed()
Test4
End EventHandler
End Control
Sub Test1()
'Conditional formatting: highlighting cells that begin With the given Text
'
'This example defines a conditional format with a bold font and sets conditional formatting for the range B3:B11 for highlighting cells that begin with 'a'.
Dim book As New XLBookMBS(True)
Dim sheet As XLSheetMBS = book.AddSheet("my")
Call sheet.WriteString(2, 1, "Poltava")
Call sheet.WriteString(3, 1, "Athens")
Call sheet.WriteString(4, 1, "Thessaloniki")
Call sheet.WriteString(5, 1, "Kalamata")
Call sheet.WriteString(6, 1, "Amsterdam")
Call sheet.WriteString(7, 1, "Alexandroupolis")
Call sheet.WriteString(8, 1, "Kyiv")
Call sheet.WriteString(9, 1, "London")
Call sheet.WriteString(10, 1, "New York")
Dim cFormat As XLConditionalFormatMBS = book.addConditionalFormat()
cFormat.Font.bold = true
Dim cf As XLConditionalFormattingMBS = sheet.addConditionalFormatting(2, 10, 1, 1)
cf.addRule(cf.FormatTypeBeginWith, cFormat, "a")
Dim f As FolderItem = SpecialFolder.Desktop.Child("out1.xlsx")
call book.save(f)
f.Launch
End Sub
Sub Test2()
'Conditional formatting: creating a gradated Color scale on the cells
'
'This example creates a gradated color scale for the range C4:C11.
Dim book As New XLBookMBS(True)
Dim sheet As XLSheetMBS = book.AddSheet("my")
book.RgbMode = True
Call sheet.WriteString(2, 1, "Country")
Call sheet.WriteString(2, 2, "Road injures")
Call sheet.WriteString(3, 1, "USA")
Call sheet.WriteString(4, 1, "UK")
Call sheet.WriteString(5, 1, "Switzerland")
Call sheet.WriteString(6, 1, "Spain")
Call sheet.WriteString(7, 1, "Italy")
Call sheet.WriteString(8, 1, "London")
Call sheet.WriteString(9, 1, "Greece")
Call sheet.WriteString(10, 1, "Japan")
Call sheet.WriteNumber(3, 2, 64)
Call sheet.WriteNumber(4, 2, 94)
Call sheet.WriteNumber(5, 2, 88)
Call sheet.WriteNumber(6, 2, 93)
Call sheet.WriteNumber(7, 2, 86)
Call sheet.WriteNumber(8, 2, 75)
Call sheet.WriteNumber(9, 2, 67)
Call sheet.WriteNumber(10, 2, 91)
Dim cf As XLConditionalFormattingMBS = sheet.addConditionalFormatting(3, 10, 2, 2)
cf.add2ColorScaleRule(book.PackColor(&hFF, &h71, &h28), book.PackColor(&hFF, &hEF, &h9C))
Dim f As FolderItem = SpecialFolder.Desktop.Child("out2.xlsx")
call book.save(f)
f.Launch
End Sub
Sub Test3()
'Conditional formatting: highlighting cells that more than the specified value
'
'This example highlights cells whose values are greater than the specified value (90) with the light green background in the range C4:C11. It's possible to use any operator from CFormatOperator enum.
Dim book As New XLBookMBS(True)
Dim sheet As XLSheetMBS = book.AddSheet("my")
Call sheet.WriteString(2, 1, "Country")
call sheet.WriteString(2, 2, "Road injures")
call sheet.WriteString(3, 1, "USA")
call sheet.WriteString(4, 1, "UK")
call sheet.WriteString(5, 1, "Switzerland")
call sheet.WriteString(6, 1, "Spain")
call sheet.WriteString(7, 1, "Italy")
call sheet.WriteString(8, 1, "London")
call sheet.WriteString(9, 1, "Greece")
call sheet.WriteString(10, 1, "Japan")
Call sheet.WriteNumber(3, 2, 64)
Call sheet.WriteNumber(4, 2, 94)
Call sheet.WriteNumber(5, 2, 88)
Call sheet.WriteNumber(6, 2, 93)
Call sheet.WriteNumber(7, 2, 86)
Call sheet.WriteNumber(8, 2, 75)
Call sheet.WriteNumber(9, 2, 67)
Call sheet.WriteNumber(10, 2, 91)
Const COLOR_LIGHTGREEN = 42
Dim cFormat As XLConditionalFormatMBS = book.addConditionalFormat()
cFormat.FillPattern = cFormat.FillPatternSolid
cFormat.PatternBackgroundColor = COLOR_LIGHTGREEN
Dim cf As XLConditionalFormattingMBS = sheet.addConditionalFormatting(3, 10, 2, 2)
cf.addOpNumRule(cf.FormatOperatorGreaterThan, cFormat, 90)
Dim f As FolderItem = SpecialFolder.Desktop.Child("out3.xlsx")
call book.save(f)
f.Launch
End Sub
Sub Test4()
'Conditional formatting: highlighting alternating rows
'
'This example highlights alternating rows (banded rows) and makes the data in a worksheet easier to scan. It's possible to do this with the formula expression "=MOD(ROW(),2)=0" in a conditional formatting rule. If you want to alternate columns please use this formula: "=MOD(COLUMN(),2)=0".
Dim book As New XLBookMBS(True)
Dim sheet As XLSheetMBS = book.AddSheet("my")
book.RgbMode = True
Dim cFormat As XLConditionalFormatMBS = book.addConditionalFormat()
cFormat.FillPattern = cFormat.FillPatternSolid
cFormat.PatternBackgroundColor = book.PackColor(240, 240, 240)
Dim cFormatting As XLConditionalFormattingMBS = sheet.addConditionalFormatting(4, 20, 1, 10)
cFormatting.addRule(cFormatting.FormatTypeExpression, cFormat, "=MOD(ROW(),2)=0")
For row As Integer = 4 To 19
For col As Integer = 1 To 9
Call sheet.WriteNumber(row, col, row + col)
Next
Next
Dim f As FolderItem = SpecialFolder.Desktop.Child("out4.xlsx")
call book.save(f)
f.Launch
End Sub
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
End Project
Download this example: Conditional formatting.zip
The items on this page are in the following plugins: MBS XL Plugin.