麻辣家族討論版版's Archiver

fish0226 發表於 2020-10-18 23:01

合併儲存格

新手試寫合併儲存格

一開始先用錄製如下[code]
Sub combine()
    Range("A1:L1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
End Sub
[/code]後來上網爬文發現可簡化為[code]
Sub 合併儲存格()
    Range("A1:L1").Select
    With Selection.MergeCells = True
    End With
  End Sub
[/code]一樣的效果,為什麼錄製多了這麼多,多出的細項意思是什麼呢?

ikboy 發表於 2020-10-19 09:40

錄製是電腦基於人手操作記錄下, 如 Select, 但寫是按人的思維做的, 前兩段你都已經看到是有分別, 那以很明顯目前人寫代碼可以繞過很多無謂的語句, 看下面還可以更短。[code]Sub 不同合併方法()
    Range("A1:L1").MergeCells = True
End Sub[/code]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供