+ Reply to Thread
Results 1 to 6 of 6

Reformatting an excel 2007 file

Hybrid View

  1. #1
    Registered User
    Join Date
    11-16-2007
    Posts
    12

    Reformatting an excel 2007 file

    Hello
    I just wondered if anyone can brighten my Friday for me.
    I need to reformat the attached file and wondered if a macro could do this for me.

    1.The value listed after "CHAINAGE" must form the beginning of the number in the 1st column.
    2.Please note that there are a varying number of lines in each block.
    3.The sheet is much larger than that I have posted.
    4.The spacing between the blocks is the same throughout.

    Your help is much appreciated.
    Thanks
    Rob
    Attached Files Attached Files

  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: Reformating an excel 2007 file

    Try this:
    Option Explicit
    
    Sub ReformatData()
    'JBeaucaire   3/12/2010
    Dim LR As Long, RNG As Range
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
        With Range("M5:M" & LR)
            .FormulaR1C1 = _
                "=IF(ISNUMBER(RC1), INDEX(C2, MATCH(""CHAINAGE"", R1C1:RC1, 1)) & ""_"" & RC1, """")"
            .Value = .Value
        End With
    
        With Range("A1:A" & LR)
            .SpecialCells(xlCellTypeConstants, 2).EntireRow.Delete xlShiftUp
            .SpecialCells(xlCellTypeBlanks).EntireRow.Delete xlShiftUp
            .Value = .Offset(0, 12).Value
        End With
    
    Columns("M:M").ClearContents
    End Sub

    ==========
    How/Where to install the macro:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save as a macro-enabled workbook

    The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.
    _________________
    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
    Registered User
    Join Date
    11-16-2007
    Posts
    12

    Re: Reformating an excel 2007 file

    Many many thanks for your reply. Your macro works for the data I have sent you, but for some reason isn't quite right for the whole file which I now attach.
    The problem starts at CHAINAGE 10 when the macro lists 5 numbers as follows
    10_1 13096.959 52387.897 140.113 -3.65 CE00 EDGE OF HARD STRIP
    10_2 13100.115 52389.731 140.206 0 MC01 MASTER STRING CENTRE LINE
    10_3 13103.271 52391.565 140.115 3.65 CE0I EDGE OF HARD STRIP
    10_1 13091.922 52396.514 140.327 -3.65 CE00 EDGE OF HARD STRIP
    10_2 13095.069 52398.364 140.424 0 MC01 MASTER STRING CENTRE LINE

    whereas there are only 3 in the original file.
    Thank you again.
    Rob

  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: Reformating an excel 2007 file

    Well, shucks, I was all happy to be able to do it without "looping"...oh well. Here you go:
    Option Explicit
    
    Sub ReformatData()
    'JBeaucaire   3/12/2010
    Dim LR As Long, Rw As Long, Shp As Shape
    Dim MyKey As String, MyChn As String
    Application.ScreenUpdating = False
    
    MyKey = "CHAINAGE"
        
        With ActiveSheet
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            Rw = Cells.Find(MyKey, After:=.[A3], LookIn:=xlValues, LookAt:=xlWhole).Row
            
            Do
                If UCase(Trim(Cells(Rw, "A"))) = "CHAINAGE" Then MyChn = "_" & Cells(Rw, "B")
                If IsNumeric(Cells(Rw, "A")) And Cells(Rw, "A") > 0 Then Cells(Rw, "A") = Cells(Rw, "A") & MyChn
                Rw = Rw + 1
            Loop Until Rw > LR
        End With
    
    
    On Error Resume Next
        Range("A1:A" & LR).SpecialCells(xlCellTypeBlanks).EntireRow.Delete xlShiftUp
        Range("C1:C" & LR).SpecialCells(xlCellTypeBlanks).EntireRow.Delete xlShiftUp
        Range("C1:C" & LR).SpecialCells(xlCellTypeConstants, 2).EntireRow.Delete xlShiftUp
        Rows(1).Delete xlShiftUp
    
        For Each Shp In ActiveSheet.Shapes
            Shp.Delete
        Next Shp
    
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    11-16-2007
    Posts
    12

    Re: Reformatting an excel 2007 file

    Thank you for your work - I won't pretend that I can even begin to understand the programming.
    but have made one small change which I hope you agree with.
    The reason is that I need the data to be listed as 100_1 not 1_100 as per your 2nd code.
    Have changed it to
    If UCase(Trim(Cells(Rw, "A"))) = "CHAINAGE" Then MyChn = Cells(Rw, "B") & "_"
    If IsNumeric(Cells(Rw, "A")) And Cells(Rw, "A") > 0 Then Cells(Rw, "A") = MyChn & Cells(Rw, "A")

    This seems to do the job.
    Many thanks for your work.
    Will hit the feedback icon now.

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

    Re: Reformatting an excel 2007 file

    1) Please edit that post above...either remove the code or encase it in code tags required by forum rules (example in my signature)

    2) I noticed I create a variable and forgot use it, so a final correction might be this:
    If UCase(Trim(Cells(Rw, "A"))) = MyKey Then MyChn = Cells(Rw, "B") & "_"
    If IsNumeric(Cells(Rw, "A")) And Cells(Rw, "A") > 0 Then Cells(Rw, "A") = MyChn & Cells(Rw, "A")

+ 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