1

我有以下代码。它在一列中搜索特定值。它工作正常,但如果单元格有换行符,代码不会搜索第二行。

vardestinolinha = ThisWorkbook.Sheets("base").Range("a11").End(xlDown).Row

a = 10
k = a
For i = a To vardestinolinha
    Search = ThisWorkbook.Sheets(NomeTabela).Range("a2")
    Replacement = ThisWorkbook.Sheets(NomeTabela).Range("c" & i)
    varposicao = ThisWorkbook.Sheets(NomeTabela).Range("b" & i) '''''

    Set rngFind = ThisWorkbook.Sheets("base").Columns(2).Find(What:=Search, LookIn:=xlValues, lookat:=xlPart)
    Do While Not rngFind Is Nothing
        tamanho = Len(rngFind)
        p = InStr(1, rngFind, Search, vbTextCompare)

        If p > 0 Then
            ThisWorkbook.Sheets("base").Cells(k, 5) = ThisWorkbook.Sheets("base").Cells(k, 3)
            k = k + 1
        End If
        Set rngFind = ThisWorkbook.Sheets("base").Columns(2).FindNext
    Loop
    k = i + 1
Next

即使有换行符,我也希望代码搜索整个单元格。

4

1 回答 1

2

如果使用 Alt+Enter 方法在单元格中输入了文本,则可以在 VBA 中使用它:

" & Chr(10) & "

这是我使用的 .Find 方法。

Private Sub CommandButton1_Click()
Set RngClosedDate = Range("A1:Z10").Find(What:="Closed" & Chr(10) & "(Date)", LookAt:=xlWhole, LookIn:=xlValues)
      ' If the text that is searched for from the line above is not found then a message box is displayed and sub function is exitied
      If RngClosedDate Is Nothing Then
        MsgBox "Closed (Date) Column Header Not found.  Cannot sort or format records."
        Exit Sub
      End If
End Sub
于 2014-02-25T16:42:27.300 回答