我写了一个在打开文件时运行的宏,并用范围(“a1”)结束了宏。选择。问题是无论我结束宏,即。滚动,选择,激活,当打开文件并且宏完成运行时,所选单元格为A8。 A8直接在一行中,单元格在几列之间合并,我不确定这是不是原因。求救!
代码:
Sub Macro1()
'
Rows("6:6").Select
Selection.Insert Shift:=xlDown
Range("B7:O8").Select
Selection.ClearContents
Range("B6:H8").Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("B6:H6").Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("B7:B8,E7:E8").Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("C7:C8,F7:F8").Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("D7:E7,G7:H7").Select
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("B6").Select
With Selection.Font
.Size = 14
End With
ActiveCell.FormulaR1C1 = "Current Period"
Range("B6:H6").Select
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Range("B8").Select
ActiveCell.FormulaR1C1 = "Actual"
Range("B8").Select
With Selection
.HorizontalAlignment = xlCenter
End With
With Selection.Font
.Name = "Helvetica"
.Size = 12
End With
Range("C8").Select
With Selection.Font
.Name = "Helvetica"
.Size = 12
End With
With Selection
.HorizontalAlignment = xlCenter
End With
ActiveCell.FormulaR1C1 = "Budget"
Rows("8:8").Select
With Selection
.HorizontalAlignment = xlCenter
End With
Range("D8").Select
ActiveCell.FormulaR1C1 = "$"
Range("E8").Select
ActiveCell.FormulaR1C1 = "%"
Range("D7:E8").Select
Range("D8").Activate
With Selection.Font
.Name = "Helvetica"
.Size = 10
End With
Range("F8").Select
With Selection.Font
.Name = "Helvetica"
.Size = 12
End With
ActiveCell.FormulaR1C1 = "Last Year"
Range("G7:H8").Select
Range("G8").Activate
ActiveCell.FormulaR1C1 = "$"
With Selection.Font
.Name = "Helvetica"
.Size = 10
End With
Range("H8").Select
ActiveCell.FormulaR1C1 = "%"
Range("D7").Select
ActiveCell.FormulaR1C1 = "B/(W) Than Budget"
Range("D7:E7").Select
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Range("G7:H7").Select
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
ActiveCell.FormulaR1C1 = "B/(W) Than Last Year"
Range("B6:H8").Select
Selection.Copy
Range("I6").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("I6").Select
ActiveCell.FormulaR1C1 = "Year To Date"
Rows("1:4").Select
With Selection.Font
.Size = 14
End With
Rows("9:138").Select
With Selection.Font
.Size = 10
End With
Rows("125:125").Select
Selection.ClearContents
Rows("133:133").Select
Selection.ClearContents
Range("B:D,F:G,I:K,M1,M:N").Select
Range("M1").Activate
Selection.ColumnWidth = 15
Range("E:E,h:h,l:l,o:o").Select
Selection.ColumnWidth = 12
Range("A:A").Select
Selection.ColumnWidth = 40
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$8"
End With
Rows("6:6").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Columns("i:i").Select
Selection.Insert Shift:=xlRight
Range("g14:i14").Select
With Selection
.HorizontalAlignment = xlLeft
End With
Range("g14:h14").Select
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Range("b13:i13").Select
With Selection
.HorizontalAlignment = xlLeft
End With
Range("b13:h13").Select
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Range("k16:k200").Select
Selection.Copy
Range("i16:i200").Select
Selection.PasteSpecial Paste:=xlPasteFormats
Range("i:i,q:q").Select
Selection.ColumnWidth = 38
With Selection
.WrapText = True
End With
Range("i13").Select
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Range("i16").Select
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Range("i17").Select
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Selection.Copy
Range("i17:i200,q16:q200").Select
ActiveSheet.Paste
Range("i14").Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
ActiveCell.FormulaR1C1 = "Describe Variance"
With Selection
.HorizontalAlignment = xlCenter
End With
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "to CP Budget"
With Selection
.HorizontalAlignment = xlCenter
End With
Range("i14:i15").Select
Selection.Copy
Range("q14:q15").Select
Selection.PasteSpecial Paste:=xlPasteFormats
Range("q14").Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
ActiveCell.FormulaR1C1 = "Describe Variance"
With Selection
.HorizontalAlignment = xlCenter
End With
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "to YTD Budget"
With Selection
.HorizontalAlignment = xlCenter
End With
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(2, 0).Select
Range(ActiveCell, ActiveCell.Offset(200, 0)).Select
Selection.EntireRow.Delete
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(3, 0).Select
ActiveCell = "Explanation of EBITDA Variance to Budget (Only required if variance exceeded the acceptable range for your group)"
Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
With Selection.Font
.Size = 14
.Bold = True
End With
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Selection.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
Selection.Merge
Selection.RowHeight = 50
With Selection.Font
.Size = 10
.Bold = True
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("a1:q11").Select
With Selection
.HorizontalAlignment = xlLeft
End With
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Range("a6").Select
ActiveCell = "Publisher's Summary"
With Selection.Font
.Size = 14
End With
Range("a9").Select
ActiveCell = "Market Outlook"
With Selection.Font
.Size = 14
End With
Rows("10:10").Select
Selection.RowHeight = 70
Range("a10:q10").Select
Selection.Merge
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
End With
With ActiveSheet.PageSetup
.RightHeader = "&9Page &P of &N"
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.25)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.1)
.FooterMargin = Application.InchesToPoints(0.15)
.CenterHorizontally = True
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 2
.Zoom = False
End With
ActiveWindow.Zoom = 70
Rows("7:7").Select
Selection.RowHeight = 180
Range("a7:q7").Select
Selection.Merge
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
End With
'
Cells.Select
With Selection
.Locked = False
End With
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(1, 1).Select
Range(ActiveCell, "h13").Select
With Selection
.Locked = True
End With
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(1, 9).Select
Range(ActiveCell, "p13").Select
With Selection
.Locked = True
End With
Range("a1").Select
ActiveSheet.Protect userinterfaceonly:=True
ActiveWindow.FreezePanes = False
End Sub
答案 0 :(得分:1)
Range("a1").Select
ActiveSheet.Protect userinterfaceonly:=True
我怀疑A1是一个锁定的单元格,因此当您保护工作表时,它将跳转到最近的未锁定的可用单元格(假设从上次使用中未选中允许选择锁定单元格的选项)。
答案 1 :(得分:0)
如果您的宏在工作表之间跳转,则在选择时可能会使用错误的操作。 尝试将您的选择更改为:
Worksheets("sheetname").Range("A1").Select
答案 2 :(得分:0)
您应该首先删除所有.select语句(或尽可能多)。我知道这不是一个明确的答案,但是要做出评论的方式很多。我相信在你的代码中删除.Select将解决这个问题......但是要对它进行全面排序将会有一些工作。尽管如此,这是一个开始:
Option Explicit
Sub Macro1()
Application.ScreenUpdating = False
'
Rows("6:6").Insert Shift:=xlDown
Range("B7:O8").ClearContents
With Range("B6:H8").Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Range("B6:H6").Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Range("B7:B8,E7:E8").Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Range("C7:C8,F7:F8").Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Range("D7:E7,G7:H7").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Range("B6").Font
.Size = 14
End With
Range("B6").FormulaR1C1 = "Current Period"
Range("B6:H6").HorizontalAlignment = xlCenterAcrossSelection
Range("B8").FormulaR1C1 = "Actual"
Range("B8").HorizontalAlignment = xlCenter
With Range("B8").Font
.Name = "Helvetica"
.Size = 12
End With
With Range("C8").Font
.Name = "Helvetica"
.Size = 12
End With
Range("C8").HorizontalAlignment = xlCenter
ActiveCell.FormulaR1C1 = "Budget"
Rows("8:8").Select
With Selection
.HorizontalAlignment = xlCenter
End With
Range("D8").FormulaR1C1 = "$"
Range("E8").FormulaR1C1 = "%"
With Range("D7:E8").Font
.Name = "Helvetica"
.Size = 10
End With
With Range("F8").Font
.Name = "Helvetica"
.Size = 12
End With
Range("F8").FormulaR1C1 = "Last Year"
Range("G8").FormulaR1C1 = "$"
With Range("G7:H8").Font
.Name = "Helvetica"
.Size = 10
End With
Range("H8").FormulaR1C1 = "%"
Range("D7").FormulaR1C1 = "B/(W) Than Budget"
Range("D7:E7").HorizontalAlignment = xlCenterAcrossSelection
Range("G7:H7").HorizontalAlignment = xlCenterAcrossSelection
Range("G7").FormulaR1C1 = "B/(W) Than Last Year"
Range("B6:H8").Copy Range("I6")
Range("I6").FormulaR1C1 = "Year To Date"
With Rows("1:4").Font
.Size = 14
End With
With Rows("9:138").Font
.Size = 10
End With
Rows("125:125").ClearContents
Rows("133:133").ClearContents
Range("B:D,F:G,I:K,M1,M:N").ColumnWidth = 15
Range("E:E,h:h,l:l,o:o").ColumnWidth = 12
Range("A:A").ColumnWidth = 40
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$8"
End With
Rows("6:12").Insert Shift:=xlDown
Columns("i:i").Insert Shift:=xlRight
Range("g14:i14").HorizontalAlignment = xlLeft
Range("g14:h14").HorizontalAlignment = xlCenterAcrossSelection
Range("b13:i13").HorizontalAlignment = xlLeft
Range("b13:h13").HorizontalAlignment = xlCenterAcrossSelection
Range("k16:k200").Copy
Range("i16:i200").PasteSpecial Paste:=xlPasteFormats
Range("i:i,q:q").ColumnWidth = 38
Range("i:i,q:q").WrapText = True
Range("i13").Borders(xlEdgeTop).LineStyle = xlNone
Range("i13").Borders(xlEdgeBottom).LineStyle = xlNone
With Range("i16").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
With Range("i17").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
.ColorIndex = xlAutomatic
End With
Range("i17").Copy Range("i17:i200", "q16:q200")
With Range("i14").Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("i14").FormulaR1C1 = "Describe Variance"
Range("i14").HorizontalAlignment = xlCenter
Range("i14").Offset(1, 0).FormulaR1C1 = "to CP Budget"
Range("i14").Offset(1, 0).HorizontalAlignment = xlCenter
Range("i14:i15").Copy
Range("q14:q15").PasteSpecial Paste:=xlPasteFormats
With Range("q14").Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("q14").FormulaR1C1 = "Describe Variance"
Range("q14").HorizontalAlignment = xlCenter
Range("q14").Offset(1, 0).FormulaR1C1 = "to YTD Budget"
Range("q14").Offset(1, 0).HorizontalAlignment = xlCenter
'''''''''''
' Stopped here
'''''''''''
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(2, 0).Select
Range(ActiveCell, ActiveCell.Offset(200, 0)).Select
Selection.EntireRow.Delete
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(3, 0).Select
ActiveCell = "Explanation of EBITDA Variance to Budget (Only required if variance exceeded the acceptable range for your group)"
Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
With Selection.Font
.Size = 14
.Bold = True
End With
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Selection.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
Selection.Merge
Selection.RowHeight = 50
With Selection.Font
.Size = 10
.Bold = True
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
End With
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
Range("a1:q11").Select
With Selection
.HorizontalAlignment = xlLeft
End With
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
End With
Range("a6").Select
ActiveCell = "Publisher's Summary"
With Selection.Font
.Size = 14
End With
Range("a9").Select
ActiveCell = "Market Outlook"
With Selection.Font
.Size = 14
End With
Rows("10:10").Select
Selection.RowHeight = 70
Range("a10:q10").Select
Selection.Merge
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
End With
With ActiveSheet.PageSetup
.RightHeader = "&9Page &P of &N"
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.25)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.1)
.FooterMargin = Application.InchesToPoints(0.15)
.CenterHorizontally = True
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 2
.Zoom = False
End With
ActiveWindow.Zoom = 70
Rows("7:7").Select
Selection.RowHeight = 180
Range("a7:q7").Select
Selection.Merge
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.WrapText = True
End With
Cells.Select
With Selection
.Locked = False
End With
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(1, 1).Select
Range(ActiveCell, "h13").Select
With Selection
.Locked = True
End With
Cells.Find("ebitda", lookat:=xlWhole).Select
Selection.Offset(1, 9).Select
Range(ActiveCell, "p13").Select
With Selection
.Locked = True
End With
Range("a1").Select
ActiveSheet.Protect userinterfaceonly:=True
ActiveWindow.FreezePanes = False
Application.ScreenUpdating = True
End Sub
你会发现你的宏运行得更快,调试也更容易。我从那里开始。希望您能够获取我上面发布的代码并完成修改代码,我放入了一个注释块,我停止了。一旦你完成了,你可能会注意到问题完全消失了(因为你没有选择任何东西开始)。但即使你仍然遇到问题,整理起来也会容易得多。
这至少是我的答案,我希望它可以帮助你走正确的道路!