我曾經有一個線EXCEL VBA申請中所定義或對象定義
Set rRng1 = Worksheets("Sheet1").Range("I2:J20")
錯誤「1004」,但由於細胞的範圍可從文件改變到文件(I2是恆定雖然),我發現了一種簡單的自動化方法。
從該範圍的第一小區是總是I2,和最後一個單元爲J(last_pair_cell)
我認爲使用範圍(將細胞(2,9),將細胞(last_pair_cell),10)將到伎倆,但我得到錯誤1004 ...
這是整個代碼:
Sub LoopRange2()
Dim rCell1 As Range
Dim rCell2 As Range
Dim rRng1 As Range
Dim rRng2 As Range
Dim nCol As Integer 'Finds week column to insert values
nCol = Worksheets("Clube").Range("P69").Value + 5
'Find number of pairs that played the tournment
Dim last_pair_cell As Integer
Dim rngX As Range
Set rngX = Worksheets("Sheet_CSV").Range("A1:A10000").Find("Board", lookat:=xlPart)
If Not rngX Is Nothing Then
last_pair_cell = rngX.Row - 1
End If
**Set rRng1 = Worksheets("Sheet_CSV").Range(Cells(2, 9), Cells(last_pair_cell, 10))**
'Set rRng1 = Worksheets("Sheet1").Range("I2:J20")
Set rRng2 = Worksheets("Clube").Range("C3:C80") 'IF ERROR CHANGE C80 TO C69
For Each rCell1 In rRng1.Cells
For Each rCell2 In rRng2.Cells
If rCell2.Value = rCell1.Value Then
Worksheets("Clube").Cells(rCell2.Row, nCol).Value = Worksheets("Sheet1").Cells(rCell1.Row, 6).Value
End If
Next rCell2
Next rCell1
End Sub
在錯誤行之前使用'Debug.print last_pair_cell'來檢查值 – newguy
您聲明'*(I20 is constant is though)*'。你的意思是'I2'還是'J20'? – Jeeped
I2 :)感謝您的注意 –