我正在使用代码在每行的第一个单元格中搜索“待上传”一词。
lastrow = tmpSheet.Cells(tmpSheet.Rows.Count, "A").End(xlUp).Row
Set Foundcell = tmpSheet.Range("A2:A" & lastrow).Find(What:="To be Uploaded")
搜索了关键字&把它放在Foundcell但是在我正在应用它的excel文件中,在第12行有“要上载”,但没有把它放在Foundcell中。没有把价值放在哪里的可能性是什么? 它在调试后没有显示任何内容。
控制是在lastrow中给出12值,但它仍然没有放入Foundcell。
如何通过修剪来找到该单词(如果有空格则忽略所有空格)? 控制是在单元格中获得一些额外的空格,但我将它与较少的空格字进行比较,因此错误即将来临。有两个空格b / w To&是的,这就是控制不一样的原因。
答案 0 :(得分:0)
我希望这会有所帮助。请注意,比较是区分大小写的
Sub FindCell()
Dim rows, LastRow As Integer
With Sheets("tmpSheet")
LastRow = .Cells(.rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
If .Cells(i, 1).Value = "To Be Uploaded" Then
' Do your Stuff
' I assume you want the address
MsgBox .Cells(i, 1).Address
End If
Next
End With
End Sub
答案 1 :(得分:0)
这是.Find()
的正确使用,第二部分可能对你不感兴趣,但至少你知道如果你想保持循环,它是如何正确完成的。< / p>
看看这个:
Sub test_ksrds()
Dim FirstAddress As String, _
LastRow As Long, _
FoundCell As Range
With tmpSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set FoundCell = .Range("A2:A" & LastRow).Find(What:="To be Uploaded")
.Range("A2").Activate
With .Range("A2:A" & LastRow)
'First, define properly the Find method
Set FoundCell = .Find(What:="To be Uploaded", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If there is a result, keep looking with FindNext method
If Not FoundCell Is Nothing Then
FirstAddress = FoundCell.Address
Do
'--- Let's say you want to set the value to 1 in column D if you found the value in column A
FoundCell.Offset(0, 3).Value = 1
Set FoundCell = .FindNext(FoundCell)
'Look until you find again the first result
Loop While Not FoundCell Is Nothing And FoundCell.Address <> FirstAddress
End If
End With
End With
End Sub
答案 2 :(得分:0)
试试这段代码。我还添加了一些行来打印最后打开的Immediate Window
中的部分结果。请参阅代码中的注释。
Sub Rng_FindMethod()
Dim tmpSheet As Worksheet, pasteSheet As Worksheet
Dim FoundCell As Range, LastRow As Long
Rem Somehow you should have set these objects in your original Procedure
Rem Setting them here for testing purposes
With ThisWorkbook
Set tmpSheet = .Worksheets(1)
Set pasteSheet = .Worksheets(2)
End With
Rem New variable
Dim sFnd1st As String
With tmpSheet 'use [With] to perform several statements on the same object
'see https://msdn.microsoft.com/en-us/library/office/gg264723(v=office.15).aspx
: .Activate 'Activate the worksheet to run the test
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'use period [.] to refer to the properties of the object working [With]
: Rem Printing partial results for debugging - REMOVE WHEN FINAL!
: Debug.Print vbLf
: Debug.Print String(3, vbLf)
: Debug.Print "LastRow: ", LastRow
If LastRow <> 1 Then
With .Range(.Cells(2, 1), .Cells(LastRow, 1))
Rem Activate last cell of the range to start the search from the 1st cell (due to [After:=ActiveCell])
.Cells(.Rows.Count, .Columns.Count).Activate
Rem Search range for partial matches (due to [LookAt:=xlPart])
Set FoundCell = .Find(What:="To be Uploaded", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Rem Validate Search Results
If Not (FoundCell Is Nothing) Then
Rem Set address of first match found use later to validate completeness
sFnd1st = FoundCell.Address
Rem Run action with finding & reiterate search
Do
: Rem Printing partial results for debugging - REMOVE WHEN FINAL!
: Debug.Print "FoundCell: ", FoundCell.Address, "[" & FoundCell.Value2 & "]"
Rem Action on search results
tmpSheet.Rows(FoundCell.Row).EntireRow.Copy
pasteSheet.Rows(pasteSheet.Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Rem Find next match
Set FoundCell = .FindNext(After:=FoundCell)
Rem Validate Search Results
Loop While FoundCell.Address <> sFnd1st
End If: End With: End If: End With
: Rem Opens the Immediate window to see results posted for debugging - REMOVE WHEN FINAL!
: SendKeys "^g": Stop
End Sub