作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了以下代码,它消除了对 for 循环的需要,但它仍然卡住了 Excel。
这段代码本质上将使用边框、数字格式等以特定方式格式化 8 行。我需要加快速度,因为我正在运行它与我编写的另一个宏一起运行,该宏在合理的时间内工作,但添加这种格式会造成一些困惑。
Sub Format()
'Borders
Range("A2:F9,G2:I3,G4:I5,G6:I7,G8:I9,J2:V3,J4:V5,J6:V7,J8:V9,W2:W9,X2:AI3,X4:AI5,X6:AI7,X8:AI9,AJ2:AJ9").Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
'Format percentages
Range("X3:AI3,X5:AI5,X7:AI7,X9:AI9").Select
Selection.NumberFormat = "0.00%"
Range("F:F,J2:W" & endRow).Select
Selection.NumberFormat = "0"
Range("J1:V1").Select
Selection.NumberFormat = "mmm-yy"
Range("X1:AI1").Select
Selection.NumberFormat = "mmm"
'Text Alignment
Range("A:A,C:C,D:D,F:AJ").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.ReadingOrder = xlContext
End With
Range("A2:AJ9").Copy
Range("A2:AJ" & endRow).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A1,C1,D1,F1:I1,W1,AJ1").EntireColumn.AutoFit
Range("B1").ColumnWidth = 32
Range("E1").ColumnWidth = 40
Range("J1:V1,X1:AI1").ColumnWidth = 7.5
End Sub
变量 endRow 在其他地方确定,因为该宏在另一个内部调用。
Sub Format()
'Borders
Range("A2:F9,G2:I3,G4:I5,G6:I7,G8:I9,J2:V3,J4:V5,J6:V7,J8:V9,W2:W9,X2:AI3,X4:AI5,X6:AI7,X8:AI9,AJ2:AJ9").Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
'Format percentages
Range("X3:AI3,X5:AI5,X7:AI7,X9:AI9").Select
Selection.NumberFormat = "0.00%"
Range("F:F,J2:W" & endRow).Select
Selection.NumberFormat = "0"
'Text Alignment
Range("A:A,C:C,D:D,F:AJ").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.ReadingOrder = xlContext
End With
Range("A2:AJ9").Copy
Range("A2:AJ" & endRow).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A1,C1,D1,F1:I1,W1,AJ1").EntireColumn.AutoFit
Range("B1").ColumnWidth = 32
Range("E1").ColumnWidth = 40
Range("J1:V1,X1:AI1").ColumnWidth = 7.5
End Sub
编辑2:
Sub Format()
Dim rng As Range
Set rng = Sht.Range("A2:F9,G2:I3,G4:I5,G6:I7,G8:I9,J2:V3,J4:V5,J6:V7,J8:V9,W2:W9,X2:AI3,X4:AI5,X6:AI7,X8:AI9,AJ2:AJ9")
'Borders
With rng.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With rng.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With rng.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With rng.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
'Format percentages
Range("X3:AI3,X5:AI5,X7:AI7,X9:AI9").NumberFormat = "0.00%"
Range("F:F,J2:W" & endRow).NumberFormat = "0"
Range("J1:V1").NumberFormat = "mmm-yy"
Range("X1:AI1").NumberFormat = "mmm"
'Text Alignment
With Range("A:A,C:C,D:D,F:AJ")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.ReadingOrder = xlContext
End With
Range("A2:AJ9").Copy
Range("A2:AJ" & endRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Range("A1,C1,D1,F1:I1,W1,AJ1").EntireColumn.AutoFit
Range("B1").ColumnWidth = 32
Range("E1").ColumnWidth = 40
Range("J1:V1,X1:AI1").ColumnWidth = 7.5
End Sub
最佳答案
格式化数千行
Sub Format()
Const EndRow As Long = 80001
Application.ScreenUpdating = False
With ActiveSheet ' improve!
' Borders
With .Range("A2:F9,G2:I3,G4:I5,G6:I7,G8:I9,J2:V3,J4:V5," _
& "J6:V7,J8:V9,W2:W9,X2:AI3,X4:AI5,X6:AI7,X8:AI9,AJ2:AJ9")
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
End With
' Number Formats
.Range("X3:AI3,X5:AI5,X7:AI7,X9:AI9").NumberFormat = "0.00%"
.Range("F1:F9,J2:W9").NumberFormat = "0"
.Range("J1:V1").NumberFormat = "mmm-yy"
.Range("X1:AI1").NumberFormat = "mmm"
' Text Alignment
With .Range("A1:A9,C1:C9,D1:D9,F1:AJ9")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.ReadingOrder = xlContext
End With
' Copy Down Formats
.Range("A2:AJ9").Copy
.Range("A2:AJ" & EndRow).PasteSpecial Paste:=xlPasteFormats
' Column Widths
.Range("A1,C1,D1,F1:I1,W1,AJ1").EntireColumn.AutoFit
.Range("B1").ColumnWidth = 32
.Range("E1").ColumnWidth = 40
.Range("J1:V1,X1:AI1").ColumnWidth = 7.5
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
争论
Sub FormatSheet(ByVal ws As Worksheet)
Const EndRow As Long = 80001
Application.ScreenUpdating = False
With ws
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub Test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
FormatSheet ws
End Sub
EndRow
争论...Sub FormatSheet2(ByVal ws As Worksheet, ByVal EndRow As Long)
End Sub
并用例如调用它:FormatSheet2 ws, 80001
关于excel - 有没有办法加快格式化数千行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71928209/
我是一名优秀的程序员,十分优秀!