A bit of fun - Draw a christmas tree

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hey,

We did this years ago in Java for a bit of fun.

The challenge is to write some VBA to draw a christmas tree. The one that is written in the shortest code, and the most impressive, will win an e-cookie.

I will give it a go, but will fail miserably ;-)

Merry xmas.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Here was my first attempt, while I was waiting for a report to run. :)

Code:
Sub drawTree()
For i = 1 To 10
    With Cells(i, i + 10)
        .Offset(1, -i * 2 + 1).Resize(1, i - 1 + 1).Interior.ColorIndex = 4
        .Offset(1, -i).Resize(1, i - 1 + 1).Interior.ColorIndex = 4
        .Offset(1, -i * 2 + 1).Resize(1, i - 1 + 1).Value = "*"
        .Offset(1, -i).Resize(1, i - 1 + 1).Value = "*"
        .Offset(1, -i * 2 + 1).Resize(1, i - 1 + 1).Font.ColorIndex = 3
        .Offset(1, -i).Resize(1, i - 1 + 1).Font.ColorIndex = 3
    End With
Next i
Cells(12, "I").Resize(3, 3).Interior.ColorIndex = 53
ActiveSheet.UsedRange.ColumnWidth = 1
End Sub
I went with a simplistic approach. And I know I could build the tree with one line instead of 2, but haven't looked at what i need to do yet, or looked at the mathematic equation for it.
 
I think that's quite an elegant solution Rob, and that tree looks good!
 
Create a new workbook and copy this to the first sheet:
Excel Workbook
AB
1YX
200
301
4-101
5-42
6-82
7-33
8-63
9-24
10-44
11-15
12-35
1306
1435
1515
1644
1724
1863
1933
2082
2142
22101
2301
Tree
Excel 2010

Then, in a standard module:
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Sub MakeXmasTree()
    Dim lngLeft As Long, lngWidth As Long
    Dim lngTop As Long, lngHeight As Long
    Dim chtTree As Chart
    Dim rngSource As Range
    Dim lngCounter As Long
    
    With Sheets(1)
        .Columns("E:W").ColumnWidth = 2
        With .Range("F3")
            lngLeft = .Left
            lngTop = .Top
            lngWidth = .Offset(, 17).Left - .Left
            lngHeight = .Offset(20).Top - .Top
        End With
        Set chtTree = .ChartObjects.Add(Left:=lngLeft, Width:=lngWidth, Top:=lngTop, Height:=lngHeight).Chart
        Set rngSource = .Range("A1:B23")
    End With
    
    With chtTree
        With .Axes(xlCategory)
            .Border.LineStyle = xlNone
            .TickLabelPosition = xlNone
            .HasMajorGridlines = False
        End With
        With .Axes(xlValue)
            .Border.LineStyle = xlNone
            .TickLabelPosition = xlNone
            .HasMajorGridlines = False
        End With
        .HasLegend = False
        .HasTitle = True
        With .ChartTitle
            .Text = "merry christmas"
            .Format.TextFrame2.TextRange.Font.Name = "Comic Sans MS"
        End With
        .ChartType = xlXYScatterSmoothNoMarkers
        .SetSourceData Source:=rngSource
        For lngCounter = 3 To 22 Step 2
            With .SeriesCollection(1).Points(lngCounter)
                .MarkerStyle = xlMarkerStyleCircle
                .MarkerSize = 10
            End With
        Next lngCounter
        With .SeriesCollection(1).Points(12)
            .MarkerStyle = xlMarkerStyleDiamond
            .MarkerSize = 15
        End With
    End With
    
    With Range("E2:W2,E3:E22,E23:W23,W3:W22")
        .Value = Chr(151)
        .Font.Name = "Wingdings 2"
        .BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
        .Font.ColorIndex = 33
        For lngCounter = 1 To 3
            With .FormatConditions.Add(Type:=xlExpression, Formula1:="=RANDBETWEEN(1,3)=" & lngCounter)
                .Font.ColorIndex = 20 + lngCounter
            End With
        Next lngCounter
    End With
    
    Do
        Calculate
        Sleep 300
        DoEvents
    Loop
End Sub

Close the VBE and then run the macro (ALT+F11, choose 'MakeXmasTree' and hit Run)

To kill the macro, hit the ESCAPE key, or CTRL+Break!
 
Very nice Jon! I was going to add flashing bulbs to my tree, but decided not to in my original version. I hope to get back to it here sometime today to reduce the code and have it only need one line per action.
 
Ok I have expanded some and reduced the statements into one line:

Code:
Sub drawTree()
For i = 1 To 18
    With Cells(i, i + 10)
        If i < 4 Then
            .Offset(0, -i * 2 + 1).Resize(1, i * 2 - 1).Interior.ColorIndex = 6
            .RowHeight = 7.5
        ElseIf i < 6 Then
            .Offset(0, (i Mod 4) - i - 1).Resize(1, Abs(i * 5 - i ^ 2 - 1)).Interior.ColorIndex = 6
            .RowHeight = 7.5
        ElseIf i < 16 Then
            .Offset(0, (-i + 5) * 2 - 4).Resize(1, (i - 5) * 2 - 1).Interior.ColorIndex = 4
            .Offset(0, (-i + 5) * 2 - 4).Resize(1, (i - 5) * 2 - 1).Value = "*"
            .Offset(0, (-i + 5) * 2 - 4).Resize(1, (i - 5) * 2 - 1).Font.ColorIndex = 3
        Else
            .Offset(0, -i - 1).Resize(1, 3).Interior.ColorIndex = 53
        End If
    End With
Next i
ActiveSheet.UsedRange.ColumnWidth = 1
End Sub
 
Aww... jon's code blows up on Excel 2003. Can't set the LineStyle properties.
 
Rubbish... I can't 'judge' Jon's code as I am using 2003 at work!
 

Forum statistics

Threads
1,222,629
Messages
6,167,188
Members
452,103
Latest member
Saviour198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top