jeudi 21 mai 2020

Search whole sheet with find returns empty values

I am trying to search a whole sheet with the below code. It works when I put a breakpoint and run the code manually, however, running the code turns up empty searches. I have tried setting the search value as

FindString = MainSht.Range("A" & x).Value

as well as

FindString = MainSht.Range("A" & x).Text

but both turn up empty searches (or rather they return an "N/a" which is wrong cause I find the values when searching manually).

I need to be able to search the whole worksheet so I am setting the range as .Cells.

Sub One_Find()

Dim Compld As Range
Dim FindString As String
Dim x, NumRows As Integer
Dim MainSht, SearchSht As Worksheet
Dim MainBk, SearchBk As Workbook

Set MainBk = ThisWorkbook
Set SearchBk = ActiveWorkbook


Set MainSht = MainBk.Sheets("Curr des")
Set SearchSht = SearchBk.Sheets("Current")


NumRows = MainSht.Cells(MainSht.Rows.Count, "A").End(xlUp).Row


For x = 2 To NumRows


FindString = MainSht.Range("A" & x).Text


With SearchSht.Cells
Set Compld = .Find(what:=FindString, LookIn:=xlValues, lookat:=xlWhole)
  If Not Compld Is Nothing Then
    MainSht.Range("B" & x).Value = Compld.End(xlToRight).Value
  Else
    MainSht.Range("B" & x).Value = "N/a"
  End If
End With
Next


End Sub

Let me know if you need more information!

Aucun commentaire:

Enregistrer un commentaire