从Excel中进行数据转换

我有一些水平和垂直维度的Excel文件中的数据。 它看起来像这样:

在这里输入图像说明

这些数据必须加载到一些BI系统中。 为此,我必须将数据转换为“表格样式”。 换句话说,它应该像这样呈现在表格中:

在这里输入图像说明

我需要一些有效的algorithm来做这个转换。 我知道的唯一一个就是从第一个单元格(100000)获取值,从垂直和水平坐标(俄罗斯,人口,1900)获得数值并插入第一行。 然后拿另一个单元格等等。

这将是less量的数据工作,但大量的工作非常缓慢。 你知道这种数据更复杂的algorithm吗?

使用VBA有几种方法。 在这个解决scheme中,我首先创build一个名为Country的用户定义对象,具有四个属性:Name,Index,YR和Quantity。 没有必要这样做; 但我最近一直在与这些工作,我认为它增加了一些清晰的代码。

然后我将Source数据读入一个VBA数组(可以一步完成),遍历数组创build一个Country对象集合。

然后我通过Country集合,将属性输出到结果数组中,我想要它们。

最后,结果数组被输出到工作表 – 再次,只是一个步骤。

可以直接从Source数据数组到结果数组,但是我认为使用该对象更容易看到发生了什么。

人们也可以不打扰VBAarrays,但直接从一个工作表处理单元格到另一个。 根据我的经验,这种方法至less比使用VBAarrays方法慢一个数量级。

根据数据库的大小,可能需要改进。 一定要阅读代码中的评论。

要定义Country对象,请插入一个Class Module并将其重命名为Country 。 将下面的代码放在该模块中:

==========================================

 Option Explicit Private pName As String Private pIndex As String Private pYr As Long Private pQuantity As Double Public Property Get Name() As String Name = pName End Property Public Property Let Name(Value As String) pName = Value End Property Public Property Get Index() As String Index = pIndex End Property Public Property Let Index(Value As String) pIndex = Value End Property Public Property Get Yr() As Long Yr = pYr End Property Public Property Let Yr(Value As Long) pYr = Value End Property Public Property Get Quantity() As Double Quantity = pQuantity End Property Public Property Let Quantity(Value As Double) pQuantity = Value End Property 

=============================================

然后,插入一个常规模块,并将此代码放置在那里:

=======================================

 Option Explicit Sub TransformData() Dim wsSrc As Worksheet 'Data Source Dim wsRes As Worksheet, rRes As Range 'Results go here Dim vSrc As Variant 'Actual data goes into this array Dim vRes() As Variant 'Results will go here before being written to worksheet Dim cCTY As Country 'User defined object Dim colCountries As Collection Dim I As Long, J As Long 'counters Set wsSrc = Worksheets("Sheet2") '<--change these to whatever Set wsRes = Worksheets("Sheet3") Set rRes = wsRes.Range("A1") '<--1st cell of results array 'read data into array With wsSrc vSrc = .Range("A1").CurrentRegion '<--many ways to get this depending on your real data setup End With 'iterate through Source and create collection of results Set colCountries = New Collection For I = 2 To UBound(vSrc, 1) '<--Rows For J = 3 To UBound(vSrc, 2) '<--Columns Set cCTY = New Country With cCTY .Name = vSrc(I, 1) .Index = vSrc(I, 2) .Yr = vSrc(1, J) .Quantity = vSrc(I, J) End With colCountries.Add cCTY Next J Next I 'Results ReDim vRes(0 To colCountries.Count, 1 To 4) 'Column Labels vRes(0, 1) = "Country" vRes(0, 2) = "Index" vRes(0, 3) = "Year" vRes(0, 4) = "Value" For I = 1 To colCountries.Count With colCountries(I) vRes(I, 1) = .Name vRes(I, 2) = .Index vRes(I, 3) = .Yr vRes(I, 4) = .Quantity End With Next I Set rRes = rRes.Resize(UBound(vRes, 1) + 1, UBound(vRes, 2)) rRes.EntireColumn.Clear rRes = vRes With rRes.Rows(1) .Font.Bold = True .HorizontalAlignment = xlCenter End With rRes.EntireColumn.AutoFit End Sub 

================================================== ==

确保工作表和范围正确定义,以符合您的真实设置,并运行macros。