【问题标题】:Macro without regional date format dependency没有区域日期格式依赖的宏
【发布时间】: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,以便宏对使用不同系统语言的用户有效?

表格应该是这样的

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    检查您的屏幕截图,您正在测试的日期似乎是表格的列标题。

    既然如此,它们不是“真正的日期”,而是显示为日期的文本字符串。

    当您的区域设置一致时,Excel 可以将它们解释为日期,当设置不一致时,Excel 不能。

    因此,您必须修改 Formula1 参数以将这些点分隔的 DMY 日期字符串转换为“真实”日期。

    您可以通过添加一个 UDF 来转换到您的 VBA 项目,然后在 Formula1 中使用它来实现此目的

    UDF

    Option Explicit
    Function dateFromString(strDate) As Date
        Const dtSep As String = "."
        Dim dtParts
        Dim y As Long, m As Long, d As Long
        Dim I As Long
        
    dtParts = Split(strDate, dtSep)
        y = dtParts(2)
        m = dtParts(1)
        d = dtParts(0)
    
    dateFromString = DateSerial(y, m, d)
            
    End Function
    

    然后你会在 Formula1 中使用它,如下所示:

    FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=AND(LEN($F1)>0;WEEKDAY(datefromstring($F1);2)>5)"
    

    就您的其他问题而言,关于使用国际上有效的公式作为条件格式 Formula1(和 Formula2)参数,您可以按照Excel Macro, inserting internationally valid formula during run-time 的接受答案中所示的方式翻译它们

    尤其是作为 VBA 的新手,试着改掉 vba 宏记录器似乎鼓励的坏习惯。

    你应该摆脱所有那些选择、选择、活动单元等。请阅读How to avoid using Select in Excel VBA

    此外,始终声明变量是一种很好的做法(尽管乍一看您的代码似乎没有使用任何变量)。在 VBA GUI 中,您可以通过 Tools/Options/Editor/Code Settings 并选择 Require Variable Declaration 来强制执行此操作。这会将OPTION EXPLICIT 放置在每个插入模块的顶部(您也可以手动将其添加到选择此选项之前创建的模块中)。这对于捕捉拼写错误、类型不正确的变量等非常有用。

    【讨论】:

    • 非常感谢您的帮助。我不是这个宏的作者,但我正在努力让它变得更好。尽管我知道 Python 和 C#,但我发现 VBA 令人沮丧。难道你不知道一些学习 VBA 的好资源吗?就像 Sololearn 一样。
    • @Freemasoid 我认为我不是问这个问题的合适人选。我是自学的,对编码和计算机很感兴趣。我边做边学;阅读;并在我的贡献受到批评的论坛上闲逛。顺便说一句,如果我的回答满足您的问题,如果您将其标记为已接受,我将不胜感激。见What should I do when someone answers my question
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2017-03-08
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    相关资源
    最近更新 更多