填充2个单元格之间的单元格

时间:2019-09-25 16:00:40

标签: excel vba

我遇到了一些问题,这使我的齿轮磨了一段时间。 描述真的很简单。我想在两个单元格之间(以某种方式标记)“绘制单元格线”。就像标记2个单元格一样,然后单击按钮,它会在这两点之间创建带有填充单元格的线。

Something like this

我仍然不知道如何填充这两点之间的每个单元格。我有一些数学思想,但无法用代码使它们正常工作。

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

1 个答案:

答案 0 :(得分:1)

您可以在图像上找到相交点 (img1),将它们四舍五入。这些是您的细胞坐标以填充颜色 甚至更好:您可以找到每个像元边界的交集(img2)并使用分数值(img3)来获得像素强度 这是实现的目标:img4