让宏在选定范围内运行

时间:2016-04-27 21:27:12

标签: excel vba excel-vba

我创建了一个完美的宏,当然因为所有的宏都是标准的 - 只能按照它所记录的确切行进行工作。我需要它在我突出显示的任何行中工作,并且我尝试了各种自定义编码。除了相同的公式和格式在同一区域之外,我无法做任何事情。始终排5.这是代码......

Sub OrschelnMacro()
'
' OrschelnMacro Macro
'
' Keyboard Shortcut: Ctrl+p
'
    Rows("5:5").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("E5").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
    Range("G5").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
    Range("H5").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("F5").Select
    ActiveCell.FormulaR1C1 = "1 of 1"
    Rows("5:5").Select
    Selection.RowHeight = 75
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .Name = "Calibri"
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Font
        .Name = "Calibri"
        .Size = 26
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    Selection.Font.Bold = True
    Range("H5").Select
    With Selection.Font
        .Name = "Calibri"
        .Size = 72
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("E5:G5").Select
    With Selection
        .VerticalAlignment = xlTop
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("H5").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlTop
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("A5:H5").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Range("K7").Select
End Sub

有没有人有任何想法?非常感谢你...

2 个答案:

答案 0 :(得分:1)

我创建了一个快速的代码来执行您在任何行上所要求的操作,只需在要运行的行的任何单元格中单击即可。我不推荐这个代码,因为它相当草率并且有很多重复的代码,但它可以工作。我会努力学习它正在做什么,并摆脱任何额外的代码。

我也摆脱了几乎所有的select语句,因为它们减慢了你的代码,而不是选择一个单元格然后设置公式,你只需将它放在一行中,就像我对你的代码一样。

祝你好运学习VBA,这很有趣,你在这个网站上有很多知识渊博的人可以帮助你获得帮助。

Sub OrschelnMacro()
'
' OrschelnMacro Macro
'
' Keyboard Shortcut: Ctrl+p
'
curRow = Selection.Row
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E" & curRow).FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
Range("G" & curRow).FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
Range("H" & curRow).FormulaR1C1 = "1"
Range("F" & curRow).FormulaR1C1 = "1 of 1"
Rows(curRow).RowHeight = 75
With Rows(curRow).Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
With Rows(curRow).Font
    .Name = "Calibri"
    .Size = 26
    .Bold = True
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With
Range("H" & curRow).Select
With Range("H" & curRow).Font
    .Name = "Calibri"
    .Size = 72
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With
With Range("H" & curRow)
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlTop
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Range("E" & curRow & ":G" & curRow)
    .VerticalAlignment = xlTop
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Range("H" & curRow)
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlTop
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Range("A" & curRow & ":H" & curRow).Borders(xlDiagonalDown).LineStyle = xlNone
Range("A" & curRow & ":H" & curRow).Borders(xlDiagonalUp).LineStyle = xlNone
With Range("A" & curRow & ":H" & curRow).Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
With Range("A" & curRow & ":H" & curRow).Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
With Range("A" & curRow & ":H" & curRow).Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
With Range("A" & curRow & ":H" & curRow).Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
Range("A" & curRow & ":H" & curRow).Borders(xlInsideVertical).LineStyle = xlNone
Range("A" & curRow & ":H" & curRow).Borders(xlInsideHorizontal).LineStyle = xlNone
End Sub

答案 1 :(得分:1)

替换它:

var time:NSDate {
    didSet {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "h a"

        self.title.text = dateFormatter.stringFromDate(time)
    }
}

Rows("5:5").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E5").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
Range("G5").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-1]C)"
Range("H5").Select
ActiveCell.FormulaR1C1 = "1"
Range("F5").Select
ActiveCell.FormulaR1C1 = "1 of 1"
Rows("5:5").Select
Selection.RowHeight = 75

我不会进入所有格式化代码,因为它不是您的问题所在 - 关键是您可以使用变量获取Dim myRow As Long myRow = Selection.Row Rows(myRow).Insert Range("E" & myRow & ":H" & myRow).FormulaR1C1 = _ Array("=SUM(R[-2]C:R[-1]C)", "1 of 1", "=SUM(R[-2]C:R[-1]C)", "1") Rows(myRow).RowHeight = 75 属性并使用该属性在你的代码中。