+ Reply to Thread
Results 1 to 9 of 9

Insert rows and copying values if cells contain values

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2011
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    7

    Insert rows and copying values if cells contain values

    Hey Guys,

    I want to create a macro in Excel. An example of the data is added to this thread.

    First thing you notice when you open the data is the top row with all the different variables.
    Now as you notice this line of data is just one out of a bigger dataset. You also see that not every cell has got a value, this is normal. What I want to achieve is some kind of a transpose, I guess, but I have illustrated this in the dataset as well.

    First Thing:
    So what I need is when a cell contains a value, for instance cell N2, I need the variable name, in this case it's "g", to be copied to the first cell. Then I need the corresponding value, this is 2 in this case, and I also need the corresponding value next to this variable, this is cell O2 and corresponds to 5.
    I call these variables "row specific" since every row will be unique, and the number of rows will be depended on how many cells contain a value. In this case only 2 cells contain a value (yes, 4 indeed but "g and b01g" belong together as do "j" and "b01j").

    Second Thing:
    There are also "fixed" variables. The values of these variables need to be copied as well to the different rows. An example is "ID", cell A2. Other examples are "Fixed" (cell AH2) and "Bpost" (cell AI2).

    Anyways, hope someone can help me and let me know if more explanation is needed!

    Sincerely
    Attached Files Attached Files
    Last edited by Waninio; 12-12-2011 at 07:20 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Insert rows and copying values if cells contain values

    Hi

    Add a new sheet to the workbook, called sheet2 then try
    Sub aaa()
      Dim OutSh As Worksheet
      Set OutSh = Sheets("Sheet2")
      OutSh.Range("A1:F1").Value = Array("ID", "Measure", "Choice", "Importance", "Fixed", "Bpost")
      For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        For j = 2 To 32 Step 2
          If Len(Cells(i, j)) > 0 Then
            outrow = OutSh.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            OutSh.Cells(outrow, 1).Value = Cells(i, 1).Value
            OutSh.Cells(outrow, 2).Value = Cells(1, j).Value
            OutSh.Cells(outrow, 3).Value = Cells(i, j).Value
            OutSh.Cells(outrow, 4).Value = Cells(i, j + 1).Value
            OutSh.Cells(outrow, 5).Value = Cells(i, "AH").Value
            OutSh.Cells(outrow, 6).Value = Cells(i, "AI").Value
          End If
        Next j
      Next i
    End Sub
    HTH

    rylo

  3. #3
    Registered User
    Join Date
    11-23-2011
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert rows and copying values if cells contain values

    Thanks rylo!

    The test worked like a charm. However, I only copied a sample out of the dataset. In fact, I've got over 30 "fixed" variables that I need to copy. How do I add them in the script? I presume that "OutSh.Cells()" should do the trick, one for every variable. However, what values do I need to use then?

    Thanks a million!

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Insert rows and copying values if cells contain values

    Hi

    Well you could just continue with the structure of the
    outsh.cells(outrow,?).value = cells(i,"??").value
    where ? is the column that gets the fixed variable and "??" is the column that contains it.

    However, if you have a block of fixed variables that are in contiguous cells, then you may be able to transfer / copy a block of cells more easily. Can you give a more detailed example of your entire structure and what has to be copied.

    rylo

  5. #5
    Registered User
    Join Date
    11-23-2011
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert rows and copying values if cells contain values

    Hi rylo,

    I've attached the full document. All values are in sheet "TEST". However the variable names have changed a bit. Everything, except the following, are "fixed" so to say:
    maat01a & belang01a again belong together
    and these go up to maat01p & belang01p (T1:AY1). In the previous example I gave them a simpler name.

    The structure of the output is in the other sheet "Uitkomst". The fixed output variables are cells A1 and E1:AR1; the variable output variables are cells B1,C1,D1.

    I hope you still get it but the previous macro worked fabulous!

    Greetings
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Insert rows and copying values if cells contain values

    Hi

    Can you update your example file with an expected output from the sample data. I can find most of the items, but don't have the foggiest where the data for B1, C1 and D1 is supposed to come from, or where the output from T to AY is supposed to go on the output sheet.

    rylo

  7. #7
    Registered User
    Join Date
    11-23-2011
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert rows and copying values if cells contain values

    I updated the file with the first ID, number 908.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Insert rows and copying values if cells contain values

    Hi

    Think I've matched them all.

    Sub aaa()
      Dim OutSH As Worksheet
      Set OutSH = Sheets("Uitkomst")
      
      Sheets("TEST").Activate
      For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        For j = 20 To 50 Step 2
          If Len(Cells(i, j)) > 0 Then
            outrow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            OutSH.Cells(outrow, 1).Value = Cells(i, 1).Value
            OutSH.Cells(outrow, 2).Value = Right(Cells(1, j).Value, 1)
            OutSH.Cells(outrow, 3).Value = Cells(i, j).Value
            OutSH.Cells(outrow, 4).Value = Cells(i, j + 1).Value
            OutSH.Cells(outrow, 5).Resize(1, 18).Value = Cells(i, 2).Resize(1, 18).Value
            OutSH.Cells(outrow, 23).Resize(1, 22).Value = Cells(i, 52).Resize(1, 22).Value
          End If
        Next j
      Next i
      
    End Sub
    If this doesn't match, then give the cell references of where the data in Uitkomst comes from in TEST.

    rylo

  9. #9
    Registered User
    Join Date
    11-23-2011
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert rows and copying values if cells contain values

    Yes indeed, I calculated the number of cells containing a value and it equals 1690. Great, thanks! This saved me a lot of time and moreover a smaller chance to make errors ;-)

+ 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