+ Reply to Thread
Results 1 to 11 of 11

vba for rearranging the data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Chennai,India
    MS-Off Ver
    2007,2003
    Posts
    123

    vba for rearranging the data

    Hi All,

    I want to rearranging the data in year wise for each company. My data (Sheet 1) is in this order.

    Year Company A Company B

    1996 Data 1 Data 1
    .......
    2006 Data 11 Data 2

    I wan to rearrange the data (Sheet 2) in to the following order.

    Company A 1996 Data 1
    ........
    2006 Data 11

    Company B 1996 Data 1
    .........
    2006 Data 11

    Is there any possibility for vba for this soloution. If i will increase the no of companies, whether the vba will work or not.

    I have attached a file for the same

    Waiting for ur Suggestions,
    Attached Files Attached Files
    Last edited by pani_hcu; 08-29-2009 at 06:18 AM. Reason: Got the solution
    Upananda

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba for rearranging the data

    Which sheet is Before and which sheet is After?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Chennai,India
    MS-Off Ver
    2007,2003
    Posts
    123

    Re: vba for rearranging the data

    Hi Jb,

    Sheet 1 is before and Sheet 2 is after.

    Regards,

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba for rearranging the data

    Try this, start the macro with the raw data sheet active, it will create an Output sheet for you.

    Option Explicit
    
    Sub ReArrangeYearly()
    'JBeaucaire  (8/28/2009)
    Dim LR As Long, LC As Long, NR As Long, i As Long
    Dim ws As Worksheet
    
    If ActiveSheet.Name = "Output" Then
        MsgBox "Please start the macro from the data sheet"
        Exit Sub
    End If
    
    Set ws = ActiveSheet
    If Not Evaluate("ISREF(Output!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Output"
        Range("A2") = "Company Name"
        Range("B2") = "Year"
        Range("C2") = "S.R."
        Range("A2:C2").Interior.ColorIndex = 15
        Range("A2:C2").Font.Bold = True
        Range("A2:C2").HorizontalAlignment = xlCenter
        Range("A2:C2").Borders.LineStyle = xlContinuous
        Range("A3").Select
        ActiveWindow.FreezePanes = True
        ws.Activate
    Else
        Sheets("Output").Range("A3:D" & Rows.Count).ClearContents
    End If
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    LC = Cells(2, Columns.Count).End(xlToLeft).Column
    
    Set ws = Sheets("Output")
    NR = 3
    
        For i = 2 To LC
            ws.Cells(NR, "A") = Cells(2, i)
            Range(Cells(3, "A"), Cells(LR, "A")).Copy
            ws.Range("B" & NR).PasteSpecial xlPasteValues
            Range(Cells(3, i), Cells(LR, i)).Copy
            ws.Cells(NR, "C").PasteSpecial xlPasteValues
            NR = NR + Range(Cells(3, "A"), Cells(LR, "A")).Cells.Count
        Next i
        
    ws.Activate
    Columns("B:C").AutoFit
    Range("A3").Select
    End Sub

  5. #5
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Chennai,India
    MS-Off Ver
    2007,2003
    Posts
    123

    Re: vba for rearranging the data

    Hi Jb,

    Thanks a lot. I have a doubt that If I am going to increase the no of companies whether the macro will automatically takes care of that in arranging the columns or not ?

    Thanking you.

  6. #6
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Chennai,India
    MS-Off Ver
    2007,2003
    Posts
    123

    Re: vba for rearranging the data

    Hi,

    It seems the macro is not rearranging the data from the sheet 1. I want the output file should rearrange the data like sheet 2, where i have done for two companies.

    Hope i am clear this time.

    Regards,

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba for rearranging the data

    Here's your own sheet back with the macro in it. I've made no changes to your sheet. Run it, it makes the OutPut sheet just fine.
    Attached Files Attached Files

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,960

    Re: vba for rearranging the data

    here's another one:
    Adjusted closing price(bvj).xls
    Ben Van Johnson

  9. #9
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Chennai,India
    MS-Off Ver
    2007,2003
    Posts
    123

    Smile Re: vba for rearranging the data

    Thanks JB,

    Your Macro has saved a lot of time of mine.

    with sincere regards,

  10. #10
    Forum Contributor
    Join Date
    10-11-2008
    Location
    Chennai,India
    MS-Off Ver
    2007,2003
    Posts
    123

    Re: vba for rearranging the data

    Hi JB,

    I am going to ask ur suggestion regarding one small problem. I want to add data on some more variable from Sheet 2. As my no of companies is not necessary equal all the time. But I want to add more variables from the sheet 2. Which formula i should use to pull the data of only those companies exists on output sheet and "Sheet 2" as well.

    I have attached the file for the reference.

    Thanks and Regards,
    Upananda
    Attached Files Attached Files

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: vba for rearranging the data

    Please start a new thread rather than tacking onto already solved ones. Include links to prior threads only when it seems it would be relevant, which it probably isn't in this case.

+ 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