每當我運行此代碼時,我總是收到錯誤。該錯誤是1004運行時錯誤。請幫我弄清楚我的代碼出錯了。我完全新的VBA,但我不知道如何使用Python和C.VBA項目中的運行時錯誤
Option Explicit
Sub Experiment()
Dim m1 As Worksheet
Set m1 = ThisWorkbook.Worksheets("Sheet1")
Dim col As Integer
Dim row As Integer
Dim initial As Double
Dim s1 As Double
Dim s1_pos As Integer
Dim s2 As Double
Dim s2_pos As Integer
Dim min As Double
Dim candidate As Double
Dim temp_swap As Double
Dim r As Integer
col = 2
'For col = 2 To 18 Step 3
For row = 5 To 47 Step 2
initial = m1.Cells(row, col).Value
s1 = m1.Cells(row + 1, col).Value
s1_pos = row + 1
min = Abs(36 - (initial + s1))
r = row + 1
Do While r < 49
s2 = m1.Cells(r, col).Value
candidate = Abs(36 - (initial + s2))
If candidate < min Then
min = candidate
s2_pos = r
End If
r = r + 1
Loop
temp_swap = s1
m1.Cells(s1_pos, col).Value = s2
m1.Cells(s2_pos, col).Value = temp_swap
Next row
End Sub
請指定@哪一行錯誤被拋出?如果你可以分享excel表單圖片,那會很棒 – Siva
@Kurst你想達到什麼效果?因爲你的'While'循環似乎並不理想,你是否在尋找一個取決於動態範圍的最小值?在這種情況下,你可以使用'匹配'與'Min' –
@Siva m1.Cells(s2_pos,col).Value = temp_swap,對不起,代碼段工具不工作:'( – Kurst