Consulting

Results 1 to 8 of 8

Thread: Transfer data from adjacent cells to non-adjacent cells

  1. #1

    Transfer data from adjacent cells to non-adjacent cells

    Hello everyone
    I have devised that code that enables me to transfer data from adjacent cells to non-adjacent cells
    Sub Test()
        Dim a As Variant
        Dim b As Variant
        Dim i As Long
        Dim x As Long
        
        a = Range("H12:H16").Value
        ReDim b(1 To UBound(a, 1) * 2, 1 To UBound(a, 2))
        
        For i = LBound(b, 1) To UBound(b, 1) Step 2
            x = x + 1
            b(i, 1) = a(x, 1)
        Next i
        
        Range("N12").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    End Sub
    Is there a simpler way to achieve that?

  2. #2
    I have found a simpler code
    Sub Demo()
        Dim a As Variant
        
        a = Split(Join(Application.Transpose(Range("H12:H16")), ",,"), ",")
        Range("N12").Resize(UBound(a) + 1).Value = Application.Transpose(a)
    End Sub
    But I welcome any other solutions

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or

    Sub M_snb()
      [N12:N20] = [choose(row(1:9),H12,"",H13,"",H14,"",H15,"",H16)]
    End Sub

  4. #4
    That's great but the original source is about 100 values and it will be exhausted to refer to each cell individually ..

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Set a = Range("H12").CurrentRegion
    Set b = Range("N16")
    For i = 0 To a.Cells.Count - 1
    b.Offset(2 * i) = a(i + 1)
    Next i
    End Sub
    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'

  6. #6
    Thank you very much Mr. mdmackillop (Your name is difficult for me .. but I promise I will keep it by heart)

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MD are my initials, MacKillop the surname
    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'

  8. #8
    It is honor to know about you Mr. MD Mackillop
    Thanks a lot for your great efforts

Posting Permissions

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