Vba 我可以使这个宏更高效或更快吗?
我对编码是全新的。这个宏运行缓慢,我希望有人能帮我清理它。提前感谢你的帮助Vba 我可以使这个宏更高效或更快吗?,vba,excel,Vba,Excel,我对编码是全新的。这个宏运行缓慢,我希望有人能帮我清理它。提前感谢你的帮助 Sheets("Fresh Agent Leads").Select Sheets("Fresh Agent Leads").Copy After:=Workbooks( _ "MSS Call Routing Master List.xlsx").Sheets(1) Columns("F:F").Select Selection.Insert Shift:=xlToRig
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
我开发了代码,用从外部购买的新线索更新公司的“呼叫路由器”工作表。这些潜在客户以原始格式出现在名为Fresh Agents leads的工作表中。一旦将“新代理线索”表复制到“MSS呼叫路由主列表”文件中(该文件包含“呼叫路由”工作表),宏将减少原始数据,从而消除我们不使用的部分。然后,它重新格式化剩余内容,以匹配旧呼叫路由器工作表的格式,并将两者合并。然后重新命名新的主工作表以调用路由器
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
代码设计为从包含Fresh Agent Lead表的工作簿内部开始。指示用户在执行代码之前,在桌面上打开Fresh Agents Leads文件和MSS Call Routing Master List
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
我总是用下面这句话
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Application.ScreenUpdating = False
关闭使宏运行更快的屏幕更新。只需将其添加到宏的开头即可。虽然这段代码基本上属于CR,但由于我手头有很多时间,我决定至少发布一些关于您代码的内容
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
===
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
以下是一些提示:
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
。选择和。尽可能激活。录制宏是VBA的良好开端,但第一大步是离开这些属性提供的“舒适区”。一开始,它们是好的,但从长远来看,它们肯定会制造问题
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
此工作簿.Sheets(“Sheet1”)
等等。您更希望使用Sh1
或Sh2
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Union
、With
等将类似过程组合在一起。这与上面的(1)是一致的。稍后您将看到一个示例 Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
应用程序。屏幕更新
-Excel VBA中最佳的时间剃须技巧之一 Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
现在,有几个示例:
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
(1)避免。选择| |学习使用非常好的。复制一行
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
这部分
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
…可简化为:
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Range("A1").Copy Range("F:F")
With Columns("C:C")
.ColumnWidth = 8.29
.HorizontalAlignment = xlCenter
End With
With Rows(1:1)
.RowHeight = 30
.WrapText = True
End With
从四行到一行。而且它更具可读性。上面的第二个代码段基本上是这样写的,“将A1的值复制到整个F列”。请注意,这实际上相当占用内存,就像在Excel 2010中一样,使用该命令可以粘贴到一百万行或更多行。最好是具体的,比如范围(“F1:F1000”)
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
(2)将命令集中在一起
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
在“书面”VBA中将命令集中在一起与在宏中执行命令的方式不同。由于宏是记录的,所以一切都基于实时修改。在“书面”VBA中,您可以指定一个操作,该操作允许您对多个对象应用单个操作。例如,假设您希望删除A列和C列,同时将所有相关数据向左移动
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
录制宏以执行此操作时,可以同时选择a和C并将其删除。然而,大多数初学者选择安全路径,一次记录一列的删除,虽然安全,但这是非常违反直觉的。在删除之前选择两者是最好的选择
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
在书面的VBA中,上面的第二种方法是一种大规模的“不”方法(或者至少,它不是标准)。除非有特定和必要的原因,否则将类似的命令集中在一起是惯例,因为它在很大程度上消除了错误,并且不占用资源
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
在你的代码中
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
。。。阅读是如此痛苦。我们不知道为什么会有两次删除,我们也不确定S列中的数据最初在哪里,等等。在这样的情况下,提前确定要删除的范围并执行删除是最好的方法
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
例如,让我们假设您想要删除列A、C、E和F到O。下面这样一种简洁的方法将非常快速有效地实现这一点
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Union(Range("A:A"),Range("C:C"),Range("E:E"),Range("F:O")).Delete
Union
是你早期最好的朋友之一。与数学中的集合表示法一样,您指定的范围一起放在一组范围中,并同时对其进行操作(在本例中,.Delete
d同时执行)。由于默认的移位是向左的,我们可以完全删除shift:=xlToLeft
行(另一个漂亮的VBA事实)
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
(3)有
-一件事你不能没有
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
此时,您可能会想,在这些范围内执行多个操作如何?我们只在多个范围内执行了单个操作,而不是相反。这就是With
的作用点。在此上下文中,With
将仅用于范围
,但它几乎可以用于VBA中的任何内容。对象、范围、外部应用程序等。我将不深入讨论这一点,但我只想说,将与一起使用就像在您希望通过几个过程处理的事情上使用锚定一样
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
在你的代码中,我们发现
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
。。。可简化为:
Sheets("Fresh Agent Leads").Select
Sheets("Fresh Agent Leads").Copy After:=Workbooks( _
"MSS Call Routing Master List.xlsx").Sheets(1)
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:S").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RIGHT(RC[1],4))"
Range("C1").Select
Selection.AutoFill Destination:=Range("C1:C1048575")
Range("C1:C1048575").Select
Sheets("Call Router").Select
Rows("1:1").Select
Selection.Copy
Sheets("Fresh Agent Leads").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Application.Run "PERSONAL.xlsb!MergeIdenticalWorksheets"
Columns("C:C").Select
Selection.NumberFormat = "0000"
Range("A:A,B:B,F:F").Select
Range("F1").Activate
Selection.ColumnWidth = 14
Columns("E:E").Select
Selection.ColumnWidth = 25
Columns("C:C").Select
Selection.ColumnWidth = 8.29
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Rows("1:1").Select
Selection.RowHeight = 30
With Selection
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Columns("D:D").Select
Selection.EntireColumn.Hidden = True
Range("E2").Select
ActiveWindow.FreezePanes = True
Sheets(Array("Call Router", "Fresh Agent Leads")).Select
Sheets("Call Router").Activate
ActiveWindow.SelectedSheets.Delete
Sheets("Master").Select
Sheets("Master").Name = "Call Router"
Range("C23").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub
Range("A1").Copy Range("F:F")
With Columns("C:C")
.ColumnWidth = 8.29
.HorizontalAlignment = xlCenter
End With
With Rows(1:1)
.RowHeight = 30
.WrapText = True
End With
基本上,我们在这里做了两件事。首先,我们锚定在C列上,并对其执行两个操作:设置列宽,然后设置水平对齐。锚定到柱C和modi后