2011-04-07 101 views
0

我实际上需要思考过程和选项的帮助,而不是真正的编码。Excel宏 - 值比较

我希望用户输入他们的邮政编码。然后,我需要查看邮政编码的范围。 编辑:因为我来自Belgium,邮编是4号长(1000,1500,8380,...)

Zip code: 2800 
Range between; 
1000-1999 
2000-2999 
3000-3999 
4000-4999 
If the range is between one of the following, display all zip codes for that range 
Else do nothing 

,我想出了一个如果,否则结构。但我想知道是否有更好的选择?

Zip code: 2800 
If zip code > 3999 then 
    'Select all zipcodes within this range 
    'Range(" .. ").Copy Destination:=Sheets... 
ElsIf zip code > 2999 then 
    'Select all zipcodes within this range 
    'Range(" .. ").Copy Destination:=Sheets... 
Elsif zipcode .... 

谢谢。

@@@@@@@@@@@@@

Openshac

我想出这个。

x = 1 
Sheets("Reference").Select 
For i = 1 to 115 
    If Range("A" & i).value > 5999 and Range("A" & i).value < 6000 then 
     Range("A" & i).copy Destination:=Sheets("Design").Range("A" & x) 
     x = x + 1 
    End if 
Next i 
+0

什么是“做点什么”?每个邮政编码范围都会改变吗? – shahkalpesh 2011-04-07 08:21:35

+0

等一下,我会改变它。 – CustomX 2011-04-07 08:27:57

+0

hmmmm其中(Integer ??)数大于(>)5999,同时小于(<)6000 .....坏坏坏硬编码!所以这是否意味着任何给定的邮政编码,你想显示您的表中的所有邮政编码在理智的千位范围内......我制定了一个单独的答案 – MikeD 2011-04-07 11:58:57

回答

0

switch语句怎么样?

Select Case zipCode 
Case 1000 To 1999 
    'Do something 
Case 2000 To 2999 
    'Do something 
Case 3000 To 3999 
    'Do something 
Case 4000 To 4999 
    'Do something 
Case Else 
    ' Do nothing 
End Select 

你有什么是好的,它只是对什么是最可读的,以你的情况下/你最容易维护。你不应该注意到任何性能差异

+0

是的,我想知道是否使用案例。也许你可以帮我选择范围,我需要复制? – CustomX 2011-04-07 08:29:42

+0

#Tom是范围(“..”)。复制目标:=表...代码在你原来的问题? – openshac 2011-04-07 08:38:56

+0

不,我改变它了解什么是做什么应该是。出于好奇,只问你能不能帮忙。 – CustomX 2011-04-07 08:40:29

0

阅读各种评论我还不清楚是什么问题。我侦察你提供一个输入邮政编码(ARG = 2800),并要显示一个数据表中的所有ZIP探头在千位在千位输入的匹配,换句话说

ZIPdata.ZIP BETWEEN INT(ARG/1000) * 1000 AND INT(ARG/1000) * 1000 + 999 

没有任何VBA你可以定义基于上述公式计算出的绕圈高级过滤器,该绕圈公式将是

=AND(B9>=$B$2;B9<=$E$2) 

    B9 being the ZIP in your first data record (not the heading) 
    B2 being the lower bound using above formulae 
    E2 being the upper bound using above formulae 

或者,也可以跨ZIP和饲料洛韦限定自动过滤和上装订成经由VBA自动筛选标准。