Pasting a formula into a dynamic table column

WxShady13

Board Regular
Joined
Jul 24, 2018
Messages
185
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I am pasting a Vlookup formula into a table (information) that is dynamic. The formula goes fine, however I run code after that to replace #N/A with a 0. I have a line of code that finds the last row, however all my #N/A code does is find the last cell and place a 0 in it.


Code:
Sub VlookupFormula()
Dim lRow As Long
Dim ws As Worksheet
Range("P1").Select
ActiveCell.FormulaR1C1 = "Vlookup Amount"
Range("P2").Formula = "=VLOOKUP(O2,Table3[#All],4,FALSE)"
lRow = Cells(Rows.Count, 1).End(xlUp).Row
Set NARange = Worksheets("Data").Range("P2" & lRow)
Worksheets("Data").Activate
For Each CheckCell In NARange
    If Application.WorksheetFunction.IsNA(CheckCell) Then
         'CheckCell.Offset(0, 1).Copy
         Worksheets("Data").Range("Y1").Copy
         CheckCell.Select
         Worksheets("Data").Paste  'Doing this rather than value will bring formatting
    End If
Next CheckCell
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this:


I am pasting a Vlookup formula into a table (information) that is dynamic. The formula goes fine, however I run code after that to replace #N/A with a 0. I have a line of code that finds the last row, however all my #N/A code does is find the last cell and place a 0 in it.


Code:
Sub VlookupFormula()
Dim lRow As Long
Dim ws As Worksheet
Range("P1").Select
ActiveCell.FormulaR1C1 = "Vlookup Amount"
Range("P2").Formula = "=VLOOKUP(O2,Table3[#All],4,FALSE)"
lRow = Cells(Rows.Count, 1).End(xlUp).Row
Set NARange = Worksheets("Data").[B][COLOR=#ff0000]Range("P2:P" & lRow)[/COLOR][/B]
Worksheets("Data").Activate
For Each CheckCell In NARange
    If Application.WorksheetFunction.IsNA(CheckCell) Then
         'CheckCell.Offset(0, 1).Copy
         Worksheets("Data").Range("Y1").Copy
         CheckCell.Select
         Worksheets("Data").Paste  'Doing this rather than value will bring formatting
    End If
Next CheckCell
End Sub
 
Upvote 0
Shouldn't the VLOOKUP function just be wrapped in an IFERROR function?
 
Upvote 0
Question Sir...For some reason when I run the code it does not stop at the last line of the table. It just continues on, any ideas?
 
Upvote 0
The last line is:

Code:
[COLOR=#333333][I]lRow = Cells(Rows.Count, 1).End(xlUp).Row[/I][/COLOR]

check if in column A you have blank cells facing down
 
Upvote 0
Perform the test with 3 records in column A and execute with F8 step by step the macro and tell me the line number stored in the variable lrow
 
Upvote 0
Sir THANK YOU for troubleshooting...What I have found is it runs fine, just a RAM issue on my computer doing the calculations. Sir a thank you truly isn't enough, you saved me a ton of work!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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