paste to visible cells (excel 2007)

jlax34

New Member
Joined
Feb 18, 2015
Messages
26
Need some help finishing off this Macro. The portion of the code in question is below. It checks cell I1 (which subtotals the column), and if it comes up an error, it filtes the rows with the error and changes the vlookup to another file. The problem I have is with the code pasting the new vlookup to the other visible cells. I can see it correctly update the formula on the first visible row, but the copy down part doesnt work. I've tried searching but must not be using the magical words because most every response I find is how to COPY visible cells to another column/workbook etc....not copy a formula only to visible cells.

Note: I also have blank cells below this data so I need to set the range somehow to prevent copying tot he whole sheet



Code:
If IsError(Range("I1")) = True Then
    Range("I2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$N$150000").AutoFilter Field:=9, Criteria1:="#N/A"
    Range([H3], Cells(Rows.Count, "H")).SpecialCells(xlCellTypeVisible)(1).Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(RC[-6],'[Inventory View.xlsx]Sheet1'!C3:C22,20,FALSE)"
    Selection.FillDown
    ActiveSheet.ShowAllData
End If




[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]#N/A[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Store[/TD]
[TD]Item Code[/TD]
[TD]Description[/TD]
[TD]Bill Qty[/TD]
[TD]Ship Pack[/TD]
[TD]Cost[/TD]
[TD]Cube[/TD]
[TD]Total Cube[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]Misc[/TD]
[TD]2[/TD]
[TD]24[/TD]
[TD]19.8[/TD]
[TD]#N/A[/TD]
[TD]#N/A[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD]Misc[/TD]
[TD]3[/TD]
[TD]12[/TD]
[TD]20.2[/TD]
[TD]#N/A[/TD]
[TD]#N/A[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
See if this will work.
Code:
If IsError(Range("I1")) = True Then
    Range("I2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$N$150000").AutoFilter Field:=9, Criteria1:="#N/A"
    Range([H3], Cells(Rows.Count, "H")).SpecialCells(xlCellTypeVisible)(1).FormulaR1C1 = _
        "=VLOOKUP(RC[-6],'[Inventory View.xlsx]Sheet1'!C3:C22,20,FALSE)"
    ActiveSheet.ShowAllData
End If
 
Upvote 0
Not quite. All that does is correctly select the first row of visible cells (after the 2 header rows) and update that row. It doesn't select any other rows that have an #N/A value in column H.
 
Upvote 0
Based on this I did find a way to get what I want, but I'm sure its a horrible way in the grand scheme of things. I can add a loop to the end of this section and update the top row each time until they are gone. I usually only have 4-5 lines that show up this way so it isn't too bad, but could be. I'd still like to find the "proper" way to do this so I know for future refrence.

Code:
If IsError(Range("I1")) = True Then
   Do Until IsError(Range("I1")) = False
    Range("I2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$N$150000").AutoFilter Field:=9, Criteria1:="#N/A"
    Range([H3:H150000], Cells(Rows.Count, "H")).SpecialCells(xlCellTypeVisible)(1).FormulaR1C1 = _
        "=VLOOKUP(RC[-6],'[Inventory View.xlsx]Sheet1'!C3:C22,20,FALSE)"
    ActiveSheet.ShowAllData
    Loop
End If
 
Upvote 0
Not quite. All that does is correctly select the first row of visible cells (after the 2 header rows) and update that row. It doesn't select any other rows that have an #N/A value in column H.

The 'FillDown' statement requires that the source row be included in the range to be filled and the range must be greater than one row. If you used a specific range rather than 'Selection' lhen that statement should work. Provided you have the correct column reference, etc. Personally, I stay away from the select, activate and selection statements unless there are no other alternatives. Learning to use variables efrfectively was the best thing I ever did in using VBA.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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