Re: [算表] 請問如何以函數欄轉列(局部且欄數非固 …(VBA)
以下是 VBA 程式,看看是不是你要的?
使用的方法請參考精華區或之前的文章
Sub Consolidate()
Dim lastrow As Long, nextcol As Long, lastcol As Long, i As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, _
Header:= xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
For i = lastrow To 2 Step -1
If Cells(i, 1).Value = Cells(i - 1, 1).Value Then
nextcol = Cells(i - 1, Columns.Count).End(xlToLeft).Column + 1
lastcol = Cells(i, Columns.Count).End(xlToLeft).Column
Range(Cells(i, 2), Cells(i, lastcol)).Copy Cells(i - 1, nextcol)
Rows(i).EntireRow.Delete (xlShiftUp)
End If
Next i
Application.ScreenUpdating = True
MsgBox ("完成")
End Sub
※ 引述《liquidbox (樹枝擺擺)》之銘言:
: 軟體:excel
: 版本:2003
: 資料範例如下:
: A B
: 1 王小明 硬碟
: 2 王小明 記憶體
: 3 王小明 顯示卡
: 4 趙大華 蛋餅
: 5 趙大華 筆記本
: ...
: ....
: 請問能否利用excel函數將紀錄整理為
: A B C D
: 1 王小明 硬碟 記憶體 顯示卡
: 2 趙大華 蛋餅 筆記本
: 3 .....
: 因我時常有需要將資訊進行以上轉換,且資訊類型差異頗大,即:
: 原A欄不一定為人名,可能為編號、英文、中文,
: 原B欄也不一定為中文,甚至可能是一段數百字元、中英與符號夾雜的文章,
: 且資訊轉換後,王小明與趙大華後接的欄位數不定,可能一個,也可能有數十個。
: 我在這個版的舊文中有看到類似案例,
: 但這個範例轉換後的欄位數不定,且原A欄與原B欄在內容上沒有對應關係,
: 所以無法單純以舊文方法解決。
: 我目前的做法是用word重新排版,將分行、分段、分頁、tab等符號取代掉,
: 另行定義新的分隔符號、存成txt、匯入excel。
: 只是這種做法有其危險性,因為原B欄可能為各式文章,
: 取代分隔符號恐危及內容完整性。
: 所以我想請問,是否能以函數進行如此轉換?
: 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.229.86.165
推
06/29 21:24, , 1F
06/29 21:24, 1F
→
06/30 17:17, , 2F
06/30 17:17, 2F
Office 近期熱門文章
PTT數位生活區 即時熱門文章