共用方式為


NamedRange.Merge 方法

NamedRange 控制項建立合併儲存格。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
Sub Merge ( _
    Across As Object _
)
void Merge(
    Object Across
)

參數

  • Across
    型別:System.Object
    true 表示將指定範圍內每一列的儲存格合併為個別的合併儲存格。預設值是 false。

備註

合併範圍的值在 NamedRange 控制項左上角的儲存格內指定。

選擇性參數

如需選擇性參數的詳細資訊,請參閱Office 方案中的選擇性參數

範例

在下列範例中,程式碼會顯示在使用 Merge 方法,合併包含單一儲存格 NamedRange 的多重儲存格 NamedRange 之前與之後,單一儲存格 NamedRangeMergeAreaMergeCells 屬性的值。

這是示範文件層級自訂的範例。

    Private Sub MergeRange()
        Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("A1", "A5"), _
            "namedRange1")

        Dim namedRange2 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("A1"), "namedRange2")

        Dim beforeMergeAddress As String = _
            namedRange2.MergeArea.Address(, , _
            Excel.XlReferenceStyle.xlA1, , )

        MessageBox.Show("Before merging, the MergeArea property is '" & _
            beforeMergeAddress & "' and the MergeCells property is " & _
            " '" & namedRange2.MergeCells.ToString() & "'.")

        namedRange1.Select()
        namedRange1.Merge(False)

        Dim afterMergeAddress As String = _
            namedRange2.MergeArea.Address(, , _
            Excel.XlReferenceStyle.xlA1, , )

        MessageBox.Show("After merging, the MergeArea property is '" & _
            afterMergeAddress & "' and the MergeCells property is " & _
            " '" & namedRange2.MergeCells.ToString() & "'.")
    End Sub

private void MergeRange()
{
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A5"],
        "namedRange1");

    Microsoft.Office.Tools.Excel.NamedRange namedRange2 =
        this.Controls.AddNamedRange(this.Range["A1"],
        "namedRange2");

    string beforeMergeAddress = namedRange2.MergeArea.get_Address(
        Excel.XlReferenceStyle.xlA1);

    MessageBox.Show("Before merging, the MergeArea property is '" +
        beforeMergeAddress + "' and the MergeCells property is " +
        " '" + namedRange2.MergeCells.ToString() + "'.");

    namedRange1.Select();
    namedRange1.Merge(false);

    string afterMergeAddress = namedRange2.MergeArea.get_Address(
        Excel.XlReferenceStyle.xlA1);

    MessageBox.Show("After merging, the MergeArea property is '" +
        afterMergeAddress + "' and the MergeCells property is " +
        " '" + namedRange2.MergeCells.ToString() + "'.");
}

.NET Framework 安全性

請參閱

參考

NamedRange 介面

Microsoft.Office.Tools.Excel 命名空間