为什么控制不把variables的价值?

我正在使用代码在每行的第一个单元格中search单词“待上传”。

lastrow = tmpSheet.Cells(tmpSheet.Rows.Count, "A").End(xlUp).Row Set Foundcell = tmpSheet.Range("A2:A" & lastrow).Find(What:="To be Uploaded") 

它search关键字并将其放在Foundcell上,但是在我正在使用的Excel文件中,在第12行有“待上载”,但没有将该值存入Foundcell。 它没有把价值放在什么地方? debugging后没有任何显示。

在这里输入图像说明

控制在拉斯特罗给出了12个值,但仍然没有放入Foundcell。

如何通过修剪find这个词(如果有的话,忽略所有的空格)? 控制是在单元格中获得一些额外的空间,但我用较less的空间比较它,所以错误即将到来。 b / w有2个空格,这就是为什么控制不一样。

我希望这有帮助。 请注意,比较是区分大小写的

 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 

这里是.Find()的正确使用,第二部分对你来说可能不是很有趣,但是至less你会知道如何保持循环。

看看这个 :

 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 

试试这个代码。 我还包括了一些行,在最后打开的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