0

シートの各行に存在する場合、配列変数のそれぞれを検索するコードを書きました。一致が見つかったら、検索文字列が見つかった列番号を知る必要があります。vbスクリプトを使用してそれを取得する方法はありますか?

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 を継続的に与えているようです。以下のように出力を生成します。

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

なぜそうですか?

ありがとう

4

1 に答える 1

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

このシートの出力:

アルファシート:

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

複雑なもので- 言うまでもなく: ばかげた - のように

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

一体何があなたをそうさせたのですか?

于 2012-12-10T13:15:20.413 に答える