2015-11-04 149 views
0

这是VBA宏的一部分,它改变从在线商店导出的订单信息,稍后导入到货运经理。'For Each'循环没有循环遍历重复ID的行

下面的代码正在做它正在做的事情,即如果符合条件,用STL1/2替换CRL1/2。

产生的问题是,将来自商店的单个订单拆分为列A中每行具有相同订单号的单独行。该宏只处理具有相同订单号的第一行。

Dim Cel7_Lastrow As Integer 
    Cel7_Lastrow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row 
    Dim Rng As Range 
    Set Rng = Sheet2.Range("A2:A" & Cel7_Lastrow) 

    For Each Cel7 In Rng 
     Dim Lookup_Range2 As Range 

     Country_Name = Cel7.Value2 
     C_Code = Cel7.Value2 
     Weight_V = Cel7.Value2 

     Set Lookup_Range2 = Sheet2.Range("A2:R" & Cel7_Lastrow) 
     Country = Application.WorksheetFunction.VLookup(Country_Name,Lookup_Range2, 8, False) 
     Code = Application.WorksheetFunction.VLookup(C_Code,Lookup_Range2, 13, False) 
     Weight = Application.WorksheetFunction.VLookup(Weight_V,Lookup_Range2, 18, False) 

     If Country = "GB" And Code = "CRL1" And Weight <= 0.1 Then 
      Cel7.Offset(0, 12).Value = "STL1" 
     End If 

     If Country = "GB" And Code = "CRL2" And Weight <= 0.1 Then 
      Cel7.Offset(0, 12).Value = "STL2" 
     End If   
Next Cel7 
+1

由于VLOOKUP取第一值,其中有相匹配的,一旦它被改为'STL1'好得多如果在第一个之后不会实施更多的更改。 'Country = Cel7.Offset(0,7)|代码= Cel7.Offset(0,12)| Weight = Cel7.Offset(0,17)'不会使用查找。 – user3819867

+0

对于这个 – user2166874

+0

是否INDEX/MATCH函数会更好,MATCH也是第一个匹配。 – user3819867

回答

1

我可能失去了一些东西,但我认为一个循环将执行比查找

Dim Cel7_Lastrow As Integer 
Cel7_Lastrow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row 
For r = 2 To Cel7_Lastrow 
    If Sheet2.Cells(r, 8) = "GB" And Sheet2.Cells(r, 18) <= 0.1 Then 
     If Sheet2.Cells(r, 13) = "CRL1" Then Sheet2.Cells(r, 13) = "STL1" 
     If Sheet2.Cells(r, 13) = "CRL2" Then Sheet2.Cells(r, 13) = "STL2" 
    End If 
Next r 
+0

感谢从上面的评论中找出它,但这很好地阐明了它 – user2166874