+ Reply to Thread
Results 1 to 10 of 10

[SOLVED] VBA Code to autofill formula till last row with data

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 365 & Excel 2016
    Posts
    36

    [SOLVED] VBA Code to autofill formula till last row with data

    Hello,

    I've searched here and there and see that a similar question has been raised many times, however was still unable to come up with a solution for myself

    This is the thing:

    I have data in Column A (Range A1:A15).

    In Column B (Range B1:B1 = Cell B1) I have the following formula: "=MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1))" which works fine and is returning me value in Cell B1, based on Cell A1.

    I want a simple line VBA code, which autofill till the last row with data (in this case Cell A15) within Cell B2 till Cell B15.

    And since the data in Column A may vary let's say I have not 15, but 50, 60, or 100 rows, this code to be suitable respectively to autofill till B50, B60, or B100.

    P.S. I've found several possible solutions, which worked for other users with similar problems, however all of them were kind of "code within the code = first they define the range A as dynamic, but for this they use a separate code. Since I am not very familiar with VBA, is there more simple way to do that? I mean just to enter the code as one row within my code ...

    A 'solution' like the below one offered for me is not really working for a newbie like me

    Sub
    Dim last_row As Long

    last_row = Cells(Rows.Count, 2).End(xlUp).Row

    Range("B1").Formula = "=MY_FORMULA_HERE"

    Range("B1").AutoFill Destination:=Range("B1:B" & last_row)

    EndSub
    Attached Files Attached Files
    Last edited by Air^Canada; 01-24-2023 at 09:58 AM. Reason: solved

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,448

    Re: VBA Code to autofill formula till row count

    If the data that you are basing the amount to fill on is in column A then the below will be the fix:
    Sub test()
        Dim last_row As Long
        
        last_row = Cells(Rows.Count, 1).End(xlUp).Row
        Range("B1").Formula = "=1"
        Range("B1").AutoFill Destination:=Range("B1:B" & last_row)
    End Sub
    In the bit 'Cells(Rows.Count, 1)' the 1 is the column number, as it was 2 it was looking in column B to see how far to fill the data. Change it to 1 as above and it will look at column A for the last row of data.
    If things don't change they stay the same

  3. #3
    Registered User
    Join Date
    06-04-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 365 & Excel 2016
    Posts
    36

    Re: VBA Code to autofill formula till row count

    Not working, or I am doing it wrong:
    "Compile Error: Wrong number of arguments or invalid property assignment"

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,046

    Re: VBA Code to autofill formula till row count

    Does this work for you?

    Sub test2()
    Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).ClearContents
    Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = "=MID(LEFT(A1,FIND("">"",A1)-1),FIND(""<"",A1)+1,LEN(A1))"
    End Sub
    Last edited by ByteMarks; 01-23-2023 at 12:44 PM.

  5. #5
    Registered User
    Join Date
    06-04-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 365 & Excel 2016
    Posts
    36

    Re: VBA Code to autofill formula till row count

    Not working again. Still not sure if I am doing it correctly. For a better illustration, I am adding an example sheet to my initial post.

  6. #6
    Registered User
    Join Date
    06-04-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 365 & Excel 2016
    Posts
    36

    Re: VBA Code to autofill formula till row count

    Quote Originally Posted by ByteMarks View Post
    Does this work for you?

    Sub test2()
    Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row).ClearContents
    Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = "=MID(LEFT(A1,FIND("">"",A1)-1),FIND(""<"",A1)+1,LEN(A1))"
    End Sub
    This is the solution I needed, and it works perfectly fine (not sure what I did previously, but now it's okay. Thank you all once more

  7. #7
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,448

    Re: VBA Code to autofill formula till last row with data

    Maybe this:
    Sub test()
        Dim last_row As Long
        
        last_row = Cells(Rows.Count, 1).End(xlUp).Row
        Range("B1").Formula = "=TEXTAFTER(TEXTBEFORE(A1,"">""),""<"")"
        Range("B1").AutoFill Destination:=Range("B1:B" & last_row)
    End Sub
    Or maybe this:
    Sub test2()
        Dim last_row As Long
        
        last_row = Cells(Rows.Count, 1).End(xlUp).Row
        Range("B1:B" & last_row) = Evaluate("=TEXTAFTER(TEXTBEFORE(A1:A" & last_row & ","">""),""<"")")
    End Sub

  8. #8
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,046

    Re: VBA Code to autofill formula till last row with data

    Can't see an issue with the my post.
    1. Right-click Sheet1 and choose view code
    2. Paste code into the window that opens
    3. Press F5

  9. #9
    Registered User
    Join Date
    06-04-2012
    Location
    Bulgaria
    MS-Off Ver
    Excel 365 & Excel 2016
    Posts
    36

    Re: VBA Code to autofill formula till last row with data

    Quote Originally Posted by ByteMarks View Post
    Can't see an issue with the my post.
    1. Right-click Sheet1 and choose view code
    2. Paste code into the window that opens
    3. Press F5
    Now it works!

  10. #10
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2501
    Posts
    1,448

    Re: VBA Code to autofill formula till last row with data

    I have a feeling it was more to do with the extra """" in the formula part rather than any issues with code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need VBA code to fill header values and drag formula till end of last row on target column
    By MicroTees in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-20-2022, 04:33 PM
  2. need vba code to fill formula where cursor start till end of result in header value
    By MicroTees in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-30-2021, 12:03 PM
  3. Code to Add multiplication formula till the last row
    By saravanan1981 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2019, 05:20 PM
  4. [SOLVED] VBA to autofill number till last row
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-21-2014, 01:16 PM
  5. [SOLVED] How to autofill any formula till last empty cell
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-16-2013, 08:56 AM
  6. Count till met criteria, reset counter and count further
    By Romas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2011, 01:42 PM
  7. [SOLVED] VBA code to Autofill one cell to many rows below where row count will change
    By TrainingGoddess in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-27-2005, 11:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1