+ Reply to Thread
Results 1 to 9 of 9

Macro to fill column with text - How to make it stop at the last used row

  1. #1
    Registered User
    Join Date
    11-11-2013
    Location
    Mexico City, Mexico
    MS-Off Ver
    Excel 2007
    Posts
    24

    Macro to fill column with text - How to make it stop at the last used row

    Hello Forum,

    I have an excel document with a few dozen sheets. In each sheet column B is empty and I need to fill that column (starting from the third row –B3) all the way down to the last used row of the sheet with a specific text (a product name).
    Each sheet has a different text that needs to be filled in.

    I am new to VBA but with a lot of research and ´forum-hunting´ I pieced together a macro.
    The Macro references column A to establish in which row it should stop inputting text in column B.

    My problem is the following:
    The macro works great on the first sheet. (sh1 ¨AD MOSTAZA Y MIEL¨).
    BUT in all the rest of the sheets it keeps inputting the text far past the last row used!

    Can anyone help me fix it please?

    P.S.: I only posted the Macro with 5 sheets as an example. The Macro I need to use is pretty much the same just for more sheets.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Macro to fill column with text - How to make it stop at the last used row

    You need to re-calculate LastRow for each worksheet separately.
    Gary's Student

  3. #3
    Registered User
    Join Date
    11-11-2013
    Location
    Mexico City, Mexico
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to fill column with text - How to make it stop at the last used row

    Thanks for such a quick reply Jakobshavn!

    I tried inserting the LastRow for each worksheet (pls see below) as well and it did not work
    I am getting the same problem as before...

    I also tried putting sh1.LastRow = ... , sh2.LastRow = ... etc and this gives me an error message.

    How would you input the LastRow-line?


    Thanks your help so far!

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Macro to fill column with text - How to make it stop at the last used row

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-11-2013
    Location
    Mexico City, Mexico
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to fill column with text - How to make it stop at the last used row

    Yes! Thank you! Thank you!

  6. #6
    Forum Contributor
    Join Date
    03-28-2013
    Location
    *
    MS-Off Ver
    Excel 2010
    Posts
    226

    Re: Macro to fill column with text - How to make it stop at the last used row

    I know you got that what you wanted, to make short your code you can use this.
    every time you run this code it will pop up the InputBox and then just you have to enter the value that you want insert in that sheet.
    Please Login or Register  to view this content.
    Give Feedback and Click(*)

  7. #7
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Macro to fill column with text - How to make it stop at the last used row

    PHP Code: 
    Sub myTest()
        
    Dim Sh As Worksheet
            
        
    For Each Sh In ThisWorkbook.WorkSheets
           
    If Left(Sh.Name,2)=  "AD" Then          
              Sh
    .Range([b1], [B1].End(xlDown)).Value Sh.Name
           End 
    If
        
    Next Sh  
    End Sub 

  8. #8
    Registered User
    Join Date
    11-11-2013
    Location
    Mexico City, Mexico
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to fill column with text - How to make it stop at the last used row

    Venkatpvc - Thanks for that! I may not use it for this file but it is definitly useful for some other macros I am trying to write

  9. #9
    Registered User
    Join Date
    11-11-2013
    Location
    Mexico City, Mexico
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: Macro to fill column with text - How to make it stop at the last used row

    SA DQ - I love the idea of having a shorter Macro like you wrote and have it input the ´text´ according to the sheet name.

    However I am having some issues with it:
    - It only inputs the info on the first sheet and then it comes up with an error message (error 1004 - Application defined or object defined error)
    - The product names are inputted much past the last used row (column B is filled all the way to row 10.000+ and I only have about 100 rows of data)

    Could you help me? I would really appreciate it!

+ 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] Stop fill at last text in column B not the last coloured cell.
    By Wales MB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-01-2013, 12:15 PM
  2. make a column from text in macro
    By azbi in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-07-2013, 05:23 AM
  3. [SOLVED] Macro fill cell cells with text from another column
    By forfiett in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-16-2012, 08:29 AM
  4. Macro fill the text in the specific column
    By ronlau123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-23-2011, 10:08 AM
  5. Stop macro at the end of data / make macro faster
    By Shaner73 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 08-17-2010, 08:14 AM

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