+ Reply to Thread
Results 1 to 11 of 11

Setting Work Range as only populated rows

  1. #1
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Exclamation Setting Work Range as only populated rows

    HI,

    Attempting to prefix any populated cells with a project title. The projet title is in cell d2. I do not have the sheet title as the sheet is created by copying and pasting a generic sheet and populating it with info from a userform (this is to create a project page when people use this central document). The person then manually types milestones in. Before I copy and paste these milestones from this page to a generic milestone page, I need it to prefix every milestone with the project title. I need the code to stop at the last row with data in it but for some reason I am really struggling.

    Here is what I have (The Set WorkRng line is just made up and is what I really need help with)

    Dim lastRow As Long
    Dim WorkRng As Range
    Dim addstr As String
    lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    addstr = Range("d2").Value
    Set WorkRng = Range("B:B" & lastRow)
    For Each Rng In WorkRng
    Rng.Value = Rng.Value & addstr
    Next

    End Sub




    Help!?

    The simpler the code the better, i like to be able to understand what I am using

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Setting Work Range as only populated rows

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Re: Setting Work Range as only populated rows

    thanks

    The code is still not stopping it just keeps on going! Also it is adding it as a suffix not a prefix......any advice?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Setting Work Range as only populated rows

    The code will loop until the last row in column B.
    What is this line?

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Re: Setting Work Range as only populated rows

    It is currently not stopping (I change the B1 to B19 as that is where the data will be)

    To add the string of value ("d2") ... (unless i have completely got it all wrong)

  6. #6
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Re: Setting Work Range as only populated rows

    Ok i swapped them around so it prefixes instead of adding it on at the end

    However it still just carries on adding the title even when there is no existing data in the cell

  7. #7
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Re: Setting Work Range as only populated rows

    I have columns b-d merged from b19 downwards, will this affect it?

  8. #8
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Re: Setting Work Range as only populated rows

    Also, above B19 i have some rows populated and some not populated but these arent included in the milestones i want to pre fix. Though i am guessing this might affect the endup function?

    Would it be easier to just use a loop? Is there a simple way to do this code for each cell and offset by (0,1) until the cell is empty?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Setting Work Range as only populated rows

    You can have either a fixed or variable loop. You can also stop a loop at any point in time.
    You are using a loop. No, it is a bad coding to use Until loop. For each will do the job.

  10. #10
    Registered User
    Join Date
    07-30-2014
    Location
    Derby
    MS-Off Ver
    2010
    Posts
    17

    Re: Setting Work Range as only populated rows

    Thanks - I am not sure what Im doing with for. Managed to get the until loop to work for now

    using the following:


    Sub Test2()
    Dim rng As Range
    Dim addstr As String
    addstr = Range("d2").Value

    Range("B19").Select
    Do Until IsEmpty(ActiveCell)
    ActiveCell.Value = addstr & ActiveCell.Value
    ActiveCell.Offset(1, 0).Select
    Loop
    Exit Sub
    End Sub

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Setting Work Range as only populated rows

    Please Login or Register  to view this content.
    Loop

    For each will loop until the last row- From B19 until the last cell in column B.

    Do until will stop once it finds empty cell in column B, which is not necessarily the same as the last row as there may be empty rows before the last row. Please attach your sample to spare us going around the circle.

+ 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] data validadion doesn't work when worksheet is populated from userfrom??????
    By ciapul12 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2014, 03:53 PM
  2. [SOLVED] Trying to write a Loop to perform calculations within blank rows between populated rows
    By ObliviousAmI in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 01-15-2014, 06:35 PM
  3. Replies: 5
    Last Post: 02-18-2012, 11:03 AM
  4. Why would a Range including over 524 rows not work?
    By jebediah in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-05-2007, 02:24 PM
  5. Why does Range.Rows.Insert sometimes not work?
    By GaryAIS in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-23-2006, 06:45 PM

Tags for this Thread

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