Macro which finds an #NA and copies the data and appends to another tab

Hi Guys, I want to write a Macro where I loop down column H of a tab called VBANK and where I find an #NA and copy the data to the cell to the left of the NA to a tab called Mapping Table and append to the bottom of Column A.
JCutcliffeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob HensonFinance AnalystCommented:
Other than different sheet name and column, how is this different to previous question for finding #NA???
0
JCutcliffeAuthor Commented:
It is not an array. There's only one tab copied to another
0
Rob HensonFinance AnalystCommented:
Is it a different file? Just thinking it could overwrite/confuse the results from other process.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JCutcliffeAuthor Commented:
Yep. It is a different file
0
Rob HensonFinance AnalystCommented:
Credit to Norie for the original VBA routine, amended here:

Sub CopyToMappings()
Dim wsDst As Worksheet
Dim wsSrc As Worksheet
Dim rngDst As Range
Dim rngSrc As Range
Dim arrSheets As Variant
Dim i As Long
    Set wsDst = Sheets("Mappings")
    
    Set rngDst = wsDst.Range("A" & Rows.Count).End(xlUp).Offset(1)
    
    Set wsSrc = Sheets("VBANK")
        
        With wsSrc
        
            For Each rngSrc In .Range("H2", .Range("H" & Rows.Count).End(xlUp)).Cells
            
                If rngSrc.Text = "#N/A" Then
                    rngSrc.Offset(, -1).Copy rngDst              ' copy source G to destination A
                    Set rngDst = rngDst.Offset(1)
                End If
                        
            Next rngSrc
            
        End With
   
End Sub

Open in new window

0
Rgonzo1971Commented:
Hi

if the only error are #N/A then try (outcomment line 16 or 17 accordingly)
Sub CopyToMappings()
Dim wsDst As Worksheet
Dim wsSrc As Worksheet
Dim rngDst As Range
Dim rngSrc As Range
Dim arrSheets As Variant
Dim i As Long
    Set wsDst = Sheets("Mappings")
    
    Set rngDst = wsDst.Range("A" & Rows.Count).End(xlUp).Offset(1)
    
    Set wsSrc = Sheets("VBANK")
        
        With wsSrc
            On Error Resume Next
            Set rngSrc = .Range("H2", .Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeFormulas, xlErrors)
            'Set rngSrc = .Range("H2", .Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants, xlErrors)
            On Error GoTo 0
            If Not rngSrc Is Nothing Then
                rngSrc.Offset(, -1).Copy rngDst
            End If
        End With
   
End Sub

Open in new window

Regards
0
JCutcliffeAuthor Commented:
Hi Rob, it keeps on Looping forever on this line:

   If rngSrc.Text = "#N/A" Then
                    rngSrc.Offset(, -2).Copy rngDst              ' copy source G to destination A
             Set rngDst = rngDst.Offset(1)


Any idea why?
0
Rob HensonFinance AnalystCommented:
After the line:
For Each rngSrc In .Range("H2", .Range("H" & Rows.Count).End(xlUp)).Cells

Enter a line and type Stop, to end up with

For Each rngSrc In .Range("H2", .Range("H" & Rows.Count).End(xlUp)).Cells
Stop

Open in new window

When the code runs it will reach the Stop statement and then freeze. Go to the VB Editor and the Stop line should be highlighted. Hover the cursor over the Rows.Count section of the line above and it should tell you how many rows are being looked at.. It could be that the Used Range of the sheet is messed up so it is checking a lot more rows than it needs to.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JCutcliffeAuthor Commented:
Superb as always!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.