+ Reply to Thread
Results 1 to 24 of 24

copy number of rows based on a number

Hybrid View

  1. #1
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    copy number of rows based on a number

    Can anyone help me with the following, in trying to create a formula or VBA wich automate the number of copied file.
    I will try to explain with an example:

    A B C D
    circle red 3 Red Circle
    square blue 5 Blue Square
    triangle pink 1 Pink triangle


    based on the number in column C (that is variable) I would like to have the values in D (string created via formula) copied to a specific area in another sheet.

    Hope this clarifies a bit. Would be great if someone can help me!

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    By another sheet do you mean another sheet in the same workbook or in another file?
    Based on what could that specific area be found?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    Yes, same workbook another sheet. In that sheet, the row should be placed in a certain row.

    sheet 1 = 1a Asset production
    sheet 2 = 1b Upload string
    Asset Channel Aligned Filename Keywords
    Standard image Online 4 Standard_image_online.tif Standard image, online, white
    Main image Retailer 2 main_image_retailer.jpg main image, retailer, blue

    Based on the value in 'aligned' i want the filename + keywords copied in this case 2 and 4 times in a different sheet. I understand that base dup on the different values it is hard to let the formula just automate as the number of rows can vary. That is why i think a sertain place in sheet 2 is nessacary to avoid errors.
    Last edited by Aafke; 01-31-2017 at 05:18 AM.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    Best to post an example file with both sheets filled so we can see where goes what.

  5. #5
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    how do I attached that?

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    Click Go Advanced button and scroll down to Manage Attachments.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: copy number of rows based on a number

    Does this help?

    Sub Aafke()
    Dim i As Long
    For i = 2 To Range("C" & Rows.Count).End(3).row
        Cells(i, "D").Resize(, 2).Copy Sheets("another sheet").Range("A" & Rows.Count).End(3)(2).Resize(Cells(i, "C").Value)
    Next i
    End Sub

  8. #8
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    Thank you for the script, unfortunately i receive an error "run-time error '9': Subscript out of range. CAn you specify the VBA on the attached file so i can understand where you're referring to? Thanks in advance

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: copy number of rows based on a number

    What sheet are you copying too. The script is copying too "another sheet". But we need to know the exact destination: otherwise you'll get a subscript out of range. BTW there is no attachment.

  10. #10
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    attachment in this message. Thank you
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: copy number of rows based on a number

    Try:

    Sub Aafke()
    Dim i As Long
    For i = 16 To Range("N" & Rows.Count).End(3).row
        Cells(i, "N").Resize(, 3).Copy
        Sheets("1b upload string").Range("D" & Rows.Count).End(3)(2).Resize(Cells(i, "M").Value).PasteSpecial xlPasteValues
    Next i
    End Sub

  12. #12
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    This is great! it works in the example fiel. I will try to transfer this to my original file... so far so good!! Thanks for the effort!

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: copy number of rows based on a number

    You're welcome. Hope it works out for you.

  14. #14
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    I can't figure out how I insert this to my original file, when I adjust the values between the " " i keep receiving errors. Is there a way i can share my file privately so you can check what im doing wrong?

  15. #15
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: copy number of rows based on a number

    You're welcome. Hope it works out for you.

  16. #16
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    Ah we have the bottleneck.. if there is no value (cell is empty) the script stops. Can we add a addional formatting that when the number "0" is in a cell it means the script skips the row and moves to the next row.

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    Try this.

    Sub Aafke()
    Dim i As Long
    For i = 16 To Range("N" & Rows.Count).End(3).Row
        If Cells(i, "M") <> vbNullString Then
            Sheets("1b upload string").Range("D" & Rows.Count).End(3)(2) _
                    .Resize(Cells(i, "M").Value, 3).Value = Cells(i, "N").Resize(, 3).Value
        End If
    Next i
    End Sub

  18. #18
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    Perfect!! Thanks for your quick answer, it works perfectly!

  19. #19
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    If I add another column that needs to be copied from the file ( so instead 3 columns starting from N there are 4 columns) what do I need to change? When I change all numbers 3 into a 4 the formula doesn't work anymore.
    Can you please assist m on this?

    Sub Aafke()
    Dim i As Long
    For i = 16 To Range("N" & Rows.Count).End(3).Row
        If Cells(i, "M") <> vbNullString Then
            Sheets("1b upload string").Range("D" & Rows.Count).End(3)(2) _
                    .Resize(Cells(i, "M").Value, 3).Value = Cells(i, "N").Resize(, 3).Value
        End If
    Next i
    End Sub
    *sorry
    Last edited by Aafke; 02-08-2017 at 03:43 AM.

  20. #20
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    Please read Forum Rules regarding the use of Code Tags (Rule#3) when posting code.
    Please change your post#19 accordingly.
    To answer your question, in the Resize part of the code change 3 to 4 twice.

  21. #21
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    thanks, sorry for the breaking the rules.. adjusted.

    I would like to add another question, if the range "N" is in a specific file (1a template) how do add this. Can i just mention Sheets (1a template) before the range?

  22. #22
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    Something like this?

    Sub Aafke()
    Dim i As Long, ws As Worksheet
    Set ws = Sheets("1a template")
    For i = 16 To ws.Range("N" & ws.Rows.Count).End(3).Row
        If ws.Cells(i, "M") <> vbNullString Then
            Sheets("1b upload string").Range("D" & Rows.Count).End(3)(2) _
                    .Resize(ws.Cells(i, "M").Value, 3).Value = ws.Cells(i, "N").Resize(, 3).Value
        End If
    Next i
    End Sub

  23. #23
    Registered User
    Join Date
    01-31-2017
    Location
    Amsterdam
    MS-Off Ver
    13
    Posts
    14

    Re: copy number of rows based on a number

    That something, is woking perfectly! thanks a lot!!

  24. #24
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: copy number of rows based on a number

    You're welcome.

+ 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. Copy 2nd,3rd, etc. rows below to a column based on number
    By nickyD1985 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 01-03-2017, 01:52 PM
  2. [SOLVED] insert rows based on number in cell and copy the data down into the new rows
    By pziefle in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-05-2013, 11:19 AM
  3. [SOLVED] Insert Multiple Rows Based Off Number in Cell and Copy Data From Above New Rows
    By tstell1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2012, 04:15 PM
  4. [SOLVED] Excel VBA/Macro - Copy Number of Rows based on the number in a cell to another sheet
    By diane.alexander58 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-15-2012, 11:32 AM
  5. number in cell is number to add number of rows wit copy some contex
    By nicollab in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2012, 08:09 AM
  6. Copy number of rows based on cell value
    By DaveNUFC in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2011, 02:28 PM
  7. How to do this? Copy rows down based on number...
    By davelarue in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-14-2008, 05:42 AM

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