+ Reply to Thread
Results 1 to 9 of 9

Do While Loop Q: Copying sheet data ranges to another sheet's ranges

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    The first column in Sheet1 will be a range of numbers in a series. I am taking that total amount of items in that column and using it as a variables so that my do while loop will repeatedly copy a range of columns until the rows are completely copied over.
    The first two columns of Sheet1 will have varying numbers but the other columns will be consistently alike. I am missing a part for how to copy the range to a particular range on Sheet2. Can someone please help?

        Sub cpySht2Sht()
            
            Dim cpyFrm As Range
            Set cpyFrm = Sheets(1).Range("C3:D3") ''A range of columns
            
            Dim qty As Integer ''Variable to hold qty, number of times to loop
            Dim finQty As Integer ''Loop variable to check against, for finalizing loop
            
                    qty = Worksheets("Sheet1").Cells(2, 6) ''Assign amount of id's from cell to variable
                    
                    
            Do While finQty < qty 'Loop through until number of row entries are complete
            
            Sheets(2).Cells.Value = cpyFrm 'Copy the values of the first sheet's columns to that of the second sheet
            ActiveCell.Offset(1, 0).Select 'Go down to the next row after a row has been copied to above?
            finQty 1 'increment the loop's test number
    
        Loop
            
        End Sub
    I've uploaded the example if that helps.

    Thanks so much in advance.

    EDIT: Line should have read:
    qty = Worksheets("Sheet1").Cells(2, 6) ''Assign amount of id's from cell to variable

    Also, F2 should have had a value of 12 in the spreadsheet.
    Attached Files Attached Files
    Last edited by foolios; 07-28-2011 at 07:36 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    Does Row 2 of Sheet 1 have to be empty?
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    hehe, nope, not at all.

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    Is there a more specific purpose for this copy? Does sheet 1 get cleared out and then new data put in or, is the data coming into and staying in sheet 1-and then transferred? I ask because if it is just a straight copy and previous data is going to be overlapped anyways, you could just as easily select the columns from one sheet and copy them to the other. Or, you can type the following into sheet 2 starting at A2: =Sheet1!A3 (because of the row with no values). Once you have that, drag and copy it across to D2,, and then drag and copy down.

  5. #5
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    Yes, there will be a purpose. I will transfer the functionality to an already created workbook filled with functionality.
    Yes, Sheet1 will get new data after a copy has been done to sheet2. The second sheet will get uploaded to a database after sheet1 data is copied to it.
    I am still working on the button to copy data from one sheet to the other. I couldn't continue with the workbook to workbook idea. It's just too complex atm. So I realized I can put the workbooks together as two separate sheets.
    The reason why I would like a button is that there is going to be an unknown number of rows of data entered. The number of columns will always be known, but the number of rows won't. And the number of rows could be in the hundreds to thousands.
    3 columns will vary in data. Most of the columns will be duplicated, which is why I was thinking the loop would be ideal.The first column will have a series of numbers. I was thinking that I could base the loop off of that.

    EDIT:

    For as many rows as there are ID#'s, loop through copying the cells from one sheet to the other while realizing that 3 of the columns will not be identical but that the rest will be the same info as in the first row copied.
    The ID# column is the most important column. That amount of id's is what will create the number of rows. If there are 200 id#'s, there will be 200 rows of data.
    One thing more to consider that I think will help clear up why I need to copy from one sheet to the other.
    In the original workbook, the ID#'s are not all written out.
    I only have two cells with a beginning ID# and en ending ID#. These numbers will be following in series from first to last.
    So, the first sheet is only going to have two ID#'s. But it's that amount of them(the entire series) that I have to generate the data into the second sheet.
    I'm hoping the loop will consider the amount of ID#'s, copy the other columns as many times as necessary to fill the rows in the second sheet to match that amount.
    I hope that clarifies it better.

    EDIT: I am going to alter the first sheet to match the ID# scenario then upload it so you can see what I mean.
    Last edited by foolios; 07-28-2011 at 09:41 PM.

  6. #6
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    ok, it's been updated to show the range of ID#'s.
    Attached Files Attached Files

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    foolios,

    Give the following a try:
    Sub CopyIDRowsMacro_for_foolios()
        
        If WorksheetFunction.CountA(ActiveSheet.Cells(2, 1).EntireRow) < 2 Then ActiveSheet.Cells(2, 1).EntireRow.Delete xlShiftUp
        Static ur As Range: Set ur = ActiveSheet.UsedRange
        ur.AutoFilter Field:=1, Criteria1:="<>"
        With ActiveSheet.AutoFilter.Sort
            .SortFields.Clear
            .SortFields.Add Key:=ur.Resize(ur.Rows.Count, 1)
            .Header = xlYes
            .Apply
        End With
        Static vc As Range: Set vc = ur.Offset(1, 0).Resize(ur.Rows.Count - 1, ur.Columns.Count).SpecialCells(xlCellTypeVisible)
        vc.Copy Sheets("Sheet2").[A2]
        ur.AutoFilter
        
    End Sub


    Hope that helps,
    ~tigeravatar

  8. #8
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    I gave it a whirl and tried to comment out the filtering.
    After taking out the filtering, I get an error on the line:
    'Static vc As Range: Set vc = ur.Offset(1, 0).Resize(ur.Rows.Count - 1, ur.Columns.Count).SpecialCells(xlCellTypeVisible)

    I don't understand that line enough to fix it.
    I played with the code and it copies the first sheet of data to the second sheet which is great.
    But what it needs to do is realize that there are 164 lines(which can change, it's not always going to be 164) of ID#'s to create rows for.
    It has to take into consideration that the id's will increment. The ID# column and the Serial # column will not be identical between each row of data, but most of the rest of the row's data will be the same as the row above it.

    Sub CopyIDRowsMacro_for_foolios()
        
        'If WorksheetFunction.CountA(ActiveSheet.Cells(2, 1).EntireRow) < 2 Then ActiveSheet.Cells(2, 1).EntireRow.Delete xlShiftUp
        Static ur As Range: Set ur = ActiveSheet.UsedRange
        'ur.AutoFilter Field:=1, Criteria1:="<>"
        'With ActiveSheet.AutoFilter.Sort
        '    .SortFields.Clear
        '    .SortFields.Add Key:=ur.Resize(ur.Rows.Count, 1)
        '    .Header = xlYes
        '   .Apply
        'End With
        'Static vc As Range: Set vc = ur.Offset(1, 0).Resize(ur.Rows.Count - 1, ur.Columns.Count).SpecialCells(xlCellTypeVisible)
        Static vc As Range
        Set vc = ur
        vc.Copy Sheets("Sheet2").[A2]
        'ur.AutoFilter
        
    End Sub

    The sorting is a very cool feature but not necessary. Actually, it confuses me further so I tried to leave it out.

    Thanks for showing me your idea. I am stuck with what I've pasted here since I don't know how to leave out the header column in copying the pages data.
    I still believe a do while loop is necessary to create the rows of data on the next sheet considering how many ID#'s there are.
    I will keep at this in the meantime. Thank you.

    EDIT:
    My uploaded workbook will create confusion. I am sorry about that.
    I should have separated the ID# column from the rest of the columns of data on the Sheet1 to be copied to Sheet2.

    It should really look more like:
    Serial #'s	Info1	Info2
    300456987     Desc 	color
    4325425	       Desc	color
    and then separately on Sheet1 there should be a column that identifies the range of ID#'s:
    ID#'s
    10
    thru
    173
    Which would be 174 ID#'s(rows of data that have the serial # and info1 and info2) into the second sheet.

    So, there is going to be two cell's on Sheet1 that identify how many ID#'s are going to be created and copied to Sheet2.
    The copy function will take an ID# and it's associated data to include a serial# and info1 and info2 and paste that information onto a row in Sheet2.

    In hopes of simplifying this even more, I am thinking I can take one cell(one row of data) that would include info1 and info2 and have that repeatedly copied and pasted for each ID#'s row that is created.
    I can set the Serial #'s to increment along with the ID#'s as well.
    So an ID# will increment from 10 to 11 to 12 and so on. The serial # that coincides with the ID# can increment as well from 2980 to 2981 to 2982.
    idVar = nmbrOfIds
    idVar2 = nmbrOfSers

    idVarFin = 0
    do while idVarFin < idVar
    Sheet2.cell(2,1) = idVarBegIDNumbr
    Sheet2.cell(2,2) = Sheet1.cell(10,12) 'cell with info1
    Sheet2.cell(2,2).nextRow
    idVar += 1
    idVarBegIDNumbr += 1
    'serial# code to match id# code

    This is just my imagination of how it would work. I don't know what the exact coding of this is yet.
    I will try to work on making two variables that will increment based on the number of ID#'s. One variable will hold the beginning ID# and the other will hold the beginning Serial #.
    Then I will add code that will check if the previous cell has a serial#. If so, copy cell data from sheet1 info1 to it.

    That just might work.
    Last edited by foolios; 07-29-2011 at 09:12 PM.

  9. #9
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Do While Loop Q: Copying sheet data ranges to another sheet's ranges

    How do I look up a next row command in the library for Office/VBA?
    I am so frustrated. Do I look in the SDK or the MSDN?
    The links for MSDN have me going around in circles.
    How do I look up the next row command?

    This is what I have so far. What I need is a way to go down a row.

    Public Sub copyToSheet2Macro()
    
    Dim coNoVar As Integer
    Dim serNoVar As Integer
    Dim amntOfCoNo As Integer
    Dim tstFinAmnt As Integer
    Dim Sh_1 As Worksheet
    Dim Sh_2 As Worksheet
    Set Sh_1 = ActiveWorkbook.Sheets("Sheet1")
    Set Sh_2 = ActiveWorkbook.Sheets("Sheet2")
    
    coNoVar = Cells(3, 1)
    serNoVar = Cells(3, 2)
    amntOfCoNo = Cells(2, 6)
    
    Do While tstFinAmnt < amntOfCoNo
    
    Sh_2.Cells(1, 1) = coNoVar
    tstFinAmnt = tstFinAmnt + 1
    coNoVar = coNoVar + 1
    serNoVar = serNoVar + 1
    '''---Need a next row command---'''
    Loop
    
    'MsgBox ("co= " & coNoVar & " ser= " & serNoVar)
    
    End Sub
    Thanks for any info.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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