2014-02-19 27 views
0

我正在尝试查找“nFormat”,但只是在开头(仅在前7个字符中)。如果我找到了,我需要从行的最后一个字符来看看上面,如果不是@我需要写,并把两线一起在每行的前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

为什么要使用'Find'单节?为什么不比较:'如果左(Cells(Lrwo,1),7)=“nFormat”Then'? –

+0

谢谢,我开始用vba工作了2周......我没有这样做! =) –

+0

我改变了你说的方式,但它仍然没有运行,现在显示这一行,并说:运行时错误'1004'应用程序定义或对象定义的错误! –

回答

0

你得到一个错误,该呼叫

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

由于参数看着不用于定义范围使用的Find命令,但它是用来定义是否期待在该范围内的值或公式中。
LookIn可以采用枚举值XlFindLookInxlComments,xlFormulasxlValues

您应该使用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 
相关问题