+ Reply to Thread
Results 1 to 7 of 7

repeat cell value x times

  1. #1
    Registered User
    Join Date
    08-27-2015
    Location
    Victoria, Australia
    MS-Off Ver
    2010
    Posts
    4

    repeat cell value x times

    I am trying to apply a text value to a variable number of cells in one column. The number of times to be repeated is dictated by the numeric value in a cell.

    I.e.
    cell D1 text is "C"
    cell D2 value is "23"
    cell E1 text is "E"
    cell E2 value is "5"

    I have a table containing 33 rows (range A14:F47 including header row). I need a macro to apply the letter "C" in "23" cells starting at cell D15 and then apply the letter "E" in the next "5" cells. This example should result in 28 cells being filled. So, the range to be filled in this example will be D15 through to D42. I already have a macro to hide any unused rows in the table.

    The values in cells D2 and E2 will change depending on other data entered in the worksheet. I.e. D2 could be 11 and E2 could be 4 and the remaining unused rows get hidden.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Really ?!


    Very beginner level , you should try ‼

    PHP Code: 
    Sub Demo()
        
    With Sheet1
                                 
    .[D15].Resize(.[D2].Value).Value = .[D1].Value
            
    .Cells(15 + .[D2].Value4).Resize(.[E2].Value).Value = .[E1].Value
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Registered User
    Join Date
    08-27-2015
    Location
    Victoria, Australia
    MS-Off Ver
    2010
    Posts
    4

    Re: Really ?!

    Thanks for the suggestion but your solution didn't quite work. Although it did point me in the right direction so I am grateful for that.

    My solution is as follows:
    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    Worked like a charm.....not bad for a beginner
    Last edited by 6StringJazzer; 08-29-2015 at 09:06 AM.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: repeat cell value x times


    My code is exactly the reflect of your explanation ! Of course with an error in your explanation …

    Your code uses not necessary object variables (useless for once !) and you forgot to free them !
    At a bigger scale, it's a waste of time for execution and a waste of system ressources, so not so good …

    And you forgot the dots in With block !

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,957

    Re: repeat cell value x times

    Marc L, your response is unnecessarily critical. Because English is not your first language perhaps this was not intentional.

    In your initial response the code is very elegant, but headlining your post with "Really?!" and saying the solution is "beginner level" is condescending, as though you can't believe someone would ask this question. This is not the tone we wish to set in ExcelForum.

    In your follow-up response, although your facts are correct, your tone is to criticize rather than be constructive. The use of four "!" marks makes it seem as you are scolding. And the words "useless", "you forgot", and "waste" are loaded words with negative connotation. In addition, stark efficiency is not always the goal of coding. It is often useful to add variables to make the coder's intent clear. In such a small application, execution time and system resources are negligible and optimizing efficiency would be unnoticed by a user but create dense code that is more difficult to interpret.

    Please participate constructively, reaching out with support, rather than criticizing people who come here for help, especially on their very first post.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,957

    Re: Really ?!

    Quote Originally Posted by Julez74 View Post
    Thanks for the suggestion but your solution didn't quite work.
    I mocked up your file and put that code in and it seemed to work fine. What didn't work?

  7. #7
    Registered User
    Join Date
    08-27-2015
    Location
    Victoria, Australia
    MS-Off Ver
    2010
    Posts
    4

    Re: repeat cell value x times

    Your code should have worked, it made sense. I tried your code again and it still came up with an error to either end or debug. But, this time I actually read the error message (should have done this to begin with, I know). The error was caused by sheet protection. When I removed the sheet protection, your code worked. I have cells with formulas that are protected to stop users from deleting or changing them accidentally. The only reason my version worked was because I tested it while sheet protection was off. Lesson learned.

+ 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. Replies: 1
    Last Post: 07-17-2015, 02:56 AM
  2. How to repeat cell value x times in Excel?
    By kiriguy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-06-2015, 08:12 PM
  3. [SOLVED] Modify a macro to repeat as many times as given cell value.
    By David Obeid in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-23-2015, 04:00 AM
  4. [SOLVED] How do you repeat a value x times then move on to the next cell below for the same?
    By a1c2014 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-18-2014, 06:46 PM
  5. Repeat cell values a certain number of times
    By strother1990 in forum Excel General
    Replies: 2
    Last Post: 09-24-2014, 12:43 PM
  6. Replies: 4
    Last Post: 03-08-2014, 08:01 AM
  7. Repeat a cell value in another column X number of times
    By art1234 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-31-2011, 02:36 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