我是VBA的新手并且陷入了真正的困境。 我有两个工作表。我必须根据他们的地址为每个客户分配销售人员。 在Sheet1上我使用三个数据列,Zip(K),City(I)和Country(L)。 在Sheet2上,我在列B和C(低值和高值),城市(D)和国家(E)中有一个邮政编码范围。在每一行中都有指定销售人员的姓名。
代码的要求: 检查客户的国家/地区是否与第一个销售人员国家/地区匹配。 如果是,请检查客户的邮政编码是否在范围内。如果匹配复制销售人员姓名到Sheet1并移动到下一行。 如果Sheet2上没有Zip范围作为条件,或者客户的zip没有匹配,请检查City是否匹配,是否匹配复制销售人员姓名到Sheet1并移至下一行。如果Sheet2上没有城市作为标准,或者客户的城市没有匹配,请检查国家是否匹配并将销售人员名称复制到Sheet1。
如果到目前为止就是这样:
`Sub Territory()
Dim i As Integer
Dim sh1 As Worksheet, sh2 As Worksheet
Dim sh1Rws As Long, sh1Rng As Range, s1 As Range
Dim sh2lowRws As Long, sh2lowRng As Range, s2l As Range
Dim sh2highRws As Long, sh2highRng As Range, s2h As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
Set i = 1
With sh1
sh1Rws = .Cells(Rows.Count, "K").End(xlUp).Row
Set sh1Rng = .Range(.Cells(1, "K"), .Cells(sh1Rws, "K"))
End With
With sh2l
sh2lowRws = .Cells(Rows.Count, "B").End(xlUp).Row
Set sh2lowRng = .Range(.Cells(1, "B"), .Cells(sh2lowRws, "B"))
End With
With sh2h
sh2highRws = .Cells(Rows.Count, "C").End(xlUp).Row
Set sh2highRng = .Range(.Cells(1, "C"), .Cells(sh2highRws, "C"))
End With
For Each s1 In sh1Rng
For Each s2l In sh2lowRng
If s1 > s2l And s1 < s2h Then sh2lowRws.Copy Destination:=Sheet.sh1.Range("u", i)
End If
Set i = i + 1
End Sub`
答案 0 :(得分:0)
尝试以下代码,让我知道它是否有效或需要更改
Sub test()
i = Sheets(1).Range("a1048576").End(xlUp).Row
l = Sheets(2).Range("a1048576").End(xlUp).Row
For k = 2 To i
For x = 2 To l
CityCus = Sheets(1).Range("I" & k).Value
CitySales = Sheets(2).Range("D" & x).Value
CotyCus = Sheets(1).Range("L" & k).Value
CotySales = Sheets(2).Range("E" & x).Value
ZipCus = Sheets(1).Range("K" & k).Value
ZipUpperSales = Sheets(2).Range("B" & x).Value
ZiplowerSales = Sheets(2).Range("C" & x).Value
c = Sheets(1).Range("b" & k).Value
d = Sheets(2).Range("A" & x).Value
If CotyCus = CotySales Then
If CityCus = CitySales Then
If ZipCus <= ZiplowerSales And ZipCus >= ZipUpperSales Then
Sheets(1).Range("b" & k).Value = Sheets(2).Range("A" & x).Value
End If
End If
End If
Next
Next
End Sub