我上的問題與循環工作通過一定數目的列和陣列式中粘貼。對於每一個新列,我都必須更改公式以反映該列地址。但是,當我嘗試運行它時,我一直得到1004(範圍類失敗的選擇方法)錯誤。以下是我已經寫了:遍歷列和粘貼的陣列式
Sub Testlee()
Dim i As Integer
Dim LastColumn As Long
Dim rng As Range
Dim colStr As String
LastColumn = 10
For i = 1 To LastColumn
colStr = Replace(Split(Columns(i).Address, ":")(0), "$", "")
ThisWorkbook.Sheets("Data Validation").Range(colStr & "2:" & colStr & "500").Select
Selection.FormulaArray = "=IF(LEN(Agent1!" & colStr & "2:" & colStr & "500) + LEN(Agent2!" & colStr & "2:" & colStr & "500) = 0,"""",(IF(Agent1!" & colStr & "2:" & colStr & "500=Agent2!" & colStr & "2:" & colStr & "500, ""YES"", Agent1!" & colStr & "2:" & colStr & "500&""||""&Agent2!" & colStr & "2:" & colStr & "500)))"
Next i
End Sub
任何幫助,將不勝感激:)
更新:我能得到它的工作使用兩種方法的結合。以下是可用的代碼:
For i = 1 To LastColumn
colStr = Replace(Split(Columns(i).Address, ":")(0), "$", "")
With ThisWorkbook.Sheets("Data Validation").Range("A2:A500")
ThisWorkbook.Sheets("Data Validation").Range(colStr & "2:" & colStr & "500").FormulaArray = "=IF(LEN(Agent1!RC:R[498]C)+LEN(Agent2!RC:R[498]C) = 0,"""",(IF(Agent1!RC:R[498]C=Agent2!RC:R[498]C, ""YES"", Agent1!RC:R[498]C&""||""&Agent2!RC:R[498]C)))"
End With
Next i
感謝大家的幫助!
你用R1C1參考風格? – psychicebola
您只能在活動工作表中選擇單元格。你不需要選擇在所有[鏈接](http://stackoverflow.com/questions/10714251) – BrakNicku
BrakNicku嗨,我試着不使用選擇,只是這樣做有它的表(範圍).FormulaArray =「公式」但它也沒有效果。 – NLourme