簡體   English   中英

如何獲取Excel中單詞第二次(或最后一次)出現的行號?

[英]How to get the row number of the second (or the last) occurence of a word in Excel?

我正在從Excel工作表中的網頁中復制一些內容,並搜索特定的單詞,然后獲取記錄的行號。 復制的內容來自HTML頁面,並以表格格式粘貼到excel中。 有時,該單詞在表中被提及兩次,而我想獲取最后一個記錄的行號。 我有用於第一次獲取行號的代碼,但是我無法弄清楚最后一條記錄的行號。 在獲取行號值時,在變量“ row_no”中,我需要最后顯示的記錄的行號。 我該怎么辦? 請在下面找到我編寫的代碼:

Set ie = New InternetExplorerMedium
            ie.Visible = True
            ws.Activate
strHTML = URL
            ie.navigate strHTML
Do While ie.Busy Or ie.READYSTATE <> 4
                DoEvents
            Loop

            Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
            Set objCollection = ie.document.getElementsByTagName("input")

            Set tables = ie.document.getElementsByTagName("Table")

            msg = ie.document.Body.innerHTML

              If InStr(msg, "Word1") = 0 Then
            Else
               For x = 0 To tables.Length

                        If tables(x).innerText Like "*Word1*" Then
                         found = True

                         If x > 0 Then
                         Set clipboard = New MSForms.DataObject
                         clipboard.SetText tables(x).outerHTML
                         clipboard.PutInClipboard
                         ws.Activate
                         ws.Range("A1").PasteSpecial
                         Result = Application.WorksheetFunction.CountIf(Range("E2:E10000"), "End Analysis")
                         MsgBox Result
                         If Result> 1 Then
'get the row number                            
                          row_no = Worksheets("temp").Range("E2:E10000").Find("End Analysis", lookat:=xlWhole).Row
                         End If
                         End If
                    Next x
                 End If   

像這樣

Option Explicit

Public Sub test()
    Dim found As Range
    With Worksheets("temp").Range("E2:E10000")
       Set found = .Find(what:="End Analysis", searchorder:=xlByColumns, searchdirection:=xlPrevious)
       If Not found Is Nothing Then Debug.Print found.Row
    End With
End Sub

看起來更像您的:

Dim found As Range, row_no As Long
If Result > 1 Then
   Set found = Worksheets("temp").Range("E2:E10000").Find(what:="End Analysis", searchorder:=xlByColumns, searchdirection:=xlPrevious)
   If Not found Is Nothing Then 
       row_no = found.Row
   Else
      MsgBox "Not Found"
   End If
End If

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM