Seven Segment Display

I was reading about seven segment displays over at Sparkfun and thought it would be a fun exercise in Excel. I’m sure it’s been done a million times, but not by me. The first one was VBA based. Type a number into a cell and this code fills cells to display the number as a seven segment display.

OK, it’s really a 13 segment display – the seven segments and six connecting cells. Next, I did the same thing with conditional formatting. I tried to make the conditional formatting formula consistent across the cells, but I just couldn’t. The TRUEs and FALSEs change for each cell depending on if that cell is lit for that number.

Here’s the CF formula for cell H3.

=CHOOSE(MID(TEXT($C$9,"0000"),(COLUMN()+MOD(MOD(MOD(16-COLUMN(),12),8),4))/4,1)+1,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE)

H3 is lit for every number except 5 and 6. There’s data validation on the input cell to keep it under five digits. The CF formula is a CHOOSE function with nine TRUEs/FALSEs. To determine which character to represent, I use a MID function after padding the text to four digits. The starting position (second argument of MID) is determine by this:

(COLUMN()+MOD(MOD(MOD(16-COLUMN(),12),8),4))/4,1)

Column 16-Column Mod 12 Mod 8 Mod 4 Column+ /4
2 14 2 2 2 4 1
3 13 1 1 1 4 1
4 12 0 0 0 4 1
6 10 10 2 2 8 2
7 9 9 1 1 8 2
8 8 8 0 0 8 2
10 6 6 6 2 12 3
11 5 5 5 1 12 3
12 4 4 4 0 12 3
14 2 2 2 2 16 4
15 1 1 1 1 16 4
16 0 0 0 0 16 4

You can download SevenSegment.zip

4 thoughts on “Seven Segment Display

  1. This is a great exercise! I appreciate that you showed multiple approaches as it is always good to see things from multiple angles. I decided to take up your exercise and try my hand at it.

    I setup a truth table for the numbers, so I could separate that out from the conditional formatting formulas, and looked up the appropriate 1/0 for each number character from the table. Now all I need is one CF formula of Cell Value = 1 to handle all of the cells formulas.

    No matter whether the complexity is in the CF formula or on the sheet, it seems that we can’t avoid all of it. It would be interesting to see if any simpler solutions arise from this.

  2. The following single Change event procedure (no other code nor any conditional formatting required) will produce the same results as your posted setup (I have assume the column and row width have been set to their desired widths and heighths)…

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim X As Long, CellVal As Variant, Digit As Range
    If Target.Address = Me.Range("C9").Address Then
    CellVal = Target.Value
    If CellVal Like "*[!0-9]*" Then
    MsgBox "The value you entered is not valid!", vbCritical
    Application.EnableEvents = False
    Application.Undo
    Application.EnableEvents = True
    Else
    CellVal = Format(CellVal + 0, "0000")
    Application.ScreenUpdating = False
    For X = 0 To 3
    With Range("B2").Offset(, 4 * X)
    .Resize(5, 3).Interior.ColorIndex = 1
    Select Case Mid(CellVal, 1 + X, 1)
    Case 0: .Offset(1, 1).Resize(3, 1).Interior.ColorIndex = 0
    Case 1: .Resize(5, 2).Interior.ColorIndex = 0
    Case 2: .Offset(1, 0).Resize(1, 2).Interior.ColorIndex = 0
    .Offset(3, 1).Resize(1, 2).Interior.ColorIndex = 0
    Case 3: .Offset(1, 0).Resize(1, 2).Interior.ColorIndex = 0
    .Offset(3, 0).Resize(1, 2).Interior.ColorIndex = 0
    Case 4: .Offset(0, 1).Resize(2, 1).Interior.ColorIndex = 0
    .Offset(3, 0).Resize(2, 2).Interior.ColorIndex = 0
    Case 5: .Offset(1, 1).Resize(1, 2).Interior.ColorIndex = 0
    .Offset(3, 0).Resize(1, 2).Interior.ColorIndex = 0
    Case 6: .Offset(1, 1).Resize(1, 2).Interior.ColorIndex = 0
    .Offset(3, 1).Resize(1, 1).Interior.ColorIndex = 0
    Case 7: .Offset(1, 0).Resize(4, 2).Interior.ColorIndex = 0
    Case 8: .Offset(1, 1).Resize(1, 1).Interior.ColorIndex = 0
    .Offset(3, 1).Resize(1, 1).Interior.ColorIndex = 0
    Case 9: .Offset(1, 1).Resize(1, 1).Interior.ColorIndex = 0
    .Offset(3, 0).Resize(1, 2).Interior.ColorIndex = 0
    End Select
    End With
    Next
    Application.ScreenUpdating = True
    End If
    End If
    End Sub

  3. That’s a lot of code to accomplish a simple task.
    A oneliner will do.

    With conditional formatting in the range A1:O5 the result will be shown as in Dick’s example.
    Formatting conditions:
    – when a cell is not empty than black for the seven elements (A2,A4,B1,B3,B5,C2,C4);
    – if the adjacent cells are not empty then black for the corner cells between the seven elements (A1,A3,A5,C1,C3,C5). This formatting extended to the Range A1:O5.

    In the code I use an asterisk for illustration purposes; I’d prefer to use a space instead.
    The number to be shown is in cell A30 (at least in the code below).


    Sub M_snb()
    If Len(Format(Val(Cells(30, 1)))) <> 4 Then Exit Sub
    [A1:O5] = [if(iserror(find(address(row(A1:O5),mod(column(A1:O5)-1,4)+1,4),choose(mid(A30,int(column(A1:O5)/4)+1,1)+1,"A2A4B1B5C2C4","C2C4","B1C2B3A4B5","B1C2B3C4B5","A2B3C2C4","A2B1B3C4B5","A2A4B3C4B5","B1C2C4","A2A4B1B3B5C2C4","A2B1B3C2C4"))),"","*")]
    End Sub

  4. Nice (although I wouldn’t want to maintain it). Next iteration. Put this formula in A1, then fill right and down to 05.

    Then put CF on A1:05 like this

    I moved in the input cell to column B because it was wider. The download file will be updated to include @snb’s VBA solution and this one.

Leave a Reply

Your email address will not be published. Required fields are marked *