3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
Microsoft最近推出了Power Query,這是一個(gè)Excel加載項(xiàng),它為Excel內(nèi)的數(shù)據(jù)操作添加了許多有趣的功能,包括您要查找的內(nèi)容。
內(nèi)外接的實(shí)際功能被稱為“逆透視列”,這是解釋在這篇文章中。這是要點(diǎn):
下載并安裝加載項(xiàng)
打開您的Excel / CSV文件
選擇要熔化/重塑的表/范圍
在“高級(jí)查詢”選項(xiàng)卡中,單擊“從表”,這將打開“查詢編輯器”
選擇您要熔化/重塑的列(按Ctrl或Shift-Select,不要拖動(dòng))
在“轉(zhuǎn)換”選項(xiàng)卡中,單擊“取消透視列”(您還可以在此處應(yīng)用其他轉(zhuǎn)換,然后再返回Excel)
在“主頁(yè)”選項(xiàng)卡中,單擊“關(guān)閉并加載”。這將在Excel中創(chuàng)建具有所需結(jié)果的新表/查詢對(duì)象。

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
首先創(chuàng)建一個(gè)用戶窗體,并將其命名為Unpivot_Form,其中包含兩個(gè)RefEdit字段-rng_id和value_id以及一個(gè)提交/執(zhí)行按鈕。我也是R用戶,rng_id是包含id的范圍,而value_id包含值;兩個(gè)范圍都包括標(biāo)題。
做兩個(gè)宏:
Sub unpivot()
Unpivot_Form.Show
End Sub
另一個(gè)宏位于該字段的提交/執(zhí)行按鈕內(nèi):
Private Sub submit_Click()
'Code to unpivot (convert wide to long for excel)
Dim rng_id, rng_id_header, val_id As Range
Dim colvar, emptyrow, col As Integer
Dim new_sheet As Worksheet
'Put val_id range into a range object
Set val_id = Range(value_id.Value)
'Determine the parameter for the value id range
'This is used for the looping later on
numrows = val_id.Rows.Count
numcols = val_id.Columns.Count
'Resize changes the "block" to the size defined by the row and column
'Offset moves the "block"
Set rng_id_header = Range(range_id.Value).Resize(1)
Set rng_id = Range(range_id.Value).Offset(1, 0).Resize(numrows - 1)
Set new_sheet = Worksheets.Add
'Set up the first column and first batch of id vars
new_sheet.Activate
Range("A65535").End(xlUp).Activate
rng_id_header.Copy ActiveCell
colvar = Range("XFD1").End(xlToLeft).Column + 1
Range("XFD1").End(xlToLeft).Offset(, 1).Value = "Variable"
Range("XFD1").End(xlToLeft).Offset(, 1).Value = "Value"
'Start populating the value ids
For col = 1 To numcols
'populate var_id
'determine last row
emptyrow = Range("A65535").End(xlUp).Row + 1
'no need to activate to source to copy
rng_id.Copy new_sheet.Cells(emptyrow, 1)
'copy the variable
val_id.Offset(, col - 1).Resize(1, 1).Copy new_sheet.Range(Cells(emptyrow, colvar), Cells(emptyrow + numrows - 2, colvar))
'copy the value
val_id.Offset(1, col - 1).Resize(numrows - 1, 1).Copy new_sheet.Range(Cells(emptyrow, colvar + 1), Cells(emptyrow + numrows - 2, colvar + 1))
Next
Unload Me
End Sub
請(qǐng)享用!
- 3 回答
- 0 關(guān)注
- 721 瀏覽
添加回答
舉報(bào)