1

I am trying to get Excel data, which was mapped using a grid/matrix mapping into a de-normalized for so that i can enter the data into a database.

How do you copy data in a grid from one excel sheet to the other as follow illustrated below. enter image description here

I was trying something like this... but as you can see, i am far off!

Sub NormaliseList(mySelection As Range)
Dim cell As Range
Dim i As Long
i = 1
    For Each cell In mySelection
        If cell <> "" Then
                Sheets(2).Range("A" & i).Value = cell(cell.Row, 1).Value
                Sheets(2).Range("B" & i).Value = cell.Value
                Sheets(2).Range("C" & i).Value = cell(1, cell.Column).Value
                i = i + 1

    Next cell
End Sub
Community
  • 1
  • 1
Hightower
  • 968
  • 4
  • 24
  • 58
  • 1
    Have you made any attempts to try and do this? Either with VBA code (which you will probably need) or without. – Scott Holtzman Sep 28 '12 at 13:37
  • 2
    This might help: http://stackoverflow.com/questions/10921791/melt-reshape-in-excel-using-vba. – Doug Glancy Sep 28 '12 at 13:46
  • Thanks guys @ Scott Hottzman, yes, i have something extremely convoluted, will try to create something better.. @Doug Glancy, yes thanks. is "melt" a generally accepted term for this function... this is close to what i am looking for... i will also work at it!.. – Hightower Sep 28 '12 at 15:13
  • I'm going to copy over my answer from the SO question I referred to. `Melt` and `Reshape` are terms used in R, a statistical package I know nothing about :). I would call this "normalizing," in contrast to your title, as the result meets the definition of some level of normalization. – Doug Glancy Sep 28 '12 at 15:20

2 Answers2

1

For Reference. I Updated my code.. Simply add the code, assign macro shortcut to the function Select the range that contains the intersection data (not the row and column data) Run macro (Beware, sheet 2 will have data added in normalised form)

If there are multiple headings that are needed i figured i would consolidate into one column then perform a "text to columns" after processing.

Sub NormaliseList()
' to run - assign macro shortcut to sub - Select Intersection data (not row and column headings and run)
    Dim Rowname, ColumnName, IntValue As String
    Dim x, cntr As Integer
    Dim test As Boolean
    cntr = 0

For x = 1 To Selection.Count
    If Selection(x).Value <> "" Then
        cntr = cntr + 1
        Rowname = ActiveSheet.Cells(Selection.Cells(x).Row, Selection.Column - 1)
        ColumnName = ActiveSheet.Cells(Selection.Row - 1, Selection.Cells(x).Column)
        IntValue = Selection(x).Value
        test = addrecord(Rowname, ColumnName, IntValue, cntr)
     End If
  Next x
   End Sub

 Function addrecord(vA, vB, vC As String, rec As Integer) As Boolean
   'Make sure that you have a worksheet called "Sheet2"
   Sheets("Sheet2").Cells(rec, 1) = vA
   Sheets("Sheet2").Cells(rec, 2) = vB
   Sheets("Sheet2").Cells(rec, 3) = vC
 End Function
Hightower
  • 968
  • 4
  • 24
  • 58
0

I've got two posts, with usable code and downloadable workbook, on doing this in Excel/VBA on my blog:

http://yoursumbuddy.com/data-normalizer

http://yoursumbuddy.com/data-normalizer-the-sql/

Here's the code:

'Arguments
'List: The range to be normalized.
'RepeatingColsCount: The number of columns, starting with the leftmost,
'   whose headings remain the same.
'NormalizedColHeader: The column header for the rolled-up category.
'DataColHeader: The column header for the normalized data.
'NewWorkbook: Put the sheet with the data in a new workbook?
'
'NOTE: The data must be in a contiguous range and the
'rows that will be repeated must be to the left,
'with the rows to be normalized to the right.

Sub NormalizeList(List As Excel.Range, RepeatingColsCount As Long, _
    NormalizedColHeader As String, DataColHeader As String, _
    Optional NewWorkbook As Boolean = False)

Dim FirstNormalizingCol As Long, NormalizingColsCount As Long
Dim ColsToRepeat As Excel.Range, ColsToNormalize As Excel.Range
Dim NormalizedRowsCount As Long
Dim RepeatingList() As String
Dim NormalizedList() As Variant
Dim ListIndex As Long, i As Long, j As Long
Dim wbSource As Excel.Workbook, wbTarget As Excel.Workbook
Dim wsTarget As Excel.Worksheet

With List
    'If the normalized list won't fit, you must quit.
   If .Rows.Count * (.Columns.Count - RepeatingColsCount) > .Parent.Rows.Count Then
        MsgBox "The normalized list will be too many rows.", _
               vbExclamation + vbOKOnly, "Sorry"
        Exit Sub
    End If

    'You have the range to be normalized and the count of leftmost rows to be repeated.
   'This section uses those arguments to set the two ranges to parse
   'and the two corresponding arrays to fill
   FirstNormalizingCol = RepeatingColsCount + 1
    NormalizingColsCount = .Columns.Count - RepeatingColsCount
    Set ColsToRepeat = .Cells(1).Resize(.Rows.Count, RepeatingColsCount)
    Set ColsToNormalize = .Cells(1, FirstNormalizingCol).Resize(.Rows.Count, NormalizingColsCount)
    NormalizedRowsCount = ColsToNormalize.Columns.Count * .Rows.Count
    ReDim RepeatingList(1 To NormalizedRowsCount, 1 To RepeatingColsCount)
    ReDim NormalizedList(1 To NormalizedRowsCount, 1 To 2)
End With

'Fill in every i elements of the repeating array with the repeating row labels.
For i = 1 To NormalizedRowsCount Step NormalizingColsCount
    ListIndex = ListIndex + 1
    For j = 1 To RepeatingColsCount
        RepeatingList(i, j) = List.Cells(ListIndex, j).Value2
    Next j
Next i

'We stepped over most rows above, so fill in other repeating array elements.
For i = 1 To NormalizedRowsCount
    For j = 1 To RepeatingColsCount
        If RepeatingList(i, j) = "" Then
            RepeatingList(i, j) = RepeatingList(i - 1, j)
        End If
    Next j
Next i

'Fill in each element of the first dimension of the normalizing array
'with the former column header (which is now another row label) and the data.
With ColsToNormalize
    For i = 1 To .Rows.Count
        For j = 1 To .Columns.Count
            NormalizedList(((i - 1) * NormalizingColsCount) + j, 1) = .Cells(1, j)
            NormalizedList(((i - 1) * NormalizingColsCount) + j, 2) = .Cells(i, j)
        Next j
    Next i
End With

'Put the normal data in the same workbook, or a new one.
If NewWorkbook Then
    Set wbTarget = Workbooks.Add
    Set wsTarget = wbTarget.Worksheets(1)
Else
    Set wbSource = List.Parent.Parent
    With wbSource.Worksheets
        Set wsTarget = .Add(after:=.Item(.Count))
    End With
End If

With wsTarget
    'Put the data from the two arrays in the new worksheet.
   .Range("A1").Resize(NormalizedRowsCount, RepeatingColsCount) = RepeatingList
    .Cells(1, FirstNormalizingCol).Resize(NormalizedRowsCount, 2) = NormalizedList

    'At this point there will be repeated header rows, so delete all but one.
   .Range("1:" & NormalizingColsCount - 1).EntireRow.Delete

    'Add the headers for the new label column and the data column.
   .Cells(1, FirstNormalizingCol).Value = NormalizedColHeader
    .Cells(1, FirstNormalizingCol + 1).Value = DataColHeader
End With
End Sub

You’d call it like this:

Sub TestIt()
NormalizeList ActiveSheet.UsedRange, 1, "Name", "Count", False
End Sub
Doug Glancy
  • 27,214
  • 6
  • 67
  • 115