简体   繁体   中英

VBA Excel unpivot table / extract specific data

I've been trying to extract specific datapoints from a pivoted data table in MS Excel using VBA.

The data looks like this:

ID     dimension     value
0001   Name          Max
0001   Adress        Octavia St
0002   Postal Code   94100
0001   City          San Antonio
0005   Name          Sylvia
0018   Postal Code   33741

and I would like to extract specific datapoints into a larger table with information from other sources based on the required "dimension" as column header and the ID as row header:

ID     otherInfo1    Name    otherInfo2    City
0001   chef          Max     married       San Antonio
0005   bank teller   Sylvia  single        ...

Unfortunatly I was unable to find any answers to this question online and am relatively new to VBA.

Does anybody have an idea how to paste the data directly into another spreadsheet without using intermediary worksheets?

I've been trying to use a for-loop, but unsuccessfully:

For i = 12 To nRowsB
    For j = 2 To nRowsB
        If wks_source.Cells(j, 1).Value = wkstarget.Cells(i, 1).Value Then
        wkstarget.Cells(i, 1).Value = wks_source.Cells(j, 1).Value
        Debug.Print "Success1"
        Else
        j = j + 1
        End If
    Next j
Next i

nRowsB -- Number of rows in source file

Thanks a lot in advance for your help!!

This code should help you solve your problem.

Sub Unpivot()
Dim wks_source As Worksheet
Dim wks_target As Worksheet
Dim nRowsTarget As Long
Dim nColumnsTarget As Long
Dim nRowsSource As Long
Dim i As Long
Dim j As Long
Dim k As Long


Set wks_source = Worksheets("Source")
Set wks_target = Worksheets("Target")

nRowsTarget = 7
nColumnsTarget = 5
nRowsSource = 7



For i = 2 To nRowsTarget
    For j = 2 To nColumnsTarget
        For k = 2 To nRowsSource
            If wks_target.Cells(i, 1).Value = wks_source.Cells(k, 1).Value Then         ' If ID matches go on
                If wks_target.Cells(1, j).Value = wks_source.Cells(k, 2).Value Then     ' If some property matches go on
                    wks_target.Cells(i, j).Value = wks_source.Cells(k, 3).Value         ' Assign
                    Exit For
                End If
            End If
        Next k
    Next j
Next i


End Sub

It extracts data from this sheet "Source"

在此处输入图片说明

Into this sheet "Target"

在此处输入图片说明

Worked out this code last night, find attached for your reference:

 Sub get_unpivoted_data(nRowsID, source_wks, target_wks, ID_column_source, ID_column_target, search_term_2nd_level, target_write_column, source_column, source_search_term_2nd_level_column)

    For i = 12 To nRowsID
        For j = 2 To nRowsID
            If source_wks.Cells(j, ID_column_source).Value = target_wks.Cells(i, ID_column_target).Value Then
                Debug.Print "Success1"
                If source_wks.Cells(j, source_search_term_2nd_level_column).Value = search_term_2nd_level Then
                    target_wks.Cells(i, target_write_column).Value = source_wks.Cells(j, source_column).Value
                    Else
                    Debug.Print "x"
                    End If
            Else
            j = j + 1
            End If
        Next j
    Next i

    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM