Excel工作表行的匹配字符串的列号

时间:2012-12-10 07:17:51

标签: vbscript

我编写了一个代码来搜索每个数组变量,如果在Sheet的每一行中都有。现在当找到匹配时我需要知道列号是什么,找到了搜索字符串。知道如何使用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  .......

任何想法为什么如此?

由于

1 个答案:

答案 0 :(得分: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

复杂 - 不要说:愚蠢 - 喜欢

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

究竟是什么让你这样做的?