troubleshooting Question

VBA Code to Ignore Skip Copy/Paste Command on Cells Containing #N/A

Avatar of KANEDA 0149
KANEDA 0149Flag for United States of America asked on
VBAMicrosoft ExcelMicrosoft Office
5 Comments1 Solution400 ViewsLast Modified:
I have an Excel workbook with a macro to copy/paste specific cells from 'Sheet1' worksheet into specific cells to 'XML' worksheet.  The code works great but for cells in 'Sheet1' which the formula is returning #N/A (as expected), I would like the VBA script that I currently have to skip or ignore those cell ranges (however many it may be).  Any assistance would be great!

VBA Code
Sub Copy()
Dim LR As Long, i As Long, j As Long
With Sheets("Sheet1")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LR
        j = j + 1
        .Range("M" & i & ":M" & i).Copy
        Sheets("XML").Range("A" & j).PasteSpecial Paste:=xlPasteValues
        .Range("A" & i & ":A" & i).Copy
        Sheets("XML").Range("C" & j).PasteSpecial Paste:=xlPasteValues
        .Range("N" & i & ":P" & i).Copy
        Sheets("XML").Range("D" & j).PasteSpecial Paste:=xlPasteValues
        .Range("Q" & i & ":Q" & i).Copy
        Sheets("XML").Range("I" & j).PasteSpecial Paste:=xlPasteValues
    
        j = j + 1
        .Range("D" & i & ":D" & i).Copy
        Sheets("XML").Range("B" & j).PasteSpecial Paste:=xlPasteValues
        .Range("R" & i & ":R" & i).Copy
        Sheets("XML").Range("D" & j).PasteSpecial Paste:=xlPasteValues
        .Range("C" & i & ":C" & i).Copy
        Sheets("XML").Range("I" & j).PasteSpecial Paste:=xlPasteValues
        .Range("B" & i & ":B" & i).Copy
        Sheets("XML").Range("J" & j).PasteSpecial Paste:=xlPasteValues

    Next i
End With
With Application
    .CutCopyMode = False
    .ScreenUpdating = True
End With

End Sub

Example of Sheet1 tab
excel_sheet1.png
Example of XML tab
excel_xml.png
ASKER CERTIFIED SOLUTION
NorieSenior Associate
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros