2012-12-10 50 views
0

我寫了一個代碼來搜索每個數組變量,如果出現在Sheet.Now的每一行中,當匹配被找到時,我需要知道列號是什麼,在那裏找到了搜索字符串。任何想法如何獲得使用VB腳本?Excel工作表行匹配字符串的列號

ParentColmnCount=ParentColmnCount-1 

IntRow6=2 

DataCount=0 

Do While objSheet6.Cells(IntRow6,1).Value <> "" 

For DataCount=0 to UBound(VMHArray) 
    If Not objSheet6.Range(objSheet6.Cells(IntRow6,1),objSheet6.Cells(IntRow6,ParentColmnCount)).Find(VMHArray(DataCount)) Is Nothing Then 
MsgBox(objSheet6.Range(objSheet6.Cells(IntRow6,1),objSheet6.Cells(IntRow6,ParentColmnCount)).Find(VMHArray(DataCount)).Columns) 

    End If 
     Next 

IntRow6=IntRow6+1 

Loop 

更新:

IntRow6=2 
DataCount=0 
Do While objSheet6.Cells(IntRow6,1).Value <> "" 

For DataCount=0 to UBound(VMHArray) 


    Set rSearch = objSheet6.Cells(IntRow6,1).EntireRow 
    Set rFound = rSearch.Find(VMHArray(DataCount)) 

     If Not rFound Is Nothing Then 
       adrFirst = rFound.Address 

       Do 
        MsgBox(IntRow6) 
        MsgBox(rFound.Column + 1) 
        MsgBox(adrFirst) 
        rCol=rFound.Column 
        objSheet6.Cells(IntRow6,rCol + 2)= objSheet6.Cells(IntRow6,rCol + 5) 
        Set rFound = rSearch.FindNext(rFound) 
       Loop Until rFound.Address <> adrFirst And Not rFound Is Nothing 
     End If 

Next 

IntRow6=IntRow6+1 
Loop 

但似乎控制陷入無限循環,不斷給予21作爲其第一個匹配列number.Producing如下輸出:

2 33 $AF$2  2 33 $AF$2   2 33 $AF$2 ....... 

任何想法爲什麼?

感謝

回答

2

演示腳本來展示如何使用.Find:

Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject") 
    Dim sFSpec : sFSpec = oFS.BuildPath(oFS.GetAbsolutePathname("..\xls"), "13763603.xls") 
    Dim oXls : Set oXls = CreateObject("Excel.Application") 
    Dim oWb : Set oWb = oXls.Workbooks.Open(sFSpec) 
    Dim oWs : Set oWs = oWb.Worksheets(2) 
    Dim rSearch, rFound, adrFirst 

    Set rSearch = oWs.Cells(1, 1).EntireRow 
    Set rFound = rSearch.Find("b1") 
    WScript.Echo 1, "TypeName(rFound):", TypeName(rFound), rFound.Row, rFound.Column 

    Set rSearch = oWs.Range(oWs.Cells(1, 1), oWs.Cells(5, 3)) 
    Set rFound = rSearch.Find("nix") 
    WScript.Echo 2, "TypeName(rFound):", TypeName(rFound) 

    Set rFound = rSearch.Find("alpha") 
    If Not rFound Is Nothing Then 
    adrFirst = rFound.Address 
    Do 
     WScript.Echo 3, "TypeName(rFound):", TypeName(rFound), rFound.Row, rFound.Column 
     Set rFound = rSearch.FindNext(rFound) 
    Loop Until rFound.Address = adrFirst 
    End If 

    oWb.Close 
    oXls.Quit 

輸出此表:

alpha sheet

1 TypeName(rFound): Range 1 2 
2 TypeName(rFound): Nothing 
3 TypeName(rFound): Range 1 3 
3 TypeName(rFound): Range 2 1 
3 TypeName(rFound): Range 4 2 
3 TypeName(rFound): Range 5 3 

更新:

由於可以從輸出

---------------------- 
4 1 TypeName(rFound): Range 1 3 
Wahr  rFound.Address <> adrFirst 
Falsch rFound Is Nothing 
Wahr  Not rFound Is Nothing 
Wahr  rFound.Address <> adrFirst And Not rFound Is Nothing 
我的診斷代碼插入到上面的腳本

中可以看出:

WScript.Echo "----------------------" 
    Dim nCnt : nCnt = 0 
    Set rFound = rSearch.Find("alpha") 
    If Not rFound Is Nothing Then 
    adrFirst = rFound.Address 
    Do 
     nCnt = nCnt + 1 
     If nCnt > 9 Then WScript.Echo "Aborting" : Exit Do : End If 
     WScript.Echo 4, nCnt, "TypeName(rFound):", TypeName(rFound), rFound.Row, rFound.Column 
     Set rFound = rSearch.FindNext(rFound) 
     WScript.Echo CStr(rFound.Address <> adrFirst), vbTab, "rFound.Address <> adrFirst" 
     WScript.Echo CStr(rFound Is Nothing), vbTab, "rFound Is Nothing" 
     WScript.Echo CStr(Not rFound Is Nothing), vbTab, "Not rFound Is Nothing" 
     WScript.Echo CStr(rFound.Address <> adrFirst And Not rFound Is Nothing), vbTab, "rFound.Address <> adrFirst And Not rFound Is Nothing" 
     WScript.Echo 
    Loop Until rFound.Address <> adrFirst And Not rFound Is Nothing 
    End If 

這不是一個好主意,以取代我的精心打造簡單條件

Loop Until rFound.Address = adrFirst 

用東西compl icated - 不是說:傻 - 像

Loop Until rFound.Address <> adrFirst And Not rFound Is Nothing 

在地球上進行你那是什麼?

+0

優秀的幫助從你我到現在...謝謝Ekkehard –

+0

我可以幫我解決我的新問題,任何更快的代碼嗎? http://stackoverflow.com/questions/13798858/row-data-partition-empty-column-values-in-a-row-in-one-side-and-non-empties-ar#comment18983534_13798858 –

+0

根據我的研究我覺得FindNext()方法搜索相同的值,如果它存在於同一行中,那麼它將被找到一次。因此,如果它第二次出現在同一行中,循環將繼續,否則rFound.Address和adrFirst將是相同的,並且循環條件「rFound.Address <> adrFirst」應該打破循環以進一步迭代。但在我的情況下,它並沒有發生,它落入了永久循環,這正如我在更新後的代碼中所提到的那樣被Output所證明。那麼原因是什麼? –

相關問題