获取macros在选定范围内进行操作

我创build了一个完美的macros,但是当然,所有的macros都是标准的 – 只能在logging的确切的行上工作。我需要它在任何一行中工作,我突出显示,我尝试了各种自定义编码。 我不能让它做任何事情,但在同一地区的顶部相同的公式和格式。 总是第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 

有没有人有任何想法? 提前谢谢你…

我创build了一小段代码来完成你在任何行上所要求的操作,只需要在你想要运行的行的任何单元格中点击即可。 我不会推荐这个代码,因为它是相当马虎,有很多重复的代码,但它的工作原理。 我会努力学习它在做什么,摆脱任何额外的代码。

我也摆脱了几乎所有的select语句,因为它们减慢了你的代码,而不是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 

replace这个:

 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 

我不会涉及所有的格式化代码,因为它不是真正的问题是什么 – 关键是你可以使用一个variables来获得.Row属性,并在代码中使用它。