【发布时间】:2021-10-25 11:40:05
【问题描述】:
我是 VBA 编程新手,我对格式化表格的 Excel 宏有疑问。
Sub l___API_ENG()
'------------------------------Insert Formula--------------------------------------
Range("F2").Select
ActiveCell.SpecialCells(xlLastCell).Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(1, 6).Select
ActiveCell.FormulaR1C1 = "x"
ActiveCell.Offset(0, -1).Select
ActiveCell.FormulaR1C1 = "x"
ActiveCell.Offset(0, -1).Select
ActiveCell.FormulaR1C1 = "I"
Selection.AutoFilter
ActiveCell.CurrentRegion.AutoFilter Field:=5, Criteria1:="I"
Range("G4").Select
ActiveCell.FormulaR1C1 = "=RC[-1]+R[-2]C-R[-1]C"
Range("G4").Select
Selection.Copy
Range(Selection, Selection.End(xlToRight)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.SpecialCells(xlLastCell).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.ClearContents
ActiveCell.CurrentRegion.AutoFilter Field:=5
'------------------------------Lock Column--------------------------------------
Range("F2").Select
ActiveWindow.FreezePanes = True
'------------------------------Choose all cells from F2 down and right--------------------------------------
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
'------------------------------Set conditional format for the weekend - grey filling--------------------------------------
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=OR(WEEKDAY(F$1;2)=7;WEEKDAY(F$1;2)=6)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249946592608417
End With
Selection.FormatConditions(1).StopIfTrue = False
'------------------------------Set conditional format for Arrivals - blue filling--------------------------------------
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(F2>0;$E2=""A"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'------------------------------Set conditional format for shortage - red filling--------------------------------------
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(F2<0;$E2=""I"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'------------------------------Clear PN from rows P and I--------------------------------------
'------------------------------Set underline--------------------------------------
ActiveCell.CurrentRegion.AutoFilter Field:=5, Criteria1:="I"
ActiveCell.CurrentRegion.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveCell.CurrentRegion.AutoFilter Field:=5
Cells.Select
Cells.EntireColumn.AutoFit
Range("F2").Select
End Sub
问题是它应该将周末单元格涂成灰色,当我将区域日期格式设置为 21.07.22 时它可以工作,但在另一种情况下,例如 21/07/22 甚至 2022 年 7 月 21 日,它不会这样做. 通过在windows设置中切换日期格式,我发现当我从25.10.21格式切换时,阴影区域就消失了。有什么办法可以防止excel使用windows区域设置?
是否有任何功能可以替换条件格式的 AND 和 WEEKDAY,以便宏对使用不同系统语言的用户有效?
表格应该是这样的
【问题讨论】: