为范围 VBA Excel 中的每个单元格设置偏移值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18233206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:45:30  来源:igfitidea点击:

Set an offset value for each cell in a range VBA Excel

vbaexcel-vbaexcel

提问by SpenceLegend

I am trying to compare a value to each cell in a range. Once there is a match, I want to set another variable to old the value that is offset 3 columns over.

我正在尝试将一个值与范围内的每个单元格进行比较。一旦匹配,我想将另一个变量设置为旧的偏移 3 列的值。

For Each i In Worksheets("SFDC Input").Range("D2:D20").Cells
    If ProductFamily = i Then
        i.Offset(0, -3).Value = ID
    End If
Next

ProductFamily is the value I am comparing to i. I want ID to be the value holder.

ProductFamily 是我与 i 比较的值。我希望 ID 成为价值持有者。

回答by Brian Phillips

It looks as though your assignment (=) is backwards. You're setting i.Offset(0, -3).Valueto IDinstead of setting IDto the value.

看起来你的作业 ( =) 是倒退的。您设置i.Offset(0, -3).ValueID而不是设置ID为值。

Try the following:

请尝试以下操作:

ID = i.Offset(0, -3).Value

From MSDN:

MSDN

The = operator assigns the value on its right to the variable or property on its left.

= 运算符将其右侧的值分配给其左侧的变量或属性。