Results 1 to 7 of 7

Thread: Copy specific data from a row based on value in column A

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    This is not very general purpose, but it works with your sample data


    Option Explicit
    
    
    Sub FormatData()
        Dim wsIn As Worksheet, wsOut As Worksheet
        Dim r As Long, c As Long
        Dim rData As Range
        
        Set wsIn = Worksheets("Before")
        Set wsOut = Worksheets("After")
        
        'copy input cells to output
        wsIn.Cells(1, 1).CurrentRegion.Copy wsOut.Cells(1, 1)
        
        'reference output cells
        Set rData = wsOut.Cells(1, 1).CurrentRegion
        
        'down the rows, starting at 2 until the end
        For r = 2 To rData.Rows.Count
            'accross the columns, starting at 4 untile the end
            For c = 4 To rData.Columns.Count
                'if the c-th cell in the r-th is blank, get the non-blank one to the right and put it there
                '   clear it afterwards
                If Len(rData.Cells(r, c).Value) = 0 Then
                    rData.Cells(r, c).Value = rData.Cells(r, c).End(xlToRight).Value
                    rData.Cells(r, c).End(xlToRight).ClearContents
                End If
            Next c
        Next r
    
    
        'delete columns that only have the headers left
        Set rData = wsOut.Cells(1, 1).CurrentRegion
        Do While Application.WorksheetFunction.CountA(rData.Columns(rData.Columns.Count)) = 1
            rData.Columns(rData.Columns.Count).EntireColumn.Delete
            Set rData = wsOut.Cells(1, 1).CurrentRegion
        Loop
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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