1

データベースにデータを入力できるように、グリッド/マトリックス マッピングを使用して非正規化されたデータにマップされた Excel データを取得しようとしています。

以下に示すように、グリッド内のデータをある Excel シートから別の Excel シートにコピーするにはどうすればよいですか。 ここに画像の説明を入力

私はこのようなことを試みていました...しかし、ご覧のとおり、私は遠く離れています!

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
4

2 に答える 2

1

参考のため。私は自分のコードを更新しました..単にコードを追加し、関数にマクロショートカットを割り当てます(行と列のデータではなく)交差データを含む範囲を選択しますマクロを実行します(シート2には正規化された形式でデータが追加されることに注意してください)

必要な見出しが複数ある場合は、1 つの列に統合し、処理後に「列へのテキスト」を実行すると考えました。

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
于 2012-10-15T21:04:32.100 に答える
0

私のブログで Excel/VBA でこれを行うことについて、使用可能なコードとダウンロード可能なワークブックを含む 2 つの投稿があります。

http://yoursumbuddy.com/data-normalizer

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

コードは次のとおりです。

'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

次のように呼び出します。

Sub TestIt()
NormalizeList ActiveSheet.UsedRange, 1, "Name", "Count", False
End Sub
于 2012-09-28T15:18:01.370 に答える