' For Each'循环不循环具有重复ID

时间:2015-11-04 12:18:18

标签: excel vba excel-vba

这是VBA宏的一部分,它会改变从在线商店导出的订单信息,以便稍后导入到送货经理。

如果符合条件,下面的代码就是按照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 个答案:

答案 0 :(得分: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