vba 如何根据另一个单元格值修改一个单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5735749/
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 13:04:30  来源:igfitidea点击:

how to modify a cell value based on another cell value

excel-vbavbaexcel

提问by user717787

A       B      C 
aa1     b1     ac1
aa2     b2     bd2
aa3     b3     ae3
aa4     b1     bc4
aa5     b2     ad5
aa6     b3     be6

now in the above sheet, i need to change the value like this..

现在在上面的工作表中,我需要像这样更改值..

A       B      C 
xy1     b1     ac1
aa2     b2     bd2
aa3     b3     ae3
aa4     b1     bc4
aa5     b2     ad5
pq6     b3     be6

as shown in the above part, based on the cells in the column "C" i need to change values in the column A for each cells of it..

如上部分所示,基于“C”列中的单元格,我需要为它的每个单元格更改 A 列中的值。

Kindly help me...

请帮助我...

回答by Jose Rui Santos

You need to specify a formula in first cell of A and then simply propagate it across all the column.

您需要在 A 的第一个单元格中指定一个公式,然后简单地将其传播到所有列。

  1. In column A, select the cell with value "aa1"
  2. Press F2 to edit that cell
  3. Type =CONCATENATE("a",$C1)and press Enter
  4. Press Ctrl+C to copy the cell you just edited
  5. With the mouse (or with keys shift+down) select the next two cells in column A (aa2 and aa3)
  6. Press Ctrl+V
  1. 在 A 列中,选择值为“aa1”的单元格
  2. 按 F2 编辑该单元格
  3. 键入=CONCATENATE("a",$C1)并按 Enter
  4. 按Ctrl+C复制刚才编辑的单元格
  5. 使用鼠标(或使用 shift+down 键)选择 A 列中的下两个单元格(aa2 和 aa3)
  6. 按 Ctrl+V

In VBA, you need something like this:

在 VBA 中,你需要这样的东西:

Sub Button1_Click()
    For Each cell In ActiveSheet.Range("A1:A3").Cells
        cell.Value = "a" + ActiveSheet.Cells(cell.row, 3)
    Next
End Sub

You need to specify the range you want to change, in this case from A1 to A3 and then simply change the value for each cell as the concatenation of "a" with the cell value on same row but column 3 (C)

您需要指定要更改的范围,在这种情况下从 A1 到 A3,然后只需将每个单元格的值更改为“a”与同一行但第 3 (C) 列的单元格值的连接