Excel精英培训网

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

[习题] 【VBA入门点滴】listview查找并在第一行高亮显示

[复制链接]
发表于 2008-7-25 08:09 | 显示全部楼层 |阅读模式
<p>有人要帮忙,写一段利用listview查询,并显示查找到的特定行!</p><p>我做了一个,只实现了部分功能:</p><p><font color="#1a1ae6" size="4">1、打开窗体时,listview加载“资料库”工作表中的所有客户资料;</font></p><p><font color="#1a1ae6" size="4">2、双击listview某行,可以将内容添加到“数据库”工作表的最后一行;</font></p><p><font color="#1a1ae6" size="4">3、在文本框中输入姓名,通过“查询”按钮可以查询姓名,并将查找到的行,以一行高亮显示,或将查找到的行,在第一行高亮显示。</font></p><p><font color="#ee3d11"></font>&nbsp;</p><p><font color="#ee3d11">现在的问题是:</font></p><p><font color="#ee3d11">1、查询时,能否将找到的显示在第一行?而且要求显示的行的颜色可以设置在其他颜色?</font></p><p><font color="#ee3d11">2、或者,查询时,只显示查找到的行,其他的都不要显示?</font></p><p>&nbsp;</p><p><font color="#c211ee" size="6">在戏子版主的帮助中,问题已经解决!</font></p><p><font color="#c211ee" size="6">(见11楼附件和动画)</font></p><p> oNpuOvJH.rar (21.69 KB, 下载次数: 311)

[讨论]listview查找并标亮显示整行

[讨论]listview查找并标亮显示整行
 楼主| 发表于 2008-7-25 08:09 | 显示全部楼层

<p>提问的帖子在这里:</p><p> </p><div style="FLOAT: left;"><a title="[求助]listview控件示例——请帮忙写代码!!" href="http://www.excelpx.com/forum.php?mod=viewthread&tid=64310&amp;replyID=&amp;skin=1" target="_blank"><font color="#000000" style="FONT-SIZE: 15px;">[求助]listview控件示例——请帮忙写代码!!</font></a> <br/><font color="#008000">作者:<a href="http://www.excelpx.com/dispuser.asp?id=183858" target="_blank"><font color="#008000">sunkevin</font></a>&nbsp;&nbsp; 浏览:30&nbsp;&nbsp; 回复:1&nbsp;&nbsp;→ &nbsp;&nbsp;<a href="http://www.excelpx.com/forum.php?mod=forumdisplay&fid=5" target="_blank"><font color="#008000"><font color="#008000">『VBA程序开发』</font> </font></a></font><font color="#808080">2008-7-24 16:00:00&nbsp;&nbsp;</font> <hr style="BORDER-TOP: #b3b3b3 1px dashed; WIDTH: 98%; BORDER-BOTTOM: 0px; HEIGHT: 0px;"/><hr/></div><p></p><p></p><p></p><p></p><p></p>
回复

使用道具 举报

发表于 2008-7-25 11:32 | 显示全部楼层

<img alt="" src="http://www.excelpx.com/Skins/default/topicface/face9.gif"/>
回复

使用道具 举报

 楼主| 发表于 2008-7-26 08:43 | 显示全部楼层

<p><font color="#ee3d11">现在的问题是:</font></p><p><font color="#ee3d11">1、查询时,能否将找到的显示在第一行?而且要求显示的行的颜色可以设置在其他颜色?</font></p><p><font color="#ee3d11">2、或者,查询时,只显示查找到的行,其他的都不要显示?</font></p><p><font color="#ee3d11"></font></p><p></p>
回复

使用道具 举报

 楼主| 发表于 2008-7-26 20:43 | 显示全部楼层

<p>没人路过坐一会儿呀!</p><p>[em06]</p><p></p><p></p>
回复

使用道具 举报

发表于 2008-7-26 23:39 | 显示全部楼层

自己研究下告诉我俚,将答案留下[em02][em02][em02]
[此贴子已经被作者于2008-7-26 23:39:57编辑过]
回复

使用道具 举报

发表于 2008-7-27 11:02 | 显示全部楼层

大师有进展否????????
回复

使用道具 举报

 楼主| 发表于 2008-7-27 11:40 | 显示全部楼层

<div class="msgheader">QUOTE:</div><div class="msgborder"><b>以下是引用<i>wxyqxxz2007</i>在2008-7-27 11:02:00的发言:</b><br/>有进展否????????</div><p></p><p>[em06][em06][em06]</p><p>&nbsp;</p><p>&nbsp;</p>
回复

使用道具 举报

 楼主| 发表于 2008-8-2 11:54 | 显示全部楼层

<p>戏子帮忙修改了一下,思路是先在工作表中查找数据,然后再重新加载到窗框中!</p><p><table cellspacing="0" cellpadding="0" width="869" border="0" xstr="" style="WIDTH: 652pt; BORDER-COLLAPSE: collapse;"><colgroup><col width="869" style="WIDTH: 652pt; mso-width-source: userset; mso-width-alt: 22246;"></col></colgroup><tbody><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" width="869" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; WIDTH: 652pt; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312">rivate Sub 查询_Click() <font class="font5" color="#008000">''戏子</font></font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>On Error Resume Next</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font face="仿宋_GB2312" size="2"></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>With ListView1</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>.HideSelection = False<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><font class="font5" color="#008000">'选定文本总是突出显示。</font></font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>.MultiSelect = False<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><font class="font5" color="#008000">'' 不允许多行选择</font></font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>.ListItems.Clear</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>For i = 1 To Sheets("资料库").Range("A65536").End(xlUp).Row</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>If TextBox1.Text = Sheets("资料库").Cells(i, 1) Then</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Set ITM = .ListItems.Add()</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ITM.Text = Sheets("资料库").Cells(i, 1)</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ITM.SubItems(1) = Sheets("资料库").Cells(i, 2)</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ITM.SubItems(2) = Sheets("资料库").Cells(i, 3)</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ITM.SubItems(3) = Sheets("资料库").Cells(i, 4)</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ITM.SubItems(4) = Sheets("资料库").Cells(i, 5)</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ITM.SubItems(5) = Sheets("资料库").Cells(i, 6)</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>End If</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>.SelectedItem.Selected = False<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><font class="font5" color="#008000">''查询不到,则退出当前已经选中的行</font></font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Next i</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font size="2"><font face="仿宋_GB2312"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>End With</font></font></td></tr><tr height="25" style="HEIGHT: 18.75pt;"><td class="xl24" height="25" style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 18.75pt; BACKGROUND-COLOR: transparent;"><font face="仿宋_GB2312" size="2">End Sub</font></td></tr></tbody></table></p>
[此贴子已经被作者于2008-8-2 11:56:37编辑过]
回复

使用道具 举报

 楼主| 发表于 2008-8-2 13:13 | 显示全部楼层

<p>终于明白了。为什么成“灰色”的原因,是因为“查询”按钮用了“MouseMove”事件的缘故,按了以后,焦点又马上因为“MouseMove”事件而回到了“查询”按钮上。</p>
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 23:14 , Processed in 0.407523 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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