我遇到了一些问题,这使我的齿轮磨了一段时间。 描述真的很简单。我想在两个单元格之间(以某种方式标记)“绘制单元格线”。就像标记2个单元格一样,然后单击按钮,它会在这两点之间创建带有填充单元格的线。
我仍然不知道如何填充这两点之间的每个单元格。我有一些数学思想,但无法用代码使它们正常工作。
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