+ 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

Hybrid View

  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.

    Sub InputProductName()
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim sh3 As Worksheet
    Dim sh4 As Worksheet
    Dim sh5 As Worksheet
    
    Set sh1 = ActiveWorkbook.Sheets("AD MOSTAZA Y MIEL")
    Set sh2 = ActiveWorkbook.Sheets("AD SOYA Y LIMON")
    Set sh3 = ActiveWorkbook.Sheets("AD ACHIOTE")
    Set sh4 = ActiveWorkbook.Sheets("AD CESAR")
    Set sh5 = ActiveWorkbook.Sheets("AD CASA")
    LastRow = Range("A1:A" & Range("A1").End(xlDown).Row).Rows.Count
    
    sh1.Range("B3").Value = "AD MOSTAZA Y MIEL"
    sh1.Range("B3" & ":B" & LastRow).FillDown
    
    sh2.Range("B3").Value = "AD SOYA Y LIMON"
    Sh2.Range("B3" & ":B" & LastRow).FillDown
    
    sh3.Range("B3").Value = "AD ACHIOTE"
    sh3.Range("B3" & ":B" & LastRow).FillDown
    
    sh4.Range("B3").Value = "AD CESAR"
    sh4.Range("B3" & ":B" & LastRow).FillDown
    
    sh5.Range("B3").Value = "AD CASA"
    sh5.Range("B3" & ":B" & LastRow).FillDown
    
    End Sub

  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!

    Sub InputProductName()
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim sh3 As Worksheet
    Dim sh4 As Worksheet
    Dim sh5 As Worksheet
    
    Set sh1 = ActiveWorkbook.Sheets("AD MOSTAZA Y MIEL")
    Set sh2 = ActiveWorkbook.Sheets("AD SOYA Y LIMON")
    Set sh3 = ActiveWorkbook.Sheets("AD ACHIOTE")
    Set sh4 = ActiveWorkbook.Sheets("AD CESAR")
    Set sh5 = ActiveWorkbook.Sheets("AD CASA")
    
    sh1.Range("B3").Value = "AD MOSTAZA Y MIEL"
    LastRow = Range("A1:A" & Range("A1").End(xlDown).Row).Rows.Count
    sh1.Range("B3" & ":B" & LastRow).FillDown
    
    sh2.Range("B3").Value = "AD SOYA Y LIMON"
    LastRow = Range("A1:A" & Range("A1").End(xlDown).Row).Rows.Count
    sh2.Range("B3" & ":B" & LastRow).FillDown
    
    sh3.Range("B3").Value = "AD ACHIOTE"
    LastRow = Range("A1:A" & Range("A1").End(xlDown).Row).Rows.Count
    sh3.Range("B3" & ":B" & LastRow).FillDown
    
    sh4.Range("B3").Value = "AD CESAR"
    LastRow = Range("A1:A" & Range("A1").End(xlDown).Row).Rows.Count
    sh4.Range("B3" & ":B" & LastRow).FillDown
    
    sh5.Range("B3").Value = "AD CASA"
    LastRow = Range("A1:A" & Range("A1").End(xlDown).Row).Rows.Count
    sh5.Range("B3" & ":B" & LastRow).FillDown
    
    End Sub

  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

    LastRow = sh1.Range("A1:A" & sh1.Range("A1").End(xlDown).Row).Rows.Count

  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.
    Sub myTest()
        Dim ws As Worksheet
        Dim lRow As Long
        Dim sName As String
        Dim i As Integer
            For i = 1 To Sheets.Count
            lRow = Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row
                sName = InputBox("Enter Value to Input")
                Sheets(i).Range("b1:b" & lRow).Value = sName
            Next i
    End Sub
    Give Feedback and Click(*)

  7. #7
    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

  8. #8
    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 

  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