Re: [問題] excel合併儲存格

看板Office作者 (windknife18)時間14年前 (2011/12/26 13:44), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串2/2 (看更多)
請試試看以下的 VBA 囉 Option Explicit Sub Macro1() Dim i As Integer Dim j As Integer Dim prei As Integer Dim rCount As Integer Dim a As String Dim b As String Dim c As String Application.ScreenUpdating = False Application.DisplayAlerts = False With ThisWorkbook.Sheets(1) rCount = .Cells(.Rows.Count, 1).End(xlUp).Row + 1 a = .Cells(1, 1) b = .Cells(1, 2) c = .Cells(1, 3) prei = 1 For i = 1 To rCount If .Cells(i, 1) <> a Or .Cells(i, 2) <> b Or _ .Cells(i, 3) <> c Then For j = 1 To 3 Range(Cells(prei, j), Cells(i - 1, j)).Select With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Next j a = .Cells(i, 1) b = .Cells(i, 2) c = .Cells(i, 3) prei = i End If Next i .Cells(1, 1).Select End With Application.DisplayAlerts = True Application.ScreenUpdating = True MsgBox ("成功") End Sub ※ 引述《copyme21 (草莓)》之銘言: : (若是和其他不同軟體互動之問題 請記得一併填寫) : 軟體:excel : 版本:2007 : 三民國小的20297的陳小明一筆,永福國小的21776的王大成四筆, : 以此類推,請問有什麼方法,快速把不規則的合併儲存格做處理? : 資料有五、六百筆,之前用手工合併,費很多時間, : 謝謝幫忙^^ : 原始資料: : A B C D : 1 三民國小 20297 陳小明 1 : 2 永福國小 21776 王大成 1 : 3 永福國小 21776 王大成 2 : 4 永福國小 21776 王大成 3 : 5 永福國小 21776 王大成 4 : 6 成德國小 20077 李玲玲 1 : 7 成德國小 20077 李玲玲 2 : 8 成德國小 20077 李玲玲 3 : 9 再興國小 20151 劉銘銘 1 : 合併儲存格後資料: : A B C D : 1 三民國小 20297 陳小明 1 : 2 永福國小 21776 王大成 1 : 3 2 : 4 3 : 5 4 : 6 成德國小 20077 李玲玲 1 : 7 2 : 8 3 : 9 再興國小 20151 劉銘銘 1 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.131.84.78

12/26 21:20, , 1F
感謝感謝幫忙,我沒用過VBA,會努力試試看^^
12/26 21:20, 1F

12/28 09:29, , 2F
對了資料要記得先依據A,B,C排序好喔...
12/28 09:29, 2F
文章代碼(AID): #1E-0dLZM (Office)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1E-0dLZM (Office)