Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 11566|回复: 42

小问题大思路:谈字符串的补位处理

[复制链接]
发表于 2008-7-8 14:04 | 显示全部楼层 |阅读模式
<p>&nbsp; 今天的出了一个小题,本以为挺简单,但看了许多上交的答案却不太满意.</p><p>&nbsp; 题目:A列是小于等于6位的字母组合,要求不足A列不足六位的用*号补齐</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a id="ImgSpan" href="http://www.excelpx.com/UploadFile/2008-7/2008788261616986.jpg" target="_blank"><img height="200" alt="按此在新窗口浏览图片" src="http://www.excelpx.com/UploadFile/2008-7/2008788261616986.jpg" width="241" border="0"/></a></p><p>&nbsp; 分析: 其实这个题目虽小,但考查的知识点却不少:</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1、string函数的使用</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2、字符串截取方法</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3、数组的赋值</p><p>    4、其他加速技巧</p><p>下接二楼</p>
[此贴子已经被作者于2008-7-8 14:59:50编辑过]
 楼主| 发表于 2008-7-8 14:04 | 显示全部楼层

<p><font size="4">第一, <font color="#c60707"><strong>先清除,再填充</strong></font></font></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果我们需要多次把处理后的数据填充到某列,有时我们发现第一次填充时速度很快,后来运行同样的代码速度却变慢了。这是因为如果原来的数据没有删除而用覆盖的方法,会大大降低填充的速度.</p><p>  由上,我们在填充之前一定要把原来的数据先删除之:</p><p>   如:<font color="#0808e7">Range("A1:A10000")=""</font> </p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p><p>  </p>
[此贴子已经被作者于2008-7-8 14:12:45编辑过]
回复

使用道具 举报

 楼主| 发表于 2008-7-8 14:04 | 显示全部楼层

<p><font size="4">第二, <font color="#c60707"><strong>关闭屏幕刷新</strong></font></font></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这是一个老生常谈的问题,大家也掌握的很不错了,这里再罗嗦一下,算是给新手提个醒.</p><p>  如果程序中涉及大量的工作表、单元格等excel对象的操作时,屏幕会一直闪动,这些闪动会降低程序运行的效率。所以关闭屏幕刷新是很好的解决方法:</p><p>   在程序开始处添加关闭刷新语句:<font color="#0808e7">Application.ScreenUpdating = False</font></p><p><font color="#0808e7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 代码</font></p><p><font color="#0808e7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000000">在程序结尾处添加恢复屏幕刷新语句:</font><font color="#0808e7">Application.ScreenUpdating = True</font></font></p>
[此贴子已经被作者于2008-7-8 14:19:41编辑过]
回复

使用道具 举报

 楼主| 发表于 2008-7-8 14:04 | 显示全部楼层

<p><font size="4">第三, <font color="#c60707"><strong>怎么能更简单的表示变动的单元格区域</strong></font></font></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;表示变动区域的代码几乎无处不在, 多数用户却绕了个弯来表示.</p><p>&nbsp;&nbsp;&nbsp;&nbsp; 如表示A列的数据区域,大家都是这样多设置了一个变量,这样写:</p><p><font color="#0808e7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; x=Range("a65536").End(xlup).row</font></p><p><font color="#0808e7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;arr=Range("A1:A"&amp; x)</font></p><p>    Range最基本的写法是:&nbsp; Range(<font color="#3d6633"><strong>左上角单元格, 右下角单元格</strong></font>)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;所以在表示A列的变动区域时只需要这样写:<font color="#0808e7">Range("a1", [a65536].End(xlUp))</font></p>
[此贴子已经被作者于2008-7-8 14:30:27编辑过]
回复

使用道具 举报

 楼主| 发表于 2008-7-8 14:05 | 显示全部楼层

<p><font size="4">第四, <font color="#c60707"><strong>VBA函数和工作表函数</strong></font></font></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;解答本题可以使用两个函数组合 ,一个是<strong><font color="#1039de" size="3">string</font></strong>函数 ,另一个是<font color="#0831d6"><strong>right</strong></font>函数</p><p></p><p><font color="#0808e7"></font></p>    VBA中的string函数等同于工作表中的rept函数,它是取重复的字符. 用法是 <p></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong> <font color="#1039de">string(个数, 需要重复的字符)</font></strong></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 本题中我们就可以借助string函数来补齐文本,即</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <font color="#072dc6"><strong>String(6 - Len(arr(x, 1)), "*") &amp; arr(x, 1)</strong></font></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 有的会员调用了工作表函数rept :&nbsp; application.rept(.....) ,但通过大量数据的运行验证,使用VBA内置函数要比调用工作表函数快的多,也就说以后我们在处理数据时,<font color="#d60808">有VBA内置函数的就不要调用工作表函数</font>。</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这个题目还有第二个解题思路,即用先多补位后截取的方法来完成,这里的right函数也是VBA内置函数之一。</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;即:<font color="#0808e7">Right("******" &amp; arr(x, 1), 6)</font></p><p><font color="#0808e7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000">使用截取方法我们就可以省去一个函数,在速度上比使用string+len组合更快,和工作表中设置公式一样,VBA编程也是</font><font color="#dd0000">思路为王</font>。好的思路需要不断的积累和学习,</font><font color="#000000">希望大家在做题过程中多总结些高效的思路。不过这里兰色也提醒一下想提高的VBA学员:万丈高楼平地起,基础未牢切莫贪高.。</font></p>
[此贴子已经被作者于2008-7-8 14:46:54编辑过]
回复

使用道具 举报

 楼主| 发表于 2008-7-8 14:06 | 显示全部楼层

<p><font size="4">第五, <font color="#c60707"><strong>数组的灵活运用</strong></font></font></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;今天还看到yy共享的一个贴子,令他高兴的是原来需要长时间运行才能得到结果的代码,使用数组在瞬间完成了.</p><p>呵呵,这就是强大的VBA数组.它可以让你走上数据处理高速路.可以这样说,如果一个用户不懂VBA数组,那他至多也只能算一个VBA的入门汉.</p><p>  闲话少说,谈一下本题的数组使用,本题中如果要让程序高速运行,数组也是必不可少的.</p><p>  首先我们需要把A列的数据存放在数组arr里</p><p>  <font color="#1039de">arr = Range("a1", [a65536].End(xlUp))</font></p><p> 接下来就有很多会员再声明一个数组,存放处理后的数据,其实这里我们只需要对arr数组进行重新替换就可以 </p><p>   即:<font color="#0831d6">For x = 1 To UBound(arr)<br/>&nbsp;       arr(x, 1) = Right("*****" &amp; arr(x, 1), 6)<br/>     Next x</font></p><p> 处理后的数组就可以填充到单元格区域中:</p><p>     <font color="#0000dd">[c1].Resize(x - 1) = arr </font> </p>
[此贴子已经被作者于2008-7-8 14:55:38编辑过]
回复

使用道具 举报

 楼主| 发表于 2008-7-8 14:59 | 显示全部楼层

<p>代码1:使用right截取法</p><p><font color="#0f0fce">  Sub ss()<br/>    Range("c1", [c65536].End(xlUp)) = ""<br/>    Application.ScreenUpdating = False<br/>    Dim arr, x As Long<br/>    arr = Range("a1", [a65536].End(xlUp))<br/>    For x = 1 To UBound(arr)<br/>&nbsp;      arr(x, 1) = Right("*****" &amp; arr(x, 1), 6)<br/>    Next x<br/>    [c1].Resize(x - 1) = arr<br/>    Application.ScreenUpdating = True<br/>  End Sub</font></p><p>第二种:使用string函数补齐.<br/><font color="#0909f7">  Sub tt()<br/>    Range("d1", [c65536].End(xlUp)) = ""<br/>    Application.ScreenUpdating = False<br/>    Dim arr, x As Long<br/>    arr = Range("a1", [a65536].End(xlUp))<br/>    For x = 1 To UBound(arr)<br/>&nbsp;      arr(x, 1) = String(6 - Len(arr(x, 1)), "*") &amp; arr(x, 1)<br/>    Next x<br/>    [d1].Resize(x - 1) = arr<br/>    Application.ScreenUpdating = True<br/>   End Sub</font></p>
回复

使用道具 举报

 楼主| 发表于 2008-7-8 15:01 | 显示全部楼层

弄了一个小时,终于完工了,希望对大家会有所帮助[em04]
回复

使用道具 举报

发表于 2008-7-8 15:06 | 显示全部楼层

学习了!谢谢兰版的指导![em23][em23][em23]
回复

使用道具 举报

发表于 2008-7-8 15:07 | 显示全部楼层

学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-15 13:34 , Processed in 0.372422 second(s), 6 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表