【问题标题】:Replacing excel data using another spreadsheet使用另一个电子表格替换 Excel 数据
【发布时间】:2012-03-06 05:44:25
【问题描述】:

我有两个 Excel 电子表格,一个包含不同人的数据,而另一个更多的是索引。

“索引”电子表格的布局如下(作为示例);

      A       B
1 [person:][pID: ]
2 [Mark   ][1    ]
3 [James  ][2    ]
4 [John   ][3    ]
5 [David  ][4    ]
n [ ...   ][n    ]

“数据”电子表格的布局如下(作为示例);

      A
1 [pID:   ][ ...
2 [David  ][ ...
3 [David  ][ ...
4 [David  ][ ...
5 [James  ][ ...
6 [Mark   ][ ...
7 [David  ][ ...
n [ ...   ][ ...

我想做的是将“数据”电子表格中的 A 列替换为“索引”电子表格的相应索引,可能使用 VBA。

结果会是这样的;

      A
1 [pID:   ][ ...
2 [4      ][ ...
3 [4      ][ ...
4 [4      ][ ...
5 [2      ][ ...
6 [1      ][ ...
7 [4      ][ ...
n [ ...   ][ ...

我见过一些使用 switch 语句的静态方法,但我真的希望能够从另一个电子表格中导入数据。 我尽量不使用复制粘贴和“全部替换”。

谢谢!

【问题讨论】:

  • 在没有 VBA 的情况下直接从 Excel 尝试VLOOKUP()

标签: vba excel


【解决方案1】:

试试这个:

Sub HTH()

    With Sheets("Data")
        .Columns(2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Cells(1, "B").Value = .Cells(1, "A").Value
        With .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Offset(, 1)
            .Formula = "=VLOOKUP(A2,Index!A:B,2,FALSE)"
            .Value = .Value
        End With
        .Columns(1).Delete
    End With

End Sub

解释:

Sub HTH()

    With Sheets("Data")
        '// Insert a new column with same format
        .Columns(2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        '// Copy the header
        .Cells(1, "B").Value = .Cells(1, "A").Value
        '//  Work with the used range of column A 
        With .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Offset(, 1)
            '// Use a formula to replace the names with corresponding values
            .Formula = "=VLOOKUP(A2,Index!A:B,2,FALSE)"
            '// Replace formula with value
            .Value = .Value
        End With
        '//  Delete the old column
        .Columns(1).Delete
    End With

End Sub

注意:

就我个人而言,我会保留原始数据表,并使用演示表查找数据表和索引表,然后按照您的需要进行显示。然后你不一定需要代码,如果你仍然想使用代码,那么代码会简单得多。您需要添加代码来关闭屏幕更新和使用错误处理等。

ja72 解决方案的替代方案:

Sub HTH()
    Dim vNames As Variant, vResult As Variant
    Dim lLoop As Long

    vNames = Sheets("Index").UsedRange.Columns(1).Resize(, 2).Value2
    vResult = Sheets("Data").UsedRange.Columns(1).Value2

    With CreateObject("Scripting.Dictionary")
        For lLoop = 2 To UBound(vNames, 1)
            .Add vNames(lLoop, 1), vNames(lLoop, 2)
        Next lLoop

        For lLoop = 2 To UBound(vResult, 1)
            vResult(lLoop, 1) = .Item(vResult(lLoop, 1))
        Next lLoop

        Sheets("Data").Range("A1").Resize(UBound(vResult, 1), 1).Value = vResult
    End With

End Sub

安静地相信它也更快;)

【讨论】:

    【解决方案2】:

    用 VBA 试试这个:

    Public Sub FindPID()
        Dim i As Integer, N As Integer
        Dim r_index As Range
        ' Find start of index
        Set r_index = Sheets("Index").Range("A2")
        ' Count rows in index
        N = r_index.Worksheet.Range(r_index, r_index.End(xlDown)).Rows.Count
        ' Expand range with all values
        Set r_index = r_index.Resize(N, 2)
        Dim vals As Variant
        ' Move values into array
        vals = r_index.Value2
        ' Create a lookup collection
        Dim lup As New Collection
        For i = 1 To N
            ' Add each pID to collection with name as key
            lup.Add vals(i, 2), vals(i, 1)
        Next i
    
        Dim r_data As Range, M As Integer
        ' Find start of data
        Set r_data = Sheets("Data").Range("A2")
        ' Count rows in data
        M = r_data.Worksheet.Range(r_data, r_data.End(xlDown)).Rows.Count
        ' Expand range with all values
        Set r_data = r_data.Resize(M, 1)
        ' Move values into array
        vals = r_data.Value2
        For i = 1 To M
            ' Replace first column with lookup pID
            vals(i, 1) = lup(vals(i, 1))
        Next i
        ' Assign values from array back to cells.
        r_data.Value2 = vals
    End Sub
    

    这将是最快的。

    【讨论】:

    • "这将是最快的。"听起来像是一个挑战;)
    • 请注意,此例程中单元格的数量是未知的,需要计算以减慢代码速度。否则,它只是内存循环,根据我的经验,它的速度非常快。
    • 很高兴看到你看到我扭曲的幽默! +1 设置挑战。 ;)
    【解决方案3】:

    我不认为 VBA 是解决此问题的最佳方法。一个简单的 VLOOKUP 将完全完成您需要做的事情。

    =VLOOKUP(Data!$A1, Index!$A$2:$A$5, 1, FALSE)
    

    会给你想要的结果。

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多