Results 1 to 5 of 5

Thread: VBA Copy formula down to last row
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Sep 2005
    Posts
    236
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA Copy formula down to last row

    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.


    '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


    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?

  2. #2
    Board Regular igold's Avatar
    Join Date
    Jul 2014
    Location
    Delray Beach, FL, USA
    Posts
    2,402
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA Copy formula down to last row

    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
    ​igold

    I'm a drinker with a coding problem...

    All code is written with Excel 2010 - Please test all code on a backup copy of your data.


  3. #3
    Board Regular
    Join Date
    Sep 2005
    Posts
    236
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Copy formula down to last row

    Quote Originally Posted by igold View Post
    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???

  4. #4
    Board Regular igold's Avatar
    Join Date
    Jul 2014
    Location
    Delray Beach, FL, USA
    Posts
    2,402
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA Copy formula down to last row

    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.
    ​igold

    I'm a drinker with a coding problem...

    All code is written with Excel 2010 - Please test all code on a backup copy of your data.


  5. #5
    Board Regular
    Join Date
    Sep 2005
    Posts
    236
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Copy formula down to last row

    Quote Originally Posted by igold View Post
    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

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •