2011-04-07 147 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自動篩選標準。