Excel 宏 - 值比较
实际上,我需要有关思考过程和选项的帮助,而不是编码方面的帮助。
我希望用户输入他们的邮政编码。然后我需要查看邮政编码的范围。 编辑:因为我来自比利时,所以邮政编码有 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
我想出了一个 If, Else 结构。但我想知道是否有更好的选择?
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
I actually need help on the thinking process and options, and not really on the coding.
I want users to enter their zip code. Then I need to look in what Range the zip code is.
Edit: Because I'm from Belgium, the zip code is 4 numbers long (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
I've come up with an If, Else structure. But I'm wondering whether there's a better option?
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 ....
Thank you.
@@@@@@@@@@@@@
Openshac
I've come up with this.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
switch 语句怎么样?
你所拥有的很好,这只是对你来说最易读/最容易维护的情况。您不应该注意到性能有任何差异
What about a switch statement?
What you've got is fine, it's just a case of what is most readable to you / easiest for you to maintain. You shouldn't notice any difference in performance
阅读各种评论我仍然不清楚问题是什么。我认为您提供了一个输入 ZIP (ARG = 2800),并且您想要显示数据表的所有 ZIP,其中千位数字与输入的千位数字相匹配,换句话说,
无需任何 VBA,您就可以使用计算的值定义高级过滤器基于上述公式的标准,标准公式为
或者,您可以跨 ZIP 定义自动过滤器,并通过 VBA 将下限和上限输入到自动过滤器标准中。
reading the various comments I am still not clear what is the question. I recon you provide an input ZIP (ARG = 2800) and you want to display all ZIPs of a data table where the thousand digit matches the thousand digit of the input, in other words
without any VBA you can define an advanced filter with a calculated criterium based on the formula above, the criterium formula would be
Alternatively you can define an Autofilter across ZIP and feed lowe and upper bound into the autofilter criteria via VBA.