[英]Converting data to panel format using forloops vba-excel
我的數據集如下所示:
AUS, UK, USA, GERMANY, FRANCE, MEXICO, DATE
R1, R1, R1, R1 , R1 , R1 , 1
R2, R2, R2, R2 , R2 , R2 , 2
...
等等。 我想將其轉換為看起來像
COUNTRY, RETURNS, DATE,
AUS, R1, 1
AUS, R2, 2
..., ..., ...,
UK, R1, 1,
UK, R2, 2,
... ... ...,
MEXICO, R1, 1,
MEXICO, R2, 2,
... ... ...
我覺得使用簡單的嵌套forloop應該可以實現。
我試過了:
sub panel()
'dim variables
Dim i As Integer
Dim j As Integer
Dim reps As Integer
Dim country As String
Dim strfind As String
Dim obs As Integer
'count the number of countries
reps = Range("D1:AL1").Columns.Count
'count the number of observations per country
obs = Range("C4:C5493").Rows.Count
'copy and paste country into panel format
For i = 1 To reps
'set country name
country =Range("D1").Cells(1, i)
For j = 1 To obs
'copy and paste country values
Range("AS2").Cells(j, 1) = country
Next j
Next i
但是在完成j循環並設置了新的國家/地區名稱之后,新值將替換第一批單元格中的舊值。
考慮使用UNION查詢為長格式選擇每一列的SQL解決方案。 如果使用Excel for PC,則Excel可以通過ADO連接到Jet / ACE SQL引擎(Windows .dll文件),並在當前工作簿的工作表上運行SQL查詢。
通過這種方法,就避免任何for
循環,嵌套if/then
邏輯,及其他數據操作需求所需的輸出。 下面的示例假定數據駐留在名為DATA的選項卡中,並且位於名為RESULTS的空選項卡中。
Sub RunSQL()
Dim conn As Object, rst As Object
Dim strConnection As String, strSQL As String
Dim i As Integer
Set conn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
' CONNECTION STRINGS (TWO VERSIONS)
' strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" _
' & "DBQ=C:\Path\To\Workbook.xlsm;"
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source='C:\Path\To\Workbook.xlsm';" _
& "Extended Properties=""Excel 8.0;HDR=YES;"";"
strSQL = " SELECT 'AUS' AS COUNTRY, AUS AS RETURNS, [DATE] FROM [DATA$]" _
& " UNION ALL SELECT 'UK', UK AS Country, [DATE] FROM [DATA$]" _
& " UNION ALL SELECT 'USA', USA AS Country, [DATE] FROM [DATA$]" _
& " UNION ALL SELECT 'GERMANY', GERMANY AS Country, [DATE] FROM [DATA$]" _
& " UNION ALL SELECT 'FRANCE', FRANCE AS Country, [DATE] FROM [DATA$]" _
& " UNION ALL SELECT 'MEXICO', MEXICO AS Country, [DATE] FROM [DATA$];"
' OPEN CONNECTION & RECORDSET
conn.Open strConnection
rst.Open strSQL, conn
' COLUMN HEADERS
For i = 1 To rst.Fields.Count
Worksheets("RESULTS").Cells(1, i) = rst.Fields(i - 1).Name
Next i
' DATA ROWS
Worksheets("RESULTS").Range("A2").CopyFromRecordset rst
rst.Close: conn.Close
Set rst = Nothing: Set conn = Nothing
End Sub
輸出量
COUNTRY RETURNS DATE
AUS R1 1
AUS R2 2
UK R1 1
UK R2 2
USA R1 1
USA R2 2
GERMANY R1 1
GERMANY R2 2
FRANCE R1 1
FRANCE R2 2
MEXICO R1 1
MEXICO R2 2
好的,我修復了它。 可能不是最好的代碼,但是它可以工作:)。
Sub replicate_dates()
'declare variables
Dim i As Double
Dim j As Double
Dim reps As Integer
Dim country As String
Dim strfind As String
Dim obs As Integer
'set strfind value
strfind = "-DS Market"
'count the number of countries
reps = Range("D1:AL1").Columns.Count
'count the number of observations per country
obs = Range("C4:C5493").Rows.Count
i = 0
'copy and paste country into panel format
For k = 1 To reps
'set country name and clean string
country = Replace(Range("D1").Cells(1, k), strfind, "")
For j = i + 1 To obs + i
'copy and paste country values
Range("AS5").Cells(i, 1) = country
i = 1 + i
Next j
Next k
結束子
編輯:Nvm,這無法在特定情況下工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.