VBA Copy formula down to last row

rplohocky

Board Regular
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?
 

igold

Well-known Member
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
 

rplohocky

Board Regular
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???
 

igold

Well-known Member
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.
 

rplohocky

Board Regular
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.
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top