Issue with Find in my script

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
This works fine, but I rather not use .Select
Code:
Range("F13:F22").Select
For Each c In Range("F25:F33").Cells
 If Not IsEmpty(c.Value) Then
    Set FoundCell = Selection.Find(What:=c.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not FoundCell Is Nothing Then c.Offset(0, 1).Value = FoundCell.Offset(0, 2).Value + c.Offset(0, 1).Value
 End If
Next c

So I thought this would work, but it doesn't.
Code:
With Range("F13:F22")
  For Each c In Range("F25:F33").Cells
    If Not IsEmpty(c.Value) Then
      Set FoundCell = .Find(What:=c.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
      If Not FoundCell Is Nothing Then c.Offset(0, 1).Value = FoundCell.Offset(0, 2).Value + c.Offset(0, 1).Value
    End If
  Next c
End With

Neither does this.
Code:
For Each c In Range("F25:F33").Cells
 If Not IsEmpty(c.Value) Then
    Set FoundCell = Range("F13:F22").Find(What:=c.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not FoundCell Is Nothing Then c.Offset(0, 1).Value = FoundCell.Offset(0, 2).Value + c.Offset(0, 1).Value
 End If
Next c

The result of both are that the values are not updated.
The purpose of the script is the find all values from F25:F33 in F13:F22 and update G25:G33 with value from H13:H22.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
All three of those codes should work.
Try stepping through the code line by line using F8 to see what is happening.
 
Upvote 0
Find doesn't find =)
Added some debug lines and as this script show, there is something wrong with
Set FoundCell = .Find(What:=c.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

The result seems to be "nothing". Only conclusion I have is that 'With' doesn't compute with '.Find' in this situation.
Code:
With Range("F13:F22")
  For Each c In Range("F25:F33").Cells
   Debug.Print "1: " & c.Address, c.Value
    If Not IsEmpty(c.Value) Then
      Set FoundCell = .Find(What:=c.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
      If Not FoundCell Is Nothing Then c.Offset(0, 1).Value = FoundCell.Offset(0, 2).Value + c.Offset(0, 1).Value
      If Not FoundCell Is Nothing Then Debug.Print " F: " & c.Offset(0, 1).Value, FoundCell.Offset(0, 2).Value, c.Offset(0, 1).Value
    End If
  Next c
End With
Debug.Print "---------------------"
Range("F13:F22").Select
For Each c In Range("F25:F33").Cells
Debug.Print "2: " & c.Address, c.Value
 If Not IsEmpty(c.Value) Then
    Set FoundCell = Selection.Find(What:=c.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not FoundCell Is Nothing Then c.Offset(0, 1).Value = FoundCell.Offset(0, 2).Value + c.Offset(0, 1).Value
    If Not FoundCell Is Nothing Then Debug.Print " F: " & c.Offset(0, 1).Value, FoundCell.Offset(0, 2).Value, c.Offset(0, 1).Value
 End If
Next c

Code:
1: $F$25      Test1
1: $F$26      Test2
1: $F$27      
1: $F$28      
1: $F$29      
1: $F$30      
1: $F$31      
1: $F$32      
1: $F$33      
---------------------
2: $F$25      Test1
 F: 55500      1500          55500 
2: $F$26      Test2
2: $F$27      
2: $F$28      
2: $F$29      
2: $F$30      
2: $F$31      
2: $F$32      
2: $F$33
 
Upvote 0
As I said before all three of the codes you posted work.
What else do you have in that sub?
 
Upvote 0
As I said before all three of the codes you posted work.
What else do you have in that sub?

There are no more code in the sub.

Some of the cells are merged, but the second code I posted should prove that there are no data issue. Since both code streams use same data, right!?
 
Upvote 0
Some of the cells are merged
That is the problem, NEVER use merged cells, they are an abomination & should be avoided like the plague.
 
Upvote 0
That is the problem, NEVER use merged cells, they are an abomination & should be avoided like the plague.

Hahaha, yeah, starts to realize that now. Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top