Hi!
I'm using the following code to connect the cells that contain the letter "o" and nothing else.
I have a range that is a homemade graph and I want it to all be connected and made pretty.
The following code works when there are no gaps in the lines, however, if there is a gap in the lines for some reason the line will stop.
How can i modify this to draw the lines from each "o" to the next even if there is a gap?
(I'm pretty sure the "If rngFrom Is Nothing" then line is screwing me up but I'm stumped....)
I'm using the following code to connect the cells that contain the letter "o" and nothing else.
I have a range that is a homemade graph and I want it to all be connected and made pretty.
The following code works when there are no gaps in the lines, however, if there is a gap in the lines for some reason the line will stop.
How can i modify this to draw the lines from each "o" to the next even if there is a gap?
(I'm pretty sure the "If rngFrom Is Nothing" then line is screwing me up but I'm stumped....)
Code:
Private Sub CommandButton3_Click()
'figure out if the cells have what i need
'if so call the draw function to connect them
Dim rngTo As Range
Dim rngFrom As Range
Dim rngCol As Range
For Each rngCol In Range("$B$3:$AF$33").Columns
For Each rngTo In rngCol.Cells
If rngTo = "o" Then
If rngFrom Is Nothing Then
Set rngFrom = rngTo
Else
DrawLine rngTo, rngFrom
End If
Exit For
End If
Next
Set rngFrom = rngTo
Next
End Sub
Code:
Sub DrawLine(FromCell As Range, ToCell As Range)
'draw lines! and make them pretty
With FromCell.Parent.Shapes.AddLine(1, 1, 1, 1)
If FromCell.Left > ToCell.Left Then
.Left = ToCell.Left + (ToCell.Width / 2)
.Width = (FromCell.Left + (FromCell.Width / 2)) - .Left
Else
.Left = FromCell.Left + (FromCell.Width / 2)
.Width = (ToCell.Left + (ToCell.Width / 2)) - .Left
End If
If FromCell.Top > ToCell.Top Then
.Top = ToCell.Top + (ToCell.Height / 2)
.Height = (FromCell.Top + (FromCell.Height / 2)) - .Top
Else
.Top = FromCell.Top + (FromCell.Height / 2)
.Height = (ToCell.Top + (ToCell.Height / 2)) - .Top
.Flip msoFlipVertical
End If
.Line.Weight = 2
.Line.ForeColor.SchemeColor = 3
End With
End Sub