简体   繁体   English

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

[英]Copy range from column A and write to column B

I have a range in column A and I want to write for each element of range A something in Cell B. I am using this code but it doesn't seem to work: 我在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

What I want to write in column B. The last cell in A is 1887: 我想在B列中写什么。A中的最后一个单元格是1887:

在此处输入图片说明

Can anyone assist please? 有人可以帮忙吗?

Set rng2 = Range("A:A" & LastRow).Select 设置rng2 = Range(“ A:A”&LastRow)。选择

this should be Set rng2 = Range("A 1 :A" & LastRow) 应该设置为rng2 = Range(“ A 1 :A”&LastRow)

and your offset is the cell underneath, should be (0,1) 并且您的偏移量是下方的单元格,应为(0,1)

I corrected your code, give it a try : 我更正了您的代码,尝试一下:

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

Avoid using Select as much as you can, it is a source of trouble. 避免尽可能多地使用“选择”,这会带来麻烦。 In this code, you dont need to use it. 在此代码中,您不需要使用它。 I suppose that isOK is a user-defined function (UDF): 我想isOK是一个用户定义的函数(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

Try that better with normal excel formulas: 使用普通的excel公式更好地尝试:

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

copy that into every cell in column B and you will get your expected result ;) 将其复制到B列的每个单元格中,您将获得预期的结果;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM