简体   繁体   English

查找连续具有特定值的第一个和最后一个条目

[英]finding the first and last entry with a certain value in a row

I have a excel sheet containing multiple cells with a string foo in the first row. 我有一个excel表包含多个单元格,第一行有一个字符串foo I want to find the first and last column in which the string is written. 我想找到写入字符串的第一列和最后一列。 I have tried the following 我尝试了以下内容

Dim first_col As Integer
Dim last_col As Integer
Dim col As Integer
Dim found As Range
Dim ws_MLB as Worksheet
Dim foo as String

set ws_MLB = ThisWorkbook.Sheet(1)

Set found = ws_MLB.Rows(1).Find(foo)
If Not found Is Nothing Then
    col = found.Column
    first_col = col
    last_col = col
    Do
        found = ws_MLB.Rows(1).FindNext(found)
        col = found.Column

        If col < first_col Then
            first_col = col
            MsgBox ("This should not happen")
        ElseIf col > last_col Then
            last_col = col
        End If
    Loop While Not found Is Nothing And col <> first_col
Else
    MsgBox ("not found")
End If

But this way I only get the the first value for both first_col and last_col. 但是这样我只得到first_col和last_col的第一个值。 When I search for the string with the integrated excel search I find multiple instances. 当我使用集成的Excel搜索搜索字符串时,我找到了多个实例。 So the string is there. 所以字符串就在那里。 Have I done a mistake or is there a better way to do this? 我做错了还是有更好的方法来做到这一点?

edit forgot to mention that I also tried to change the search direction, but I still got the first entry. 编辑忘记提及我也试图改变搜索方向,但我仍然得到了第一个条目。

You can make this a lot easier by using the SearchDirection Parameter in .Find by using xlNext you search Left to Right then xlPrevious searches Right to Left. 你可以做这个有很多使用容易SearchDirection在参数.Find使用xlNext你搜索从左到右再xlPrevious搜索从右到左。

Sub FindFL()

    Dim wbk As Workbook
    Dim ws As Worksheet
    Dim fColumn As Long, lColumn As Long

    Set wbk = ThisWorkbook 'Change this to your workbook
    Set ws = wbk.Worksheets("Sheet1") 'Change this to your worksheet

    With ws
        'Find first column that foo shows up
        fColumn = .Cells.Find(What:="foo", _
            After:=.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext, _
            MatchCase:=False).Column
        'Find last column that foo shows up
        lColumn = .Cells.Find(What:="foo", _
            After:=.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Column

        Debug.Print "First Column is " & fColumn; vbNewLine _
        ; "Last Column is " & lColumn
    End With
End Sub

I would do that like this: 我会这样做:

Public Sub foo()
  Dim nCol As Integer
  Dim nFirst As Integer
  Dim nLast As Integer

  With ActiveSheet
    nCol = 1
    Do Until .Cells(1, nCol) = ""
      If .Cells(1, nCol) = "foo" Then
        If nFirst = 0 Then
          nFirst = nCol
        Else
          nLast = nCol
        End If
      End If
      nCol = nCol + 1
    Loop
  End With

  MsgBox "First: " & nFirst & vbCr & "Last: " & nLast
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM