选择向下七个单元格宏

问题描述 投票:0回答:1

我想要一个宏来选择所选单元格下方的七个单元格,合并它们并旋转文本。我的宏的问题是它不适用于任何单元格。有谁可以帮忙吗

Sub Merge7()
'
' Merge7 Macro
'

'
    Range("A10:A17").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 90
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
    End With
    With Selection.Font
        .Name = "Calibri"
        .Size = 20
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    Range("A18").Select
End Sub

我想将它与任何单元格一起使用,而不仅仅是 A10:A17

excel macros
1个回答
0
投票

应用于活动单元格

用法

Sub Merge7()
    MergeBelowCell 7
End Sub

方法

Sub MergeBelowCell(Optional ByVal CellsBelow As Long = 1)
    
    With ActiveCell.Resize(CellsBelow + 1)
        Application.DisplayAlerts = False
            .Merge
        Application.DisplayAlerts = True
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        With .Font
            .Name = "Calibri"
            .Size = 20
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
        .Orientation = 90
        .Offset(1).Select
    End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.