VBA Help- Looping the Find Function

jbjetmore

New Member
Joined
Feb 26, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am new to VBA and largely have been attempting to teach myself how to do this with Google.

Essentially I want part of this macro to loop the 'Find' function to locate every instance of the text "#N/A" in a very large data set. If the line above the cell has data, I want to copy that data and paste it over the "#N/A" cell. If the line above does not have data, I want to copy the data from the cell below and paste it over the "#N/A" cell. If neither the cell above nor the cell below has data, I want it to just input "Filler" over the "#N/A" cell. And I want this to loop until there are no more "#N/A"s.

I know that this is not correct and only partially addresses what I am trying to do, but so far I have this:


VBA Code:
Do Until nullVal = False
    Columns("D:D").Select
    Selection.Find(What:="#N/A", After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Select
    If Not IsEmpty(ActiveCell.Offset(-1, 0)) Then
    ActiveCell.Offset(-1, 0).Select
    ActiveCell.Copy ActiveCell.Offset(1, 0)
    ElseIf IsEmpty(ActiveCell.Offset(-1, 0)) Then
    ActiveCell.FormulaR1C1 = "Filler"
    End If
    Loop


If you could help me turn this into what I am trying to accomplish, that would be wonderful, but at the very least, could someone please assist me to code the looping lines correctly? What should the "Do" line read in order to run my code until there are no more results with the "Find" function.

I really appreciate any and all help and consideration!
 
Last edited by a moderator:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
It appears to be the ElfseIf line

ElseIf Cells(M(T) + 1, "D") <> "" Then
I cant see why you would get that error on on that line (if that is the line you are using), but if you are running the code when you are on a sheet other than AGED AR DATA you'll need to use the code below or it will act on the wrong sheet.

VBA Code:
Sub FindNAx()
    Dim M, T&

    M = Filter(Evaluate("transpose(IF(ISERROR('AGED AR DATA'!D:D),Row(D:D),false))"), False, False)
 
    With Sheets("AGED AR DATA")
    
        For T = 0 To UBound(M)
        
            If .Cells(M(T) - 1, "D") <> "" Then
                .Cells(M(T), "D") = .Cells(M(T) - 1, "D")
            ElseIf .Cells(M(T) + 1, "D") <> "" Then
                .Cells(M(T), "D") = .Cells(M(T) + 1, "D")
            Else
                .Cells(M(T), "D") = "Filler"
            End If
    
        Next T

    End With
 
End Sub
 
Last edited:
Upvote 0
I cant see why you would get that error on on that line (if that is the line you are using), but if you are running the code when you are on a sheet other than AGED AR DATA you'll need to use the code below or it will act on the wrong sheet.

VBA Code:
Sub FindNAx()
    Dim M, T&

    M = Filter(Evaluate("transpose(IF(ISERROR('AGED AR DATA'!D:D),Row(D:D),false))"), False, False)
 
    With Sheets("AGED AR DATA")
   
        For T = 0 To UBound(M)
       
            If .Cells(M(T) - 1, "D") <> "" Then
                .Cells(M(T), "D") = .Cells(M(T) - 1, "D")
            ElseIf .Cells(M(T) + 1, "D") <> "" Then
                .Cells(M(T), "D") = .Cells(M(T) + 1, "D")
            Else
                .Cells(M(T), "D") = "Filler"
            End If
   
        Next T

    End With
 
End Sub
It still appears to be having an error, but now it is showing a different error message associated with the same line of the code.
"Run-time error '13'': Type mismatch."

It seems to get choked up in an instance where there are two "#N/A"s in a row, and no data above the first one. Perhaps these are niche instances where I need to manually fix them unless there is a way of identifying those instances in the code and adjusting them to also be "Filler"
 
Upvote 0
"I changed the worksheet to the proper name, and then changed the range to just D:D"
I'm not comfort with evaluate, but could it be because of that: there is D:D inside transpose ?
 
Upvote 0
Given that multiple #N/A values can occur together, I am unsure of the logic for a situation like this

jbjetmore.xlsm
D
1Hdr
2abc
3
4#N/A
5#N/A
6def
Sample


It would seem that when cell D4 is dealt with, since it has no value above and another #N/A below then D4 would get populated with "Filler"
When D5 is considered, it now has a text value above so does it get "Filler" from above or should it be considered in its original state with #N/A above and since it has a non-error value below should it get the "def" from below?

Anyway, you could try these to see if either suits your purpose

Sample data

jbjetmore.xlsm
D
1Hdr
2abc
3#N/A
4#N/A
5
6
7#N/A
8xx
923
10
11#N/A
12#N/A
13asdas
14
15
16#N/A
17#N/A
18#N/A
19
20
21#N/A
22#N/A
23#N/A
24xvc
AGED AR DATA


VBA Code:
Sub ReplaceNA_v2()
  Dim rFound As Range
  
  With Sheets("AGED AR DATA").Columns("D")
    Set rFound = .Find(What:="#N/A")
    If Not rFound Is Nothing Then
      Do
        Select Case True
          Case rFound.Offset(-1).Value <> ""
            rFound.Value = rFound.Offset(-1).Value
          Case Not IsError(rFound.Offset(1).Value)
            If rFound.Offset(1).Value <> "" Then
              rFound.Value = rFound.Offset(1).Value
            Else
              rFound.Value = "Filler"
            End If
          Case Else
            rFound.Value = "Filler"
        End Select
        Set rFound = .Find(What:="#N/A")
      Loop Until rFound Is Nothing
    End If
  End With
End Sub

Result of above code

jbjetmore.xlsm
D
1Hdr
2abc
3abc
4abc
5
6
7xx
8xx
923
10
11Filler
12Filler
13asdas
14
15
16Filler
17Filler
18Filler
19
20
21Filler
22Filler
23Filler
24xvc
AGED AR DATA


VBA Code:
Sub ReplaceNA_v3()
  Dim rFound As Range
  
  With Sheets("AGED AR DATA").Columns("D")
    Set rFound = .Find(What:="#N/A")
    If Not rFound Is Nothing Then
      Do
        Select Case True
          Case rFound.Offset(-1).Value <> "" And rFound.Offset(-1).Value <> "Filler"
            rFound.Value = rFound.Offset(-1).Value
          Case Not IsError(rFound.Offset(1).Value)
            If rFound.Offset(1).Value <> "" Then
              rFound.Value = rFound.Offset(1).Value
            Else
              rFound.Value = "Filler"
            End If
          Case Else
            rFound.Value = "Filler"
        End Select
        Set rFound = .Find(What:="#N/A")
      Loop Until rFound Is Nothing
    End If
  End With
End Sub

Result of above code

jbjetmore.xlsm
D
1Hdr
2abc
3abc
4abc
5
6
7xx
8xx
923
10
11Filler
12asdas
13asdas
14
15
16Filler
17Filler
18Filler
19
20
21Filler
22Filler
23xvc
24xvc
AGED AR DATA
 
Upvote 0

Forum statistics

Threads
1,224,931
Messages
6,181,838
Members
453,067
Latest member
mdiz777

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