Consulting

Results 1 to 3 of 3

Thread: reorganizing cell data on a diferent sheet

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    2
    Location

    reorganizing cell data on a diferent sheet

    My idea is to reorganize the data on sheet2, into sheet1, instead of doing it manually:

    Lets say in Sheet2, I have data displayed in horizontal form, the rows have diferent lengts:
    DATA-B DATA-A1 DATA-A2 DATA-A3 DATA-A4 ... DATA-A31 DATA-A32
    X Y A
    S A D S S
    D D D
    S S D
    D S D F D ... S S
    A D
    S E


    And I need to relate the rows from the second columnt until their last to the name in the first row:
    DATA-A DATA-B
    Y X
    A X
    A S
    D S
    S S
    S S
    D D
    D D
    .. ..
    E S


    My problem is that I have tried with loop, but not find the way to put this to work right:

    Code:
    Sub test()


    Dim lastRow As Long
    Dim pointerx As Integer
    Dim pointery As Integer
    Dim n As Integer
    Dim cella As String
    Dim cellb As String


    'MsgBox lastRow
    lastRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row




    For pointery = 2 To lastRow
    For pointerx = 2 To 32


    cella = Trim(CStr(Sheet2.Cells(pointery, pointerx).Value))
    ThisWorkbook.Sheets("Sheet1").Cells(pointerx, 1).Value = cella
    If cella = "" Then
    Exit For
    End If
    cellb = Trim(CStr(Sheet2.Cells(pointery, 1).Value))
    ThisWorkbook.Sheets("Sheet1").Cells(pointery, 2).Value = cellb


    Next pointerx


    Next pointery






    MsgBox ("finished")


    End Sub



    I am wondering if I will need a Do while, or another type of loop to do this task. I need help to troubleshoot this. I have attached a file.
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    The workbook and the requirements were a little confusing, but this takes the 2D array on sheet2 and makes it into a 2 col list with col A value o sheet2 in col B on sheet1, and each of the other values in col A


    Option Explicit
    Sub test3()
        Dim r As Long, c As Long, o As Long
        Dim v As Variant
        Dim wsFrom As Worksheet, wsTo As Worksheet
        
        Application.ScreenUpdating = True
        
        Set wsFrom = Worksheets("Sheet2")
        Set wsTo = Worksheets("Sheet1")
        
        v = wsFrom.Cells(1, 1).CurrentRegion.Value
        o = 1
        
        For r = LBound(v, 1) + 1 To UBound(v, 1)
            For c = LBound(v, 2) + 1 To UBound(v, 2)
                If Len(v(r, c)) > 0 Then
                    wsTo.Cells(o, 1).Value = v(r, c)
                    wsTo.Cells(o, 2).Value = v(r, 1)
                    o = o + 1
                End If
            Next c
        Next r
        Application.ScreenUpdating = True
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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
    Jan 2016
    Posts
    2
    Location
    Thanks Paul Hossler!!!!!
    This is what I needed!!! just only needed to start on A2, but yes this is what I need

Posting Permissions

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