简体   繁体   中英

Merging cells using vba

How can I do the same thing which the code below is doing, except I want to use cells instead of range ? That will allow me to refer to cells in (row,column) format.

ActiveSheet.Range("A1:A5").Merge

You could use the following to return the cell address of the appointed cell(s). Then use the stored string addresses to represent the merge range:

Sub merge()

    Dim cell1, cell2 As String

    cell1 = Cells(1, 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)

    cell2 = Cells(5, 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)

    ActiveSheet.Range(cell1 + ":" + cell2).merge

End Sub

You could use:

Cells(1, 1).Resize(5).Merge

or:

Range(Cells(1, 1), Cells(5, 1)).Merge

Define a range variable

Excel.Range range = ActiveSheet.get_range(ActiveSheet.Cells[1,1], ActiveSheet.Cells[1,4]);
range.Merge();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM