Steam Leaf Plot in Excel

  • Thread starter Thread starter Guest
  • Start date Start date
Ben,
Try this as a starter:

Sub StemAndLeafPlot()

Dim inRng As Range, cell As Range
Dim ws1 As Worksheet, ws2 As Worksheet
Dim MaxVal As Integer, MinVal As Integer, lIndex As Integer, uIndex As Integer
Dim i As Integer, n As Integer, j As Integer

Set ws1 = Worksheets("sheet1")
With ws1
Set inRng = .Range("a1:a" & .Cells(Rows.Count, "A").End(xlUp).Row)
End With

MaxVal = Application.Max(inRng)
MinVal = Application.Min(inRng)

lIndex = Int(MinVal / 10) * 10
uIndex = (Int(MaxVal / 10)) * 10

Set ws2 = Worksheets("sheet2")
ws2.Cells.ClearContents

i = 0
For n = lIndex To uIndex Step 10 ' Write Stems
i = i + 1
ws2.Cells(i, 1) = n
Next n

For Each cell In inRng ' Write Leaves
i = Int((cell.Value - lIndex) / 10) + 1
n = cell.Value Mod 10
j = Application.CountA(ws2.Rows(i)) + 1
ws2.Cells(i, j) = n
Next cell

End Sub

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top