找回密码
 加入
搜索
查看: 9726|回复: 29

[AU3基础] 【已解决】求教---快速查找excel中指定字符串

 火.. [复制链接]
发表于 2011-8-28 16:42:59 | 显示全部楼层 |阅读模式
本帖最后由 SHINE 于 2011-9-16 13:47 编辑

求教---快速查找excel中指定字符串,并返回字符串的位置
我是菜鸟,现在用_ExcelReadCell的方式,读取3个子表,每个子表20列,1000行,共花掉了1分半钟~~觉得这个速度太慢~~
有什么方法可以快速查询吗?
请各位帮忙看看,谢谢了~~
发表于 2011-8-29 13:51:26 | 显示全部楼层
本帖最后由 kevinch 于 2011-8-29 13:52 编辑
Global $oExcelApp,$oExcel

$oExcelApp=ObjGet("","excel.application")
If IsObj($oExcelApp) Then
        $oExcelApp.visible=True
        If $oExcelApp.workbooks.count>0 then
                For $oExcel In $oExcelApp.activeworkbook.worksheets
                        searchcell("test2")
                Next
        EndIf
EndIf

Func searchcell($sFindValue)
    With $oExcel
                $oRng=.usedrange.find($sFindValue)
                If IsObj($oRng) Then
                        $oR=$oRng
                        Do
                                MsgBox(0,"GetColor",$oExcel.name&@TAB&$oR.address&@tab&$oR.offset(0,2).interior.color)
                                $oR=.usedrange.findnext($oR)
                        Until $oR.address=$oRng.address
                        MsgBox(0,"END","END")
                EndIf
        EndWith
EndFunc
改成这样,为了让你明白,再提供一组对应的vba代码,看后你会知道多明白一点的。
Sub test()
Dim Ws As Worksheet, Rng As Range, R As Range
For Each Ws In Worksheets
    If Not Ws.UsedRange.Find("test2") Is Nothing Then
        With Ws
            Set Rng = .UsedRange.Find("test2")
            Set R = Rng
            Do
                MsgBox .Name & vbTab & R.Address & vbTab & R.Offset(, 2).Interior.Color, , ""
                Set R = .UsedRange.FindNext(R)
            Loop Until Rng.Address = R.Address
        End With
    End If
Next Ws
MsgBox "End", vbOKOnly, "End"
End Sub
发表于 2011-8-28 17:10:45 | 显示全部楼层
贴上你的代码,大家好帮你改下。
 楼主| 发表于 2011-8-28 22:42:02 | 显示全部楼层
本帖最后由 SHINE 于 2011-8-29 09:35 编辑

#include <array.au3>
#include <ExcelCOM_UDF.au3>

Global $sRowStart,$iRowEnd,$iColStart,$iColEnd,$oExcel,$sFindValue,$i,$j,$get,$k,$color
Global $aArray[10]
$sFindValue="test2"
$sRowStart=1
$iRowEnd=1000
$iColStart=1
$iColEnd=20

$oExcel=_ExcelBookOpen("C:\Users\shine\Desktop\test\1.xls", 0)
$aArray = _ExcelSheetList($oExcel)

For $k = $aArray[0] To 1 Step -1 ;Work backwards through loop
    _ExcelSheetActivate($oExcel, $aArray[$k])
        disgroup()       
        searchcell()
Next
MsgBox(0,"END","END")
_ExcelBookClose($oExcel , 0,0)

Func disgroup()
With $oExcel
                For $Rng In .activesheet.usedrange
                        If $rng.mergecells=True Then
                    $rng.select
                                        $rng.parent.parent.parent.selection.unmerge
                    $rng.parent.parent.parent.selection.cells.value=$rng.value
            EndIf
                Next
EndWith
EndFunc

Func searchcell()
    For $i = $sRowStart To $iRowEnd
        For $j = $iColStart To $iColEnd
            If _ExcelReadCell($oExcel, $i, $j) = $sFindValue Then
                                 $color=_ExcelCellColorGet($oExcel, $i, $j)
                                 MsgBox(0,"getcolor",$color)
           EndIf
        Next
    Next
MsgBox(0,"END","END")
EndFunc

谢谢各位了
 楼主| 发表于 2011-8-28 22:42:36 | 显示全部楼层
本帖最后由 SHINE 于 2011-8-28 22:57 编辑

这个执行的时间有点久了
主要应该是耗在这段上了
Func searchcell()
    For $i = $sRowStart To $iRowEnd               ;  1000
        For $j = $iColStart To $iColEnd               ;20
            If _ExcelReadCell($oExcel, $i, $j) = $sFindValue Then
                                 $color=_ExcelCellColorGet($oExcel, $i, $j)
                                 MsgBox(0,"getcolor",$color)
           EndIf
        Next
    Next
MsgBox(0,"END","END")
EndFunc
发表于 2011-8-29 09:53:42 | 显示全部楼层
Func searchcell($sFindValue)
    With $oExcel
                $oRng=.usedrange.find($sFindValue)
                If IsObj($oRng) Then
                        $oR=$oRng
                        Do
                                MsgBox(0,"GetColor",$oR.interior.color)
                                $oR=.usedrange.findnext($oR)
                        Until $oR.address=$oRng.address
                EndIf
        EndWith
MsgBox(0,"END","END")
EndFunc
这个测试通过
发表于 2011-8-29 09:54:27 | 显示全部楼层
Global $oExcelApp,$oExcel

$oExcelApp=ObjGet("","excel.application")
If IsObj($oExcelApp) Then
        $oExcelApp.visible=True
        If $oExcelApp.workbooks.count>0 then
                $oExcel=$oExcelApp.activeworkbook.activesheet
                searchcell("付出款")
        EndIf
EndIf

Func searchcell($sFindValue)
    With $oExcel
                $oRng=.usedrange.find($sFindValue)
                If IsObj($oRng) Then
                        $oR=$oRng
                        Do
                                MsgBox(0,"GetColor",$oR.interior.color)
                                $oR=.usedrange.findnext($oR)
                        Until $oR.address=$oRng.address
                EndIf
        EndWith
MsgBox(0,"END","END")
EndFunc
完整的测试程序
 楼主| 发表于 2011-8-29 11:01:29 | 显示全部楼层
本帖最后由 SHINE 于 2011-8-29 11:09 编辑

谢谢!速度很快了,但是不懂这写法,不会改~~
试下来发现只能在当前子表里面查询,我想在3个(或者更多)子表里面查。
另外,查询到字符串后,可否知道该单元格坐标呢?想根据它再来抓去其前后单元格的信息。如附件中,找到test2后,再抓到后面框的颜色(标为红色),以及前面filepath。附件只是一个格式~~

感谢~~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2011-8-29 15:06:40 | 显示全部楼层
可以了,谢谢~~

再麻烦问一下,如果要从它前一个单元格读取内容,命令咋写呢?

这个是啥语言写的呢?找来学学~~呵呵,以前没见过~~

非常感谢~~
发表于 2011-8-29 21:37:36 | 显示全部楼层
MsgBox(0,"GetColor",$oExcel.name&@TAB&$oR.address&@tab&$oR.offset(0,2).interior.color)
改成
MsgBox(0,"GetColor",$oExcel.name&@TAB&$oR.address&@tab&$oR.offset(0,-1).interior.color)
就是报前面一个单元格的了。
offset(行偏移量,列偏移量)
offset(0,0)是自己
 楼主| 发表于 2011-8-29 22:30:14 | 显示全部楼层
谢谢,搞定了~~~
哥哥,这是什么语言呢?
发表于 2011-8-31 12:39:51 | 显示全部楼层
Excel VBA
Visual Basic for Application
 楼主| 发表于 2011-8-31 19:40:09 | 显示全部楼层
谢谢你~~~~
发表于 2011-8-31 21:52:53 | 显示全部楼层
用msado把excel做数据库源来读 数据快很多
发表于 2011-8-31 22:09:51 | 显示全部楼层
单个表的话,数组+字典比sql快,这个我们进行过测试的,平均2-3倍吧。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-19 06:53 , Processed in 0.082848 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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