Complicated Transposition Challenge

dancole42

Registered User.
Local time
Today, 18:52
Joined
Dec 28, 2012
Messages
21
I need help converting a sheet in Excel to a table in Access, but the data in Excel is arranged poorly. Can anyone offer some suggestions?

The Excel table has headers corresponding to each country, and then each record is some data on that country (and yes, there are blanks).

Code:
UK US AU
1   2   2
2   1   2
2   3   2
1       1
1
1

I want my table to look like:

Code:
UK 1
UK 2
UK 2
UK 1
UK 1
UK 1
US 2
US 1
US 3
AU 2
AU 2
AU 2
AU 1

Any suggestions?
 
Assuming a table of imported spreadsheet data (tblImport) that looks like this;

attachment.php



And a new table (tblNewData) with the fields CountryCode and DataValue;

attachment.php


Then looping through a recordset of the import table with code like the following;

Code:
Private Sub Command4_Click()
  
    Dim strUpdate As String
    Dim i As Long
    
    With CurrentDb.OpenRecordset("Select * From tblImport", dbOpenSnapshot)
        .MoveFirst
        Do While Not .EOF
            For i = 0 To .Fields.Count - 1
                If Nz(.Fields(i).Value, "") <> "" Then
                    strUpdate = "Insert Into tblNewData (CountryCode, DataValue) " _
                           & "Values (""" & .Fields(i).Name & """, """ & .Fields(i).Value & """);"
                    CurrentDb.Execute strUpdate, dbFailOnError
                End If
            Next
            .MoveNext
        Loop
    End With
  
End Sub

Gives these results in the new table;

attachment.php
 

Attachments

  • tblResults.jpg
    tblResults.jpg
    21.9 KB · Views: 152
  • tblImport.jpg
    tblImport.jpg
    13.6 KB · Views: 151
  • tblNewdata.jpg
    tblNewdata.jpg
    8.5 KB · Views: 155
You are a beautiful, beautiful man... thank you!!!
 

Users who are viewing this thread

Back
Top Bottom