2014-09-02 97 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