2014-09-02 91 views
-4

我在下面的格式非常大​​的数据集:Excel的VBA - 串联行重复键值

以前

enter image description here

我是全新的,以VBA,但我正在努力确定这些数据,以便将其输入到SPSS中。对于我们而言,它需要像这样:

的想法是相匹配的ID号码的所有行合并成ň长度的单排。如图所示,行数不一致。此外,我们需要能够处理空白单元格 - 在某些情况下,可能不会输入值或长度,但下一行需要从每个标题的正确位置开始。

我在Bash做了很多次,但是我的妻子需要能够自己重现这一点,因为有许多这种类型的数据的电子表格。

我目前正在搞清楚语法和写出来,我最初的做法是筛选唯一的ID,复制到第二张纸,然后做一个For Each循环来追加数据。

我会粘贴我的代码,但它在当前阶段会比其他任何有用的东西更多地分散注意力。任何关于此方法的见解都将受到高度赞赏,特别是如果有一种更容易或更少征税的方式来做到这一点。

感谢您的阅读! 迈克

+0

这是如何在SPSS中成为可用数据的?列标题不唯一。这将有助于展示你的代码(这是你在这里的期望 - 因为通过修改现有的代码比从头开始编写代码更容易提供帮助)。干杯。 – 2014-09-02 14:17:31

+0

我可能会考虑对行进行迭代,根据ID构建一个分隔字符串(以逗号或制表符分隔)来表示格式化输出中的每个“行”,然后将其写入TXT文件,该文件可能很容易由SPSS读取。 – 2014-09-02 14:23:26

+0

大卫,你是对的。实际文件中的标题将是唯一的,具体为:Type_1,Value_1,Type_2,Value_2等。 我正在处理代码。感谢您的建议 - 这非常有意义。一旦我有一些真正清晰的东西,我就会发布它。 – 2014-09-02 14:26:38

回答

1

这是我在我上面的评论中描述的方法:

我可能会看遍历所有行,构建分隔字符串(分隔逗号或制表符)来表示每个“行”基于ID的格式化输出,然后将其写入可以通过SPSS轻松读取的TXT文件

以下是代码。它比30行我估计:)

Sub FormatDataFileForSPSS() 
Dim rng As Range   'the range representing the entire set of data to be formatted 
Dim r As Range    'row iterator for the data table 
Dim key As Variant   'id number 
Dim rowData As String  'concatenated row data 
Dim outputPath As String 'the place to put the output file 
Dim outputFile As String 'the file name 

'--- REQUIRES REFERENCE TO MICROSOFT SCRIPTING RUNTIME --- 
Dim dict As Scripting.Dictionary 'a dictionary that we will use to concat each row by ID 
Dim fso As Scripting.FileSystemObject 'used to write the output file 

'Begin procedure here... 

'Allow the user to select a range of data to format 
' do NOT select the "header" row! 
Set rng = Application.InputBox("Select the data to be formatted", "Select Data", Type:=8) 

'Create the dictionary: 
Set dict = CreateObject("Scripting.Dictionary") 

'get the destination for the output file: 
outputPath = CreateObject("Wscript.Shell").SpecialFolders("Desktop") 'Or modify to use a different filepath 
outputFile = outputPath & "\my output.txt" 'Modify as needed 

'Iterate the data table: 
For Each r In rng.Rows 
    'get the key value 
    key = r.Cells(1, 1).Value 
    'Concatenate the row data to a string 
    rowData = r.Cells(1, 2) & vbTab & r.Cells(1, 3) & vbTab & r.Cells(1, 4) & vbTab & r.Cells(1, 5) 
    'Check if this KEY value already exists 
    If Not dict.Exists(key) Then 
     'if not, then add it to the dictionary 
     dict.Add key, rowData 
    Else: 
     'Append to the existing key's value: 
     dict(key) = dict(key) & vbTab & rowData 
    End If 
Next 

'Create our FileSystemObject to write the text file: 
Set fso = CreateObject("Scripting.FileSystemObject") 
With fso.CreateTextFile(Filename:=outputFile, overwrite:=True, unicode:=False) 
    For Each key In dict.Keys 
     .WriteLine dict(key) 
    Next 
    .Close 
End With 

End Sub 

输出制表符分隔稍多,无标题行(因为在你的榜样的头是不是唯一的开始)。我相当确定您可以在SPSS中指定导入无标题行的数据,并且它会分配默认变量名称,您可以根据需要稍后进行修改。

enter image description here

这里是SPSS(根据提示打开分隔文本文件)

enter image description here

或者你也可以打开TXT delmited Excel文件,并遵循一些提示,看数据,将其指定为制表符分隔,然后您可以在Excel文件中添加标题信息:

enter image description here

+0

大卫! 感谢您的建议修复!这是比我所看到的更优雅的解决方案。 我现在要测试它,我会让你知道我的结果! -Mike – 2014-09-02 15:13:11

+0

解决大多数问题的方法有多种,复制数据,创建新工作表,过滤等是一种可能的解决方案,它非常强悍:)该方法的好处是新手/新手VBA民众几乎会立即明白,因为它会使用从宏录像机获得的“基本”方法和行动,但它会更慢,更笨拙等等。干杯! – 2014-09-02 15:16:34