我编写了一个在打开文件时运行的宏,并以 range("a1").select 结束宏。问题是无论我用什么结束宏,即。滚动,选择,激活,当文件打开并且宏运行完成时,选定的单元格为 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