-3
我有一個數據集移調在Excel
2011 A 560
2011 B 349
2012 A 456
2012 B 789
輸出:
A B
2011 560 349
2012 456 789
我手動,也希望在Excel通過VBA。任何幫助讚賞。
我有一個數據集移調在Excel
2011 A 560
2011 B 349
2012 A 456
2012 B 789
輸出:
A B
2011 560 349
2012 456 789
我手動,也希望在Excel通過VBA。任何幫助讚賞。
IFF這就是你的數據集的樣子。
選擇所有數據並插入數據透視表。 將您的行設置爲第一列(具有年份的列) 將您的列設置爲第二列(具有A或B的列) 將您的值設置爲第三列的平均值數字)
首先給你的數據包頭
創建數據透視表數據之外的信息
Sub PivotData()
Dim PivotSource As Range
Dim PivotName As String
Dim PivotDest As Range
PivotName = "PivotTable1"
Set PivotSource = Sheets("Sheet1").UsedRange
Set PivotDest = Sheets("Sheet2").Range("A1")
'Select the sheet containing data
Sheets(PivotSource.Parent.Name).Select
'Create a pivot cache out of the data
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=PivotSource, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=PivotDest, _
TableName:=PivotName, _
DefaultVersion:=xlPivotTableVersion14
'Select the destination Sheet
Sheets(PivotDest.Parent.Name).Select
'Create the pivot table
With ActiveSheet.PivotTables(PivotName)
.PivotFields("Year").Orientation = xlRowField
.PivotFields("Year").Position = 1
.AddDataField .PivotFields("Value"), "Sum of Value", xlSum
.PivotFields("Type").Orientation = xlColumnField
.PivotFields("Type").Position = 1
.RowGrand = False
.ColumnGrand = False
End With
'Convert the pivot table to a range
Cells.Copy
Cells.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
'Remove the pivot table header
Rows(1).Delete
Range("A1").ClearContents
ActiveSheet.UsedRange.Columns.AutoFit
Range("A1").Select
End Sub
結果:
谷歌搜索'轉置在Excel中'將產生許多有用的結果。如果您想在VBA中執行此操作,請記錄一個宏並查看它生成的代碼。儘管我懷疑你不想轉置,但看起來更像是一個數據透視表操作。 – Flynn1179 2014-09-12 12:37:55
我無法以這種方式轉置它。當我應用粘貼特殊的時候,它將整個桌子轉換成與我的預期相反的行。 – user1693842 2014-09-12 12:39:25
我該怎麼做手動?沒有vba? – user1693842 2014-09-12 12:41:13