2014-02-19 33 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 
相關問題