Consulting

Results 1 to 7 of 7

Thread: Appends the contents to the respective columns

  1. #1
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location

    Exclamation Appends the contents to the respective columns

    hi,

    I have a sheet named "Sample" and "Data". Sample is the master datasheet where i used to append the datas from the DATA sheet to the corresponding columns in the SAMPLE sheet.

    Datas in the DATA sheet varies daily. for ex in the sample sheet the below column names are standard ones.

    Anu |Asha |Chris | janu | NPO| PO | Shalu| vani| surya| swami

    Whereas in the DATA sheet it depends the 1st name may be ASHA, then Janu.... Asha data from data column to be entered in the asha column of sample sheet....

    This i was doing manually. Can this be done using macros... if so help me out in this please.

    thanks in advance !
    -Sindhuja

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Option Explicit
    Sub CopyData()
    Dim Rng As Range
    Dim Rw As Long, Col As Long
    Dim i As Long, j As Long
    With Sheets("data")
    'Find last cell
    Col = Cells(1, Columns.Count).End(xlToLeft).Column
    Rw = Cells(Rows.Count, 1).End(xlUp).Row
    Set Rng = Range(Cells(1, 1), Cells(Rw, Col))
    'Loop though rows and columns
    For i = 2 To Rng.Rows.Count
    Rw = Sheets("Sample").Cells(Rows.Count, 1).End(xlUp).Row + 1
    Sheets("Sample").Cells(Rw, 1) = .Cells(i, 1)
    For j = 2 To Rng.Columns.Count
    'Find column for data
    Col = Sheets("Sample").Rows("1:1").Find(.Cells(1, j), lookat:=xlWhole).Column
    Sheets("Sample").Cells(Rw, Col) = .Cells(i, j)
    Next j
    Next i
    End With
    'Format cells
    With Sheets("Sample")
    .Range("A2:K2").Copy
    Range(.Cells(2, 1), .Cells(Rw, 11)).PasteSpecial Paste:=xlPasteFormats
    .Activate
    .Range("A1").Select
    End With

    Application.CutCopyMode = False

    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Thanks for the quick Response md....

    -Sindhuja

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    As it's a quiet day
    [VBA]
    Sub CopyData2()
    Dim Source As Range
    Dim tgt As Range
    With Sheets("data")
    Set Source = Range(.Cells(2, 1), .Cells(2, 1).End(xlDown))
    End With
    With Sheets("sample")
    Set tgt = .Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End With
    Source.Copy tgt
    Set tgt = tgt.Offset(, 1).Resize(Source.Rows.Count, 10)
    With tgt
    tgt.FormulaR1C1 = _
    "=OFFSET(data!R1C1,MATCH(RC1,data!C1,0)-1,MATCH(R1C,data!R1,0)-1,1,1)"
    .Offset(-1).Resize(1, 10).Copy
    .PasteSpecial xlPasteFormats
    .Copy
    .PasteSpecial xlPasteValues
    .SpecialCells(xlCellTypeConstants, 16).ClearContents
    End With
    Sheets("sample").Activate
    Range("A1").Select
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Hi md,

    Just a query! Both the codes given above produce the same results...
    wat makes the difference between those two..

    -Sindhuja

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The first moves the data one cell at a time. The second populates the Sample sheet with formulae to return the values, then tidies up.
    Try stepping through both codes with Sample worksheet open.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Great ! Thanks for the info Md...

    -Sindhuja

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •