Consulting

Results 1 to 3 of 3

Thread: Changing Data Layout Excel VBA

  1. #1
    VBAX Newbie
    Joined
    May 2020
    Posts
    3
    Location

    Changing Data Layout Excel VBA

    Hi everyone!!
    I just signed up today to ask for help.
    I really need help on VBA.

    My data looks like below

    123.jpg



    Is there good way to change the data layout using VBA? I am literally new to VBA.
    I would be super appreciated with your help!!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Welcome to the forum - please take a minute and read the FAQs at the link in my signature


    Put this into a standard module, or tie it to button code





    Option Explicit
    
    
    Sub TwoD_to_OneD()
        Dim rSrc As Range, rDest As Range
        Dim x As Long, y As Long, z As Long
        
        Application.ScreenUpdating = False
        
        Set rSrc = ActiveSheet.Range("B3").CurrentRegion '   <<<<<<<<<<<<<<<< change B3
        Set rDest = ActiveSheet.Range("K3")             '   <<<<<<<<<<<<<<<<< change K3
        
        rDest.CurrentRegion.ClearContents
        
        z = 0
        rDest.Offset(z, 0).Value = "X"
        rDest.Offset(z, 1).Value = "Y"
        rDest.Offset(z, 2).Value = "Z"
        
        For x = 2 To rSrc.Rows.Count
            For y = 2 To rSrc.Columns.Count
                z = z + 1
                rDest.Offset(z, 0).Value = rSrc.Cells(x, 1).Value
                rDest.Offset(z, 1).Value = rSrc.Cells(1, y).Value
                rDest.Offset(z, 2).Value = rSrc.Cells(x, y).Value
            Next y
        Next x
    
    
        Application.ScreenUpdating = True
    
    
    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

  3. #3
    VBAX Newbie
    Joined
    May 2020
    Posts
    3
    Location
    Thank you!!

Posting Permissions

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