VBA Copy formula down to last row

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a workbook that has many rows of data. I wrote a macro that filters on column B, then the macro inserts a formula into column C to copy over the first 4 characters. I am using this line of code to bring the characters over filling in to the last row.

<code>
'Below Inserts a formula that brings over just the first 4 letters of column B.
On Error Resume Next
ActiveSheet.Range("C2:C" & ActiveSheet.Cells(rows.Count, 2).End(xlUp).row).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=LEFT(RC[-1],4)"
On Error GoTo 0
Worksheets("Sheet1").Columns(10).Calculate
</code>

The problem is if there is only one row with the filter parameter I get a warning about not enough memory or something. I tested this by adding another fake row that matches my filter parameter just so I could have 2 rows, and it works fine.

Is there a way to modify this code so if there is only 1 row of data it won't give me a warning?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Assuming that the error occurs when you filter the data and there are no results so the only visible row is the header row (Row 1) then how about something like this...

Code:
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
If lastrow = 1 Then Exit Sub
 
Upvote 0
Assuming that the error occurs when you filter the data and there are no results so the only visible row is the header row (Row 1) then how about something like this...

Code:
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
If lastrow = 1 Then Exit Sub

Thanks for responding igold!

The error occurs after the filter has completed. The error occurs because the filtered results only show 1 row of data. The code above that I inserted seems to get stuck when there is only 1 result from the filter. This may happen again in the future so I need the code to only fill in populated rows but if there are no results then just keep moving the remaining macro steps.

Any way to make the macro happy with 1 result or no results???
 
Upvote 0
Hi,

I am not sure that I understand your response as to what is being shown on your sheet. The logic I was implying that may work, is that you have the code count how many rows have been returned from the filter and if it is only one then you have the code move to it's next task skipping over the part of the code which throws the error.
 
Upvote 0
Hi,

I am not sure that I understand your response as to what is being shown on your sheet. The logic I was implying that may work, is that you have the code count how many rows have been returned from the filter and if it is only one then you have the code move to it's next task skipping over the part of the code which throws the error.

My macro works fine when there 2 or rows of data but when the filter only shows one row it errors out. Down the road the filter might find more which I know the macro will work fine but at some point the filter may only show one or none. When this happens my macro is producing an error message because it gets stuck for some reason.

The code I originally put in this thread is what I am using to insert a formula in any row that has data in it but this macro fails when there is only one row of data and I can't figure out why.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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