我有一个循环2到~28000和内在循环,大约有100 000次迭代。据我所知,有一些限制,因为我得到溢出错误。
如果有帮助,则有代码
Sub slp()
Dim sx As Long
Dim sy As Long
Dim sr As Long
Dim att As Long
'SLP rindu skaits 23845
For sr = 2 To 23845
lapa = Sheets("SLP").Cells(sr, 5).Value
sx = Sheets("SLP").Cells(sr, 6).Value
sy = Sheets("SLP").Cells(sr, 7).Value
'sub metr
Dim attn As Long
Dim x1 As Long
Dim x2 As Long
Dim y1 As Long
Dim y2 As Long
Dim s1 As Long
Dim s2 As Long
Dim a12 As Long
Dim c1 As Long
Dim c2 As Long
Dim lr As Long
Dim attn1 As Long
Dim slin As Long
Dim rindas As Long
attn = 1000000000
'liniju rindas
rindas = Sheets(lapa).Cells(Sheets(lapa).Rows.Count, "A").End(xlUp).Row
For lr = 2 To rindas
'rindas ~100 000
'koordinasu kolonnas
x1 = Sheets(lapa).Cells(lr, 3).Value
y1 = Sheets(lapa).Cells(lr, 4).Value
x2 = Sheets(lapa).Cells(lr, 5).Value
y2 = Sheets(lapa).Cells(lr, 6).Value
slin = Abs((y2 - y1) * (sx - x1) - (x2 - x1) * (sy - y1))
s1 = Sqr((x1 - sx) ^ 2 + (y1 - sy) ^ 2)
s2 = Sqr((x2 - sx) ^ 2 + (y2 - sy) ^ 2)
a12 = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
c1 = (a12 ^ 2 + s2 ^ 2 - s1 ^ 2) / (2 * a12 * s2)
c2 = (a12 ^ 2 + s1 ^ 2 - s2 ^ 2) / (2 * a12 * s1)
'ja abu < cos pozitivi, tad punkts ir kaut kur blakus linijai nevis tuvāk kadam tas galapunktam
attn1 = WorksheetFunction.Min(s1, s2)
If c1 >= 0 And c2 >= 0 Then attn1 = slin
'meklejam mazako attalumu lidz visam linijam
att = WorksheetFunction.Min(attn, attn1)
attn = att
Next lr
Sheets("SLP").Cells(sr, 11) = att
Next sr
End Sub
一开始,我只是总结了一些值,如果内部循环没有输入,那么它就可以了。也许有人可以建议对此代码进行一些优化。也许我做的事情不正确,因为我在Excel VBA中的经验很短。
谢谢!