I'm pretty new to VBA but thought I'd have a bash at an old puzzle I used to play on my first Amstrad.
Heres how the story goes.
My wife told me to put 2 christmas trees up in out living room. There isn't much space in our living room. I placed the red tree on the left and the blue tree on the right.
Run once on blank sheet to see how it looked.
Doesn't that look nice...
Well I thought so. My wife had other ideas. She wanted the trees the other way round. Because of the size of the trees (9 feet tall each) and the weight of each section it was not possible to just slide them accross the room. I had to move one section at a time. But a larger section will break a smaller section so I could not place smaller sections onto larger sections.
place in Sheet1
Click a tree section to select it - It must be at the top of a pile.
Select an empty cell to move it to the cell (must be top of a pile, blank or the bottom of the pile and smaller than the tree piece it is place onto)
You can mix the red and blue trees but they must finish all the same colour with the Red on the right and blue on the left.
The Black line shows my total floor space. you cannot use any cells outside this.
How many moves can you do it in.
Comments on my code welcome / appreciated.
What breaks it? What improvements can be made?
Heres how the story goes.
My wife told me to put 2 christmas trees up in out living room. There isn't much space in our living room. I placed the red tree on the left and the blue tree on the right.
Run once on blank sheet to see how it looked.
Code:
Sub First_Build()
Range("B7").Value = "1"
Range("B8").Value = "22"
Range("B9").Value = "333"
Range("B10").Value = "4444"
Range("B11").Value = "55555"
Range("B12").Value = "666666"
Range("B13").Value = "7777777"
Range("B14").Value = "88888888"
Range("B15").Value = "999999999"
Range("B7:B15").Select
Selection.Copy
Range("F7").Select
ActiveSheet.Paste
Columns("B:F").Select
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Columns("B:F").ColumnWidth = 11.86
Range("B7:B15").Font.ColorIndex = 3
Range("F7:F15").Font.ColorIndex = 5
Range("B16:F16").Interior.ColorIndex = 56
Range("B16").Value = "X"
Range("C16").Value = "X"
Range("D16").Value = "X"
Range("E16").Value = "X"
Range("F16").Value = "X"
Range("A3").Value = "Moves"
Range("B3").Value = 0
End Sub
Doesn't that look nice...
Well I thought so. My wife had other ideas. She wanted the trees the other way round. Because of the size of the trees (9 feet tall each) and the weight of each section it was not possible to just slide them accross the room. I had to move one section at a time. But a larger section will break a smaller section so I could not place smaller sections onto larger sections.
place in Sheet1
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "X" Then
Range("D2").Value = "Thats the Base"
Exit Sub
End If
If ActiveCell.Offset(-1) <> "" Then
Range("D2").Value = "Must Select the top Block"
Exit Sub
End If
If ActiveCell <> "" Then
ActiveCell.Cut
Range("A1").Value = ActiveCell.Value
Range("D2").Value = ""
Else
If Application.CutCopyMode <> False Then
If ActiveCell.Offset(1).Value = "" Then
Range("D2").Value = "Must be placed on another block Or the Base"
Exit Sub
Else
If Range("A1").Value >= ActiveCell.Offset(1).Value Then
Range("D2").Value = "Must place on smaller block"
Exit Sub
Else
ActiveSheet.Paste
Application.CutCopyMode = False
Range("B3").Value = Range("B3").Value + 1
End If
End If
End If
End If
End Sub
Click a tree section to select it - It must be at the top of a pile.
Select an empty cell to move it to the cell (must be top of a pile, blank or the bottom of the pile and smaller than the tree piece it is place onto)
You can mix the red and blue trees but they must finish all the same colour with the Red on the right and blue on the left.
The Black line shows my total floor space. you cannot use any cells outside this.
How many moves can you do it in.
Comments on my code welcome / appreciated.
What breaks it? What improvements can be made?