需要循环和修改Excel工作表中的单元格…我从哪里开始?

我正在与什么工作

我需要修改一个文件(从salesforce导出),其中行有一个唯一的ID,一个名称和一个列,这个表中的另一行的唯一ID,将其标识为该项目的“子”。

  1. ID( columnA
  2. 名称( columnB
  3. ParentID( columnC

未受影响的数据的图像:

在这里输入图像说明

我需要完成什么

我的目标是遍历整个电子表格(55,000行),并将ParentID更改为父级的Name列中的值。

我的伪代码解决scheme

foreach row starting from the top { varA = [current row number for this loop]; varX = [value in varA:columnC]; if (varX == [regex value]) { foreach row starting from the top { varB = [current row number for this loop]; if ([value in varB:columnA] == varX) { foreach row starting from the top { varC = [current row number for this loop]; if (varC:columnC = varB:columnA) { [varC:columnC] = [varB:columnB]; } } break second foreach loop; } } } } 

手动修复后数据的图像:

在这里输入图像说明

我到目前为止所做的研究

我正在和excel合作,我发现了一些资源,讨论如何创build一个新的macros,有些甚至有一些例子,但是我不确定这将是一个合适的地方。 我也有一个朋友build议用pandas来修改这个文件比较好,但是这不是我所熟悉的。

我的问题:

我从哪说起呢? 什么是编写脚本来修改所有这些的正确工具?

另外,如果可能的话,当你使用适当的工具来完成这项工作时,你能build议一个资源作为参考吗?

改变这一点,

在这里输入图像说明

用这个代码,

 Option Explicit Sub wqweqrteq() Dim d As Long, dict As Object, vals As Variant Set dict = CreateObject("Scripting.Dictionary") With Worksheets("salesforce") With .Range(.Cells(2, "A"), .Cells(.Rows.Count, "C").End(xlUp)) vals = .Value2 End With For d = LBound(vals, 1) To UBound(vals, 1) dict.Item(vals(d, 1)) = vals(d, 2) Next d For d = LBound(vals, 1) To UBound(vals, 1) If dict.exists(vals(d, 3)) Then vals(d, 3) = dict.Item(vals(d, 3)) End If Next d With .Range(.Cells(2, "A"), .Cells(.Rows.Count, "C").End(xlUp)) .Value = vals End With End With End Sub 

..进入这个。

在这里输入图像说明

以下For循环将解决您的问题:

 Sub UpdateID() Dim lastrow As Long Dim x As Long, y As Long, z As Long Dim ParentName As String lastrow = Cells(Rows.Count, "A").End(xlUp).Row For x = 2 To lastrow If Left(Cells(x, 3), 2) = 0 Then For y = 2 To lastrow If Cells(x, 3).Value = Cells(y, 1).Value Then ParentName = Cells(y, 2).Value Exit For End If Next y End If For z = 2 To lastrow If Cells(z, 3).Value <> "" And Cells(x, 3).Value = Cells(z, 3).Value Then Cells(z, 3).Value = ParentName End If Next z Next x End Sub