[英]VBA/Excel - Migration of 'matrix' based dataset to a database
I am trying to migrate a dataset from an old spreadsheet based system to a database. 我正在尝试将数据集从旧的基于电子表格的系统迁移到数据库。 And I have one outstanding single issue to solve. 我还有一个突出的问题要解决。
I have a sheet within a spreadsheet that is acting like a many-to-many table: 我在电子表格中有一个工作表,该工作表的作用类似于多对多表:
On the crossing of rows and columns I have either an empty cell or an 'X' (X worked in old system as a relation between two different data sets) 在行和列的交叉处,我有一个空单元格或一个“ X”(X在旧系统中用作两个不同数据集之间的关系)
Rows_name|Column_name1 |Column_name2 |Column_nameX 行名称|列名称1 |列名称2 |列名称X
Row_name1| Row_name1 | | | X | X | X X
Row_name2| Row_name2 | X | X | | |
Row_name3| 行名3 | X | X | X | X | X X
For each found 'X' I require to copy Row_name and Column_name to separate sheet ready for export. 对于每个找到的“ X”,我需要将Row_name和Column_name复制到单独的工作表以准备导出。
IE For Row_name3 it would be three new rows in a new sheet as Row_name3 has three 'X's IE对于Row_name3,它将是新表中的三个新行,因为Row_name3具有三个“ X”
Rows_name|Column_name
Row_name3|Column_name1
Row_name3|Column_name2
Row_name3|Column_name3
In effect I am solving a many to many relation by having a third table. 实际上,我通过拥有第三个表来解决多对多关系。
Therefore I am looking for a help with the algorithm to find all related column/row names for each 'X'. 因此,我正在寻找一种算法帮助,以查找每个“ X”的所有相关列/行名称。
For any suggestions how to tackle this I would be very grateful. 对于如何解决此问题的任何建议,我将不胜感激。
Is this what you are trying? 这是您要尝试的吗?
Option Explicit
Sub Sample()
Dim wsInput As Worksheet, wsOutput As Worksheet
Dim LRI As Long, LRO As Long, i As Long, j As Long
'~~> Input Sheet
Set wsInput = Sheets("Sheet1")
LRI = wsInput.Range("A" & wsInput.Rows.Count).End(xlUp).Row
'~~> Output Sheet
Set wsOutput = Sheets("Sheet2")
LRO = 2
For i = 2 To LRI
With wsInput
For j = 1 To 3
If UCase(Trim(.Range("A" & i).Offset(, j).Value)) = "X" Then
.Range("A" & i).Copy wsOutput.Range("A" & LRO)
.Range("A1").Offset(, j).Copy wsOutput.Range("B" & LRO)
LRO = LRO + 1
End If
Next
End With
Next i
End Sub
SNAPSHOT 快照
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.