Const C1 = 10 '连续数调整值
Const C2 = 15 '非连续数调整值
Const S = 10 '设定连续范围
Sub process()
Dim a, i!, j!, d!, n
Range("A1").Sort key1:=Range("A1"), order1:=xlAscending
a = WorksheetFunction.Transpose(Range("A1:A" & [A65536].End(xlUp).Row))
For i = 1 To UBound(a)
d = a(i) + S
For j = i To UBound(a)
If j + 1 > UBound(a) Then Exit For
If a(j + 1) > d Then Exit For
Next
If i = j Then '非连续数
a(i) = a(i) - C2
Else '连续数
n = a(i) - C1
For k = i To j
a(k) = n
Next
i = j
End If
Next
Range("B1").Resize(UBound(a)) = WorksheetFunction.Transpose(a)
End Sub
效果如图,可下载附件参考
'假如a数组存放你已经排好序的数据
for i=1 to ubond(a)
d=a(i)+10
for j=i+1 to ubond(a)
if a(j)>d then exit for
next j
if j=i+1 then '没有特殊情况
a(i)=a(i)-15
else
for k=i to j-1 '出现了特殊情况
a(k)=a(i)-10
next i
i=j-1
end if
next i
你是指利用Excel内置的VBA进行编程还是指用VB编的程序来处理Excel文档或控制Excel?如果是前者,那么你按Alt+F11打开VB界面输入代码即可开始编程。后者则可以把Excel文档或Excel应用程序作为对象进行相应操作。至于具体如何做,那就不是这里一两句话能说清的,甚至一两本书的容量也不一定说得完。你可以自己查看Excel和VB的相关帮助文档或者到微软的MSDN网站寻求帮助。
http://club.excelhome.net/thread-340532-1-1.htmlExcel VBA实战技巧精粹视频教程分享