【问题标题】:How can I implement variables and optimize the below code?如何实现变量并优化以下代码?
【发布时间】:2019-07-22 01:08:48
【问题描述】:

我有一个工作项目,它计算 1 个 WS 中的数据并将摘要粘贴到另一个上。 由于在同一个工作簿中,我还有各种其他带有图表、数据透视表或其他代码的工作表,因此计算有时会变慢。 由于我是初学者,我的大部分代码都是录制的宏,这使得它有点长。该代码有效,但是,我想知道有没有办法使代码更短? 几天前,我要求进行部分计算,但是,我没有设法实施提供的解决方案。 我会很感激建议和想法。谢谢你

以下代码将“L:M”列中的数据从文本中清除,并将它们移到前面进行计算,其结果将粘贴到“统计”工作表上。 H 列有“IF”逻辑语句。

    Sub RemoveEUR()
 'remove  currency from cell

    Range("L3:M5000").Select

         Selection.Cut

    Range("F3:G5000").Select

         ActiveSheet.Paste


    Range("A3").Select

    Cells.Replace What:="eur", 
          Replacement:="",LookAt:=xlPart,SearchOrder:=xlByRows, _
          MatchCase:=False, SearchFormat:=False, _
          ReplaceFormat:=False

    Columns("I:N").Select

             Selection.Delete Shift:=xlToLeft

    Columns("E:G").Select

            Selection.NumberFormat = "€##,##0.00_)"

    Range("H3").Select

           ActiveCell.FormulaR1C1 = "=IF(RC[-1]>RC[-2],1,0)"

         Selection.AutoFill Destination:=Range("H3:H5000")

    Columns("H:H").Select

            Selection.HorizontalAlignment = xlCenter

            Columns(8).NumberFormat = "0"

    Worksheets("Statistics").Activate

    Range("F1").Select

       ActiveCell.FormulaR1C1 = _

        "=""Win Count = ""&SUM(GameList!R[2]C[2]:R[4999]C[2])&"" Out Of ""&COUNTA(GameList!C[-2])-1&"" GR"""


    Range("A4").Offset(1, 1).Select

            ActiveCell.FormulaR1C1 = "=COUNTA(GameList!C[-1])-2"

    Range("B6").Select

            ActiveCell.FormulaR1C1 = "=SUM(GameList!C[4])"

            ActiveCell.Offset(1, 0).FormulaR1C1 = "=AVERAGE(GameList!C[4])"

            ActiveCell.Offset(2, 0).FormulaR1C1 = "=SUM(GameList!C[4])-SUM(GameList!C[5])"

            ActiveCell.Offset(3, 0).FormulaR1C1 = "=R[-1]C/R[-3]C"

            Selection.NumberFormat = "€#,##0.00;[Red]-€#,##0.00"
End Sub

【问题讨论】:

标签: excel vba calculation


【解决方案1】:

我要感谢这里所有提供答案的贡献者,例如 @Ken White 和 @peege,以及我的 Udemy 讲师 Alan Jarvis,他们的建议帮助我找到了解决方案:)

Sub RemoveEUR() '从单元格中删除货币并计算统计数据

Range("L3:M5000").Select
     Selection.Cut
Range("F3:G5000").Select
     ActiveSheet.Paste

Range("A3").Select
Cells.Replace What:="eur", Replacement:=" ", _
    LookAt:=xlPart, SearchOrder:=xlByRows, _
    MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
Range("I:I,J:J,K:K,L:L,M:M,N:N").Delete
Columns("E:G").NumberFormat = "€##,##0.00_)"

Range("D:H").HorizontalAlignment = xlCenter
Range("H3:H10000").Formula = "=IF(RC[-1]>RC[-2],1,0)"
Columns(8).NumberFormat = "0"

Worksheets("Player's Statistics").Activate

Range("F1").Select
ActiveCell.FormulaR1C1 = _
    "=""Win Count = ""&SUM(GameList!R[2]C[2]:R[4999]C[2])&"" Out Of ""&COUNTA(GameList!C[-2])-2&"" GR"""

Range("A4").Offset(1, 1).Select
        ActiveCell.FormulaR1C1 = "=COUNTA(GameList!C[-1])-1"
Range("B6").Select
        ActiveCell.Formula = "=SUM(GameList!C[4])"
        ActiveCell.Offset(1, 0).Formula = "=AVERAGE(GameList!C[4])"
        ActiveCell.Offset(2, 0).Formula = "=SUM(GameList!C[4])-SUM(GameList!C[5])"
        ActiveCell.Offset(3, 0).Formula = "=R[-1]C/R[-3]C"
        Selection.NumberFormat = "€#,##0.00;[Red]-€#,##0.00"

结束子

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多