在每行的前7个字符中搜索单词

时间:2014-02-19 09:28:21

标签: excel vba excel-vba

我正在尝试寻找“nFormat”但只是在开头(仅在前7个字符中)。如果我找到它,我需要查看上面一行中的最后一个字符,如果它不是@我需要写它并将两行放在一起

我的节目是:

Sub Line_Config()

Dim Lrow As Long
Dim Lastrow As Long
Dim Prow As Long
Dim atual As String
Dim nextRow As String
Dim fGet As Range
Dim fnFormat As Range
Dim fa As Range

With Sheets("Get_Command")
.Select

Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

' Start the loop
For Lrow = Lastrow To 2 Step -1

    Prow = Lrow - 1

    Set fGet = Cells(Lrow, 1).Find("Get:", LookIn:=xlValues)

        If fGet Is Nothing Then 'If Get: is not found

        Set fnFormat = Cells(Lrow, 1).Find("nFormat", LookIn:=Left(Cells(Lrwo, 1), 7))

            If Not fnFormat Is Nothing Then 'If nFormat is found

            Set fa = Cells(Prow, 1).Find("@", LookIn:=Right(Cells(Prow, 1), 1))

                If fa Is Nothing Then

                    atual = Cells(Lrow, 1).Value
                    nextRow = Cells(Prow, 1).Value + "@" + atual

                    Cells(Prow, 1).FormulaR1C1 = nextRow
                    Cells(Lrow, 1).EntireRow.Delete

                End If

            Else

            atual = Cells(Lrow, 1).Value
            nextRow = Cells(Prow, 1).Value + atual

            Cells(Prow, 1).FormulaR1C1 = nextRow
            Cells(Lrow, 1).EntireRow.Delete

            End If

        End If

Next Lrow

.Columns("A").Replace _
What:="@", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByColumns

End With

End Sub

Excel告诉我错误在:

 Set fnFormat = Cells(Lrow, 1).Find("nFormat", LookIn:=Left(Cells(Lrwo, 1), 7))

我该如何更改?

由于

1 个答案:

答案 0 :(得分:0)

此次调用收到错误

Set fnFormat = Cells(Lrow, 1).Find("nFormat", LookIn:=Left(Cells(Lrwo, 1), 7))

因为参数LookIn不用于定义使用Find命令的范围,但它用于定义是否查看该范围内的值或公式。
LookIn可以采用枚举值XlFindLookInxlCommentsxlFormulasxlValues

您应该使用InStr(Start, String1, String2, Compare) function

Dim tString as String
tString = Left(Cells(Lrow, 1).Value, 7)
If InStr(1, "nFormat", tString, Compare:=vbTextCompare) > 0) then
  'nFormat was found, do stuff
End If