+ Reply to Thread
Results 1 to 4 of 4

Moving headers and cell information

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-04-2008
    Posts
    103

    Moving headers and cell information

    I have an extensive Excel file that was converted from a PDF. That is my problem because it does not put the information on the sheet the way I need it to so I can upload it to a database. Right now I have to type in all the information which is taking forever. Here is what I need. You would be saving me tons of hours if this can be done.

    The file is attached: It needs to look like this:

    StudentID (Which in this example is 169058) needs to have it's own column
    Name, (In this example is Doe, John) needs to have it's own column
    DOB
    Description, own column
    Teachers, own column
    H1, H2, H3, H4, Fin, Avg Fin, all have it's own column.

    I don't need the rest. As you will see in the attachment the headers are on the top and the information is inside the cells. I have 2500 students like this. So hopefully it would look like this:

    StuID, Name, DOB, Description, Teacher, H1, H2, H3, H4, Fin, Avg Fin.

    Can this be done?
    Attached Files Attached Files
    Last edited by taichi56; 02-25-2011 at 06:08 PM. Reason: SOLVED

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Moving headers and cell information

    Hello taichi56,

    The macro below has been added to the attached workbook. The reformatted results are appear on "Sheet2". There is button to run the macro in cell "B1".
    Sub ReformatData()
    
      Dim Data(1 To 10) As Variant
      Dim DstRng As Range
      Dim DstWks As Worksheet
      Dim N As Long, R As Long
      Dim RegExp As Object
      Dim SrcRng As Range
      Dim SrcWks As Worksheet
      
        Set SrcWks = Worksheets("Sheet1")
        Set DstWks = Worksheets("Sheet2")
        
          R = 1
          
          Set DstRng = DstWks.Range("A2")
            DstWks.UsedRange.Offset(1, 0).ClearContents
          
          Set RegExp = CreateObject("VBScript.RegExp")
            RegExp.IgnoreCase = True
        
            Do While SrcWks.Cells(R, "A") <> ""
              Set SrcRng = SrcWks.Cells(R, "A").CurrentRegion
                RegExp.Pattern = "(\d+)\s+(.+)"
                  Data(1) = RegExp.Replace(SrcWks.Cells(R, "A"), "$1")
                  Data(2) = RegExp.Replace(SrcWks.Cells(R, "A"), "$2")
                RegExp.Pattern = "DOB\:\s(.+)"
                  Data(3) = RegExp.Replace(SrcWks.Cells(R, "C"), "$1")
                    For R = R + 2 To SrcRng.Rows.Count + SrcRng.Row - 1
                      Data(4) = SrcWks.Cells(R, "C")
                      Data(5) = SrcWks.Cells(R, "F")
                      Data(6) = SrcWks.Cells(R, "H")
                      Data(7) = SrcWks.Cells(R, "I")
                      Data(8) = SrcWks.Cells(R, "J")
                      Data(9) = SrcWks.Cells(R, "K")
                      Data(10) = SrcWks.Cells(R, "L")
                        DstRng.Offset(N, 0).Resize(1, UBound(Data)).Value = Data
                      N = N + 1
                    Next R
              R = R + 1
            Loop
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Moving headers and cell information

    Try this

    Sub Test()
    Sheets(1).Activate
    Sheets(2).Cells.Clear
    For N = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(N, 1) <> "" Then
            If Left(Cells(N, 3), 3) = "DOB" Then
                StudentID = Left(Cells(N, 1), InStr(Cells(N, 1), " ") - 1)
                StudentName = Trim(Right(Cells(N, 1), Len(Cells(N, 1)) - Len(StudentID)))
                DOB = Right(Cells(N, 3), Len(Cells(N, 3)) - 5)
            ElseIf IsNumeric(Cells(N, 1)) Then
                TargetRow = Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row + 1
                Sheets(2).Cells(TargetRow, 1) = StudentID
                Sheets(2).Cells(TargetRow, 2) = StudentName
                Sheets(2).Cells(TargetRow, 3) = DOB
                Sheets(2).Cells(TargetRow, 4) = Cells(N, 3)
                Sheets(2).Cells(TargetRow, 5) = Cells(N, 6)
                Sheets(2).Cells(TargetRow, 6) = Cells(N, 8)
                Sheets(2).Cells(TargetRow, 7) = Cells(N, 9)
                Sheets(2).Cells(TargetRow, 8) = Cells(N, 10)
                Sheets(2).Cells(TargetRow, 9) = Cells(N, 11)
                Sheets(2).Cells(TargetRow, 10) = Cells(N, 12)
            End If
        End If
    Next N
    End Sub

    Open up the VBA editor by hitting ALT F11

    Insert a new module by hitting Insert - Module

    Paste the macro into the empty sheet

    Hit ALT F11 to get back to the worksheet.

    Run the macro by going to tools-macro in Excel 2003 or the view ribbon in Excel 2007.

  4. #4
    Forum Contributor
    Join Date
    05-04-2008
    Posts
    103

    Solved

    Man you guys are awesome. It is exactly what I need. I just need to add a column title Period and Avg Fin. I am working on it to see if I can get it, but probably not.

    I figured it out. Thank you guys.
    Last edited by taichi56; 02-25-2011 at 06:07 PM. Reason: SOLVED

+ 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