I have some problem which is grinding my gears for some time now. Description is really simple. I would like to "draw cells line" between two cells (which are somehow marked). Like you mark 2 cells, then click button and it creates line with filled cells between these two points.
I still cant figure out how to fill every cell between these two points. I had some mathematical ideas but couldn't get them in code to work properly.
Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim cred As Long
Dim cgreen As Long
Dim startx As Integer
Dim starty As Integer
Dim endx As Integer
Dim endy As Integer
Dim koef As Single
Dim arr(1 To 20, 1 To 20) As Boolean
Dim st As Integer
cgreen = Range("X5").Interior.Color
cred = Range("X6").Interior.Color
'Start and End
For i = 1 To 20
For j = 1 To 20
If Cells(i, j).Interior.Color = cred Then
endx = j
endy = -i
ElseIf Cells(i, j).Interior.Color = cgreen Then
startx = j
starty = -i
End If
Next j
Next i
koef = (endy - starty) / (endx - startx)
If starty < endy Then
st = 1
Else
st = -1
End If
For i = startx To endx
For k = starty To endy Step st
l = -k / koef
j = starty + koef * (i - startx)
l = starty + koef * (i + 1 - startx)
If k >= j Then
arr(i, -j) = True
End If
Next k
Next i
For i = 1 To 20
For j = 1 To 20
If arr(j, i) = True Then
Cells(i, j).Interior.Color = RGB(255, 255, 0)
End If
Next j
Next i