当他们有一个等效的识别单元格时,如何用同一个单元格replace多个数据单元格?

所以我对VBA很新,所以我为我可怕的编码道歉。 我一直在寻找几个小时才能find这个看似简单的问题的答案,我可能已经遇到了这个问题,但只是不明白它是什么。

基本上我想做的是使用一个识别variables来分类一组variables。 我所看到的数据看起来像是专辑中的顶级图像,而我所需要的数据看起来像是专辑中的第二张图像(除了我有超过20000个点)。 如果有人能帮助我,我将非常感激。

在这里输入图像描述

在这里输入图像说明

到目前为止我有:

Sub Try_3() SessionID_Cell = Value.Range("D3:D10047") SessionID_Change = Value.Range("I2:I5748") Name_Change = Value.Range("J2:J5748") For Each SessionID_Cell In Range("D2:D10047") If SessionID_Cell = SessionID_Change Then SessionID_Cell.Value = Replace(SessionID_Cell, SessionID_Change, Name_Change) Else End If Next SessionID_Cell End Sub 

我觉得macros是有点矫枉过正….为什么不使用一个简单的VLOOKUP?

 =VLOOKUP(B2,$C$2:$D$4,2,FALSE) 

这是参数意味着下面的例子

B2是要search的值

$C$2:$D$4是要查看的表的范围

2是返回值的上述范围的列索引

FALSE – 完全匹配

在这里输入图像说明

尝试这个:

 Sub test() Dim idcs As Range, idc As Range Dim rng As Range, idns As Variant Dim i As Long With Sheet1 '~~> Sheet where your data reside, change to suit Set idcs = .Range("C2", .Range("C" & .Rows.Count).End(xlUp)) Set rng = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)) '~~> pass name id's to array idns = Application.Transpose(rng) End With With CreateObject("Scripting.Dictionary") '~~> Dump id's and equivalent city on Dictionary For Each idc In idcs If Not .Exists(idc.value) Then .Add idc.Value, idc.Offset(0, 1).Value End If Next '~~> replace all name id's with equivalent city '~~> using the info dumped in the Dictionary For i = LBound(idns) To UBound(idns) If .Exists(idns(i)) Then idns(i) = .Item(idns(i)) Next '~~> Change this part if you want to dump new data in another sheet rng = Application.Transpose(idns) End With End Sub 

这将replace实际城市的所有名称标识符。
就像在C和D所设定的条件下立即发现和replace一样。
如果没有匹配的城市标识符,它将保持原样。
这是你想要的吗?