Eliminating #N/A

Gos-C

Active Member
Joined
Apr 11, 2005
Messages
258
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

This is a crossed-post (see http://www.excelforum.com/excel-programming/654825-copy-and-paste-cells-if-match-found.html) as I am still unable to eliminate the #N/A from the following code:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Application.ScreenUpdating = False
Dim dataRange As Range, formulastr As String
    With Target
        If 1 < .Row And .Column = 2 And .Cells.Count = 1 Then
            Application.EnableEvents = False
            Set dataRange = Range(.Offset(-1, 0), .Parent.Range("B1:N1"))
            formulastr = "VLOOKUP(" & .Address(, , xlR1C1) & "," & dataRange.Address(, , xlR1C1) & ",COLUMN()-" & .Column & "+1,FALSE) & """" "
            With dataRange.EntireColumn.Rows(.Row)
                With .Offset(0, 1).Resize(, .Columns.Count - 1)
                    .FormulaR1C1 = "=" & formulastr
                    .Value = Evaluate("=IF(" & .Address(, , , True) & "=0,0," & .Address(, , , True) & ")")
                    On Error Resume Next
                        .SpecialCells(xlCellTypeConstants, xlErrors).ClearContents
                    On Error GoTo 0
                End With
                .EntireRow.Range("I1,J1,K1,M1").ClearContents
            End With
        End If
    End With
    If Target.Cells.Count > 1 Then
        Exit Sub
    End If
    On Error GoTo ErrHandler:
    If Not Application.Intersect(Range("D:D,H:H"), Target) Is Nothing Then
        If IsNumeric(Target.Value) = False Then
            Application.EnableEvents = False
            Target.Value = StrConv(Target.Text, vbProperCase)
            Application.EnableEvents = True
        End If
    End If
ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

When I entered the first cheque number (or a subsequent one that hasn't been previously entered), #N/A is being filled in C, D, E, F, G, H, L, N, and O. If the cheque number entered is not found, I want C, D, E, F, G, H, L, N, and O to be blank.

I think there needs to be:

Code:
If IsError . . . Then

Can someone help me, please.

Thank you,
Gos-C
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Instead of

=VLOOKUP(..., ..., ..., ...)

you could use

=IF(ISNA(VLOOKUP(..., ..., ..., ...)),"",VLOOKUP(..., ..., ..., ...))
 
Upvote 0
Hi Tim,

I am not sure how to put that into the code.

Gos-C
 
Upvote 0
Try this...

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Application.ScreenUpdating = False
Dim dataRange As Range, formulastr As String
    With Target
        If 1 < .Row And .Column = 2 And .Cells.Count = 1 Then
            Application.EnableEvents = False
            Set dataRange = Range(.Offset(-1, 0), .Parent.Range("B1:N1"))
            formulastr = "VLOOKUP(" & .Address(, , xlR1C1) & "," & dataRange.Address(, , xlR1C1) & ",COLUMN()-" & .Column & "+1,FALSE) & """" "
            formulastr = "IF(ISNA("&formulastr& "),""""","&formulastr& ")"
            With dataRange.EntireColumn.Rows(.Row)
                With .Offset(0, 1).Resize(, .Columns.Count - 1)
                    .FormulaR1C1 = "=" & formulastr
                    .Value = Evaluate("=IF(" & .Address(, , , True) & "=0,0," & .Address(, , , True) & ")")
                    On Error Resume Next
                        .SpecialCells(xlCellTypeConstants, xlErrors).ClearContents
                    On Error GoTo 0
                End With
                .EntireRow.Range("I1,J1,K1,M1").ClearContents
            End With
        End If
    End With
    If Target.Cells.Count > 1 Then
        Exit Sub
    End If
    On Error GoTo ErrHandler:
    If Not Application.Intersect(Range("D:D,H:H"), Target) Is Nothing Then
        If IsNumeric(Target.Value) = False Then
            Application.EnableEvents = False
            Target.Value = StrConv(Target.Text, vbProperCase)
            Application.EnableEvents = True
        End If
    End If
ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Tim,

It didn't work. The following line has an error - it's red:

Code:
[COLOR="Red"]formulastr = "IF(ISNA("&formulastr& "),""""","&formulastr& ")"[/COLOR]

Gos-C
 
Upvote 0

Forum statistics

Threads
1,216,459
Messages
6,130,758
Members
449,588
Latest member
accountant606

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