vba比较行到列

时间:2016-02-24 14:41:24

标签: excel vba excel-vba comparison

我有一个电子表格,其中A列信息从第6行开始,我从列表中获得了此列表中的唯一值作为第1行顶部的标题。我想要做的是列A中的单元格是否匹配标题行,将C列单元格中的信息复制到相应的标题。

原始格式 enter image description here

希望格式 enter image description here

这是我尝试过的代码,但它没有给我预期的结果:

For i = 6 To lastRow
    For j = 4 To lastColumn
      If Cells(i, 1) = Cells(1, j) Then
        wksActRawData.Cells(i, 3).Value = wksActRawData.Cells(i, j).Value
      End If
    Next j
  Next i

1 个答案:

答案 0 :(得分:0)

假设您确实编写了lastRow和lastColumn函数,代码中唯一的错误就是获取单元格值的数据

 If Cells(i, 1) = Cells(1, j) Then
    wksActRawData.Cells(i, 3).Value = wksActRawData.Cells(i, j).Value
 End If

这应该是另一种方式,另外我还要添加一行来清除旧价格,如下所示

  If Cells(i, 1) = Cells(1, j) Then
    Cells(i, j).Value = Cells(i, 3).Value
    Cells(i, 3).ClearContents
  End If

它确实以这种方式为我工作