Why is the item I'm searching for empty? VBA Nested Loop

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
When I watch this code LookFor is empty, it shouldn't be and I cannot figure out why. Please help?
I am searching for the number found on ImpWs column B in NmWs column B then swapping out the text. Replacing NmWs with the correct text currently found in ImpWs column A.
VBA Code:
Dim NmWs As Worksheet
Dim ImpWs As Worksheet
Dim NmLastRow, ImpLastRow As Long
Dim NmRange As Range
Dim ImpRange As Range
Dim i, j As Long
Dim LookFor As Variant

Set NmWs = ThisWorkbook.Sheets("Nwsh")
Set ImpWs = ThisWorkbook.Sheets("ImportRINS")

'last rows
    NmLastRow = NmWs.Cells(Rows.Count, "A").End(xlUp).Row
    ImpLastRow = ImpWs.Cells(Rows.Count, "A").End(xlUp).Row

'set dynamic arrays
Set NmRange = Range(NmWs.Cells(2, 1), NmWs.Cells(NmLastRow, 2))
Set ImpRange = Range(ImpWs.Cells(2, 1), ImpWs.Cells(ImpLastRow, 3))
    
    For i = ImpLastRow To 2 Step -1
        For j = NmLastRow To 2 Step -1
            LookFor = ImpRange(i, 2)
                If NmRange(j, 2) = LookFor Then
                    NmWs.Cells(j, 1).Clear
                    NmWs.Cells(j, 1).Value = ImpWs.Cells(i, 1).Value
                    ImpWs.Rows(i).EntireRow.Delete
                Exit For
            End If
        Next j
    Next i

This code is ImpWs is updating NmWs

ImpWs
Data_Entry_Form_ver_25c.xlsm
ABC
1New NameRINExisting Name
2Braga, Jose Maria (b. 1897)16898Braga, Jose Maria (b. 1897)djdsj
3da Luz, Augusta Isabel da Conceição Ozorio (b. 1898)16899da Luz, Augusta Isabel da Conceição Ozorio (b. 1898) NOJN{O
4Donnelly, Matthew (b. 1884)16888Donnelly, Matthew (b. 1890)
5Haar, Emily Margaret (b. 1914)16904Harr, Emily Margaret (b. 1914)
6Wilson, Mary (b. 1886)16889Wilson, Mary (b. 1890)
ImportRINS


NmWs
Data_Entry_Form_ver_25c.xlsm
AB
16262Donnelly, Matthew (b. 1890)16888
16263Wilson, Mary (b. 1890)16889
16264Wheatley, John C. (b. 1927)16892
16265Murhall-Griffith, Thelma Jean (b. 1909)16894
16266Donovan, Thomas (b. 1851)16896
16267Long, Mary (b. 1860)16897
16268Braga, Jose Maria (b. 1897)djdsj16898
16269da Luz, Augusta Isabel da Conceição Ozorio (b. 1898) NOJN{O16899
16270Rebbechi, Victor Richard (b. 1912)16903
16271Harr, Emily Margaret (b. 1914)16904
16272Singleton, William Alan (b. 1897)16908
16273Coates, Doris Maude (b. 1900)16909
16274Worle, Ellen Mercy Eliza (b. 1881)16905
16275Wiltshire, William Henry (b. 1879)16906
16276Tann, Margaret Mavis (b. 1920)16901
16277Kendall, Abel (b. 1908)16893
16278Hurley, Ella (b. 1990)16907
16279Herrick, Leslie William (b. 1912)16900
16280Elizabeth (b. 1965)16902
16281David (b. 1950)16895
Nwsh
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You define
Set NmWs = ThisWorkbook.Sheets("Nwsh")
Set ImpWs = ThisWorkbook.Sheets("ImportRINS")

as ranges, then try to use them as single cell ranges?
Rich (BB code):
LookFor = ImpRange(i, 2)
                If NmRange(j, 2) = LookFor Then

Perhaps you meant
Rich (BB code):
LookFor = ImpWs.Cells(i, 2)
                If NmWs.Cells(j, 2) = LookFor Then
 
Upvote 0
How are you determining that LookFor is empty?
 
Upvote 0
ImpRange has been converted to an array so the bounds have been reset. The first pass of the loop is outside of the array. You would need to use something like
VBA Code:
For i = ImpRange.Rows.Count To 1 Step -1
for both loops.
 
Upvote 0
How are you determining that LookFor is empty?
When I watched it and stepped through the procedure
ImpRange has been converted to an array so the bounds have been reset. The first pass of the loop is outside of the array. You would need to use something like
VBA Code:
For i = ImpRange.Rows.Count To 1 Step -1
for both loops.
This was definitely part of the problem. Thanks for the advice.
You define
Set NmWs = ThisWorkbook.Sheets("Nwsh")
Set ImpWs = ThisWorkbook.Sheets("ImportRINS")

as ranges, then try to use them as single cell ranges?
Rich (BB code):
LookFor = ImpRange(i, 2)
                If NmRange(j, 2) = LookFor Then

Perhaps you meant
Rich (BB code):
LookFor = ImpWs.Cells(i, 2)
                If NmWs.Cells(j, 2) = LookFor Then
This was definitely part of the problem. Thanks for the advice.

A combination of these worked.
The final working code was
VBA Code:
Dim NmWs As Worksheet
Dim ImpWs As Worksheet
Dim NmLastRow, ImpLastRow As Long
Dim NmRange As Range
Dim ImpRange As Range
Dim i, j As Long
Dim LookFor As Variant

Set NmWs = ThisWorkbook.Sheets("Nwsh")
Set ImpWs = ThisWorkbook.Sheets("ImportRINS")

'last rows
    NmLastRow = NmWs.Cells(Rows.Count, "A").End(xlUp).Row
    ImpLastRow = ImpWs.Cells(Rows.Count, "A").End(xlUp).Row

'set dynamic arrays
Set NmRange = Range(NmWs.Cells(2, 1), NmWs.Cells(NmLastRow, 2))
Set ImpRange = Range(ImpWs.Cells(2, 1), ImpWs.Cells(ImpLastRow, 3))
    
    For i = ImpLastRow To 1 Step -1
        For j = NmLastRow To 2 Step -1
            LookFor = ImpWs.Cells(i, 2)
                If NmWs.Cells(j, 2) = LookFor Then
                    NmWs.Cells(j, 1).Clear
                    NmWs.Cells(j, 1).Value = ImpWs.Cells(i, 1).Value
                    ImpWs.Rows(i).EntireRow.Delete
                Exit For
            End If
        Next j
    Next i

End Sub
Thanks for your help, I really appreciate this message board and all of you who answer my questions.
Dannielle
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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