从列A复制范围并写入列B.

我在列A中有一个范围,我想在单元格B中为范围A的每个元素编写一些东西。我正在使用这段代码,但它似乎并不工作:

LastRow = Cells(Rows.Count, "A").End(xlUp).Row Set rng2 = Range("A:A" & LastRow).Select For Each Cell In rng2 Cell.Offset(1, 0).FormulaR1C1 = "=isOK" Next Cell 

我想写在B列中的最后一个单元格是1887年:

在这里输入图像说明

任何人都可以协助吗?

我更正了你的代码,试试看:

 Dim Cell as Range, LastRow As Long LastRow = Cells(Rows.Count, "A").End(xlUp).Row Set rng2 = Range("A2:A" & LastRow) For Each Cell In rng2 If Cell.Value <> vbNullString Then Cell.Offset(0, 1) = "isOK" Next Cell 

设置rng2 =范围(“A:A”和LastRow)。select

这应该是设置rng2 =范围(“A 1 :A”&LastRow)

而你的偏移是下面的单元格,应该是(0,1)

避免使用尽可能多的Select,这是一个麻烦来源。 在这个代码中,你不需要使用它。 我想这是一个用户定义的函数(UDF):

  LastRow = Cells(Rows.Count, "A").End(xlUp).Row Set rng2 = Range("A2:A" & LastRow) For Each Cell In rng2 If not IsEmtpy(Cell) Then Cell.Offset(1, 0).Formula = "=isOK()" Next Cell 

使用正常的Excel公式来更好地尝试:

 =if(lookup(A1;C1:Z20)=0;"isOK";"notOK") 

复制到列B中的每个单元格,您将得到您的预期结果;)