更有效的写法是什么?

huangapple go评论61阅读模式
英文:

What is a more effective way to write this?

问题

以下是代码部分的翻译:

    Call ParaOff
    Dim i As Integer
    Dim j As Integer
    Dim x As Integer
    Dim name As String
    j = 5
    Call HideX
    Call SortWorksheets
    If Not WorksheetExists(wsF) Then
        MsgBox "错误:工作表 '" & wsF & "' 丢失。"
    Else
        'Sheets(wsF).Activate
        Worksheets(wsF).Range("B7:U41").ClearContents
        Worksheets(wsF).Range("W7:W41").ClearContents
        For i = 1 To Worksheets.Count
            If Worksheets(i).Name <> wsF And Worksheets(i).Name <> wsG And Worksheets(i).Name <> wsI And Worksheets(i).Name <> wsJ And Worksheets(i).Name <> wsK Then
                Worksheets(i).Range("S1").Copy '项目
                Worksheets(wsF).Range("B" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("J6").Copy '位置
                Worksheets(wsF).Range("C" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("Q1").Copy '持续时间
                Worksheets(wsF).Range("D" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("M1").Copy '项目总计
                Worksheets(wsF).Range("E" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("S10").Copy '其他%
                Worksheets(wsF).Range("F" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("S11").Copy '其他$
                Worksheets(wsF).Range("G" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("N10").Copy '其他%
                Worksheets(wsF).Range("H" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("N11").Copy '其他$
                Worksheets(wsF).Range("I" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("P13").Copy 'GC总计
                Worksheets(wsF).Range("J" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("K11").Copy 'GC%
                Worksheets(wsF).Range("K" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("L11").Copy 'GC天
                Worksheets(wsF).Range("L" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("M11").Copy 'GC月
                Worksheets(wsF).Range("M" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("S14").Copy 'PM%
                Worksheets(wsF).Range("N" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("K14").Copy 'PM小时
                Worksheets(wsF).Range("O" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("S16").Copy '监管%
                Worksheets(wsF).Range("P" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("K16").Copy '监管小时
                Worksheets(wsF).Range("Q" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("S18").Copy 'PE%
                Worksheets(wsF).Range("R" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("K18").Copy 'PE小时
                Worksheets(wsF).Range("S" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range("Q10").Copy 'CARP小时
                Worksheets(wsF).Range("T" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                x = fFindRowByCol(Worksheets(i).Name, "I", "Div 26*")
                Worksheets(i).Range("P" & x).Copy 'DIV 26 $
                Worksheets(wsF).Range("U" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                x = fFindRowByCol(Worksheets(i).Name, "I", "Div 32*")
                Worksheets(i).Range("P" & x).Copy 'DIV 32 $
                Worksheets(wsF).Range("W" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If
        Next i
    End If
    Call ParaOn
    Call Happy
End Sub

希望这有所帮助!

英文:

Workbook contains a summary worksheet and a random number of estimate worksheets. This sub populates the summary worksheet with a manual update button. Most of the data are in known, fixed cells. Some data are in a random row on the estimate worksheets. I'm using a function to find those rows. This sub runs slowly, and I'm sure this is the 'sledgehammer' method. Should I write the cell values to an array first and then paste in the summary sheet? Or other, better methods?

Sub UpdateSummary()
    Call ParaOff
    Dim i As Integer
    Dim j As Integer
    Dim x As Integer
    Dim name As String
    j = 5
    Call HideX
    Call SortWorksheets
    If Not WorksheetExists(wsF) Then
        MsgBox &quot;ERROR: Worksheet &#39;&quot; &amp; wsF &amp; &quot;&#39; is missing.&quot;
    Else
        &#39;Sheets(wsF).Activate
        Worksheets(wsF).Range(&quot;B7:U41&quot;).ClearContents
        Worksheets(wsF).Range(&quot;W7:W41&quot;).ClearContents
        For i = 1 To Worksheets.Count
            If Worksheets(i).name &lt;&gt; wsF And Worksheets(i).name &lt;&gt; wsG And Worksheets(i).name &lt;&gt; wsI And Worksheets(i).name &lt;&gt; wsJ And Worksheets(i).name &lt;&gt; wsK Then
                Worksheets(i).Range(&quot;S1&quot;).Copy &#39;PROJECT
                Worksheets(wsF).Range(&quot;B&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;J6&quot;).Copy &#39;LOCATION
                Worksheets(wsF).Range(&quot;C&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;Q1&quot;).Copy &#39;DURATION
                Worksheets(wsF).Range(&quot;D&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;M1&quot;).Copy &#39;PROJECT TOTAL
                Worksheets(wsF).Range(&quot;E&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;S10&quot;).Copy &#39;MISC %
                Worksheets(wsF).Range(&quot;F&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;S11&quot;).Copy &#39;MISC $
                Worksheets(wsF).Range(&quot;G&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;N10&quot;).Copy &#39;OTHER %
                Worksheets(wsF).Range(&quot;H&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;N11&quot;).Copy &#39;OTHER $
                Worksheets(wsF).Range(&quot;I&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;P13&quot;).Copy &#39;GC TOTAL
                Worksheets(wsF).Range(&quot;J&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;K11&quot;).Copy &#39;GC %
                Worksheets(wsF).Range(&quot;K&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;L11&quot;).Copy &#39;GC DAY
                Worksheets(wsF).Range(&quot;L&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;M11&quot;).Copy &#39;GC MONTH
                Worksheets(wsF).Range(&quot;M&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;S14&quot;).Copy &#39;PM %
                Worksheets(wsF).Range(&quot;N&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;K14&quot;).Copy &#39;PM HRS
                Worksheets(wsF).Range(&quot;O&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;S16&quot;).Copy &#39;SUPER %
                Worksheets(wsF).Range(&quot;P&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;K16&quot;).Copy &#39;SUPER HRS
                Worksheets(wsF).Range(&quot;Q&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;S18&quot;).Copy &#39;PE %
                Worksheets(wsF).Range(&quot;R&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;K18&quot;).Copy &#39;PE HRS
                Worksheets(wsF).Range(&quot;S&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                Worksheets(i).Range(&quot;Q10&quot;).Copy &#39;CARP HRS
                Worksheets(wsF).Range(&quot;T&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                x = fFindRowByCol(Worksheets(i).name, &quot;I&quot;, &quot;Div 26*&quot;)
                Worksheets(i).Range(&quot;P&quot; &amp; x).Copy &#39;DIV 26 $
                Worksheets(wsF).Range(&quot;U&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                x = fFindRowByCol(Worksheets(i).name, &quot;I&quot;, &quot;Div 32*&quot;)
                Worksheets(i).Range(&quot;P&quot; &amp; x).Copy &#39;DIV 32 $
                Worksheets(wsF).Range(&quot;W&quot; &amp; j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If
        Next i
    End If
    Call ParaOn
    Call Happy
End Sub

答案1

得分: 2

去掉长时间重复的行

...通过使用变量和数据结构(例如数组、字典(集合))...

描述

  • 这段代码通过从工作簿中的其他工作表复制和粘贴数据来更新工作簿中的摘要工作表。

代码

<!-- 语言: lang-vb -->

Sub UpdateSummary()
    ' 项目,位置,持续时间,项目总计,杂项%,   杂项$,其他%,
    ' 其他$,总承包总计,总承包%,     总承包日,        总承包月,项目管理%,   项目管理小时,
    ' 管理员%, 管理员小时,PE%,     PE小时,        木工小时

    ' 定义常数。
    Dim sCells(): sCells = VBA.Array( _
        "S1", "J6", "Q1", "M1", "S10", "S11", "N10", _
        "N11", "P13", "K11", "L11", "M11", "S14", "K14", _
        "S16", "K16", "S18", "K18", "Q10")
    Dim dCols(): dCols = VBA.Array( _
        "B", "C", "D", "E", "F", "G", "H", _
        "I", "J", "K", "L", "M", "N", "O", _
        "P", "Q", "R", "S", "T")
    Const DST_FIRST_ROW As Long = 6

    ' ???
    Call ParaOff
    Call HideX
    Call SortWorksheets

    ' 引用工作簿。
    Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿

    ' 尝试引用目标工作表(而不是函数)。
    Dim dws As Worksheet
    On Error Resume Next
        Set dws = wb.Worksheets(wsF)
    On Error GoTo 0

    If dws Is Nothing Then

        MsgBox "错误:工作表 '" & wsF & "' 丢失。", vbCritical

    Else

        dws.Range("B7:U41", "W7:W41").ClearContents

        ' 将要排除的工作表的名称写入字典。
        Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
        dict.CompareMode = vbTextCompare
        ' 这些工作表名称应该在一个数组中。
        dict(wsF) = Empty
        dict(wsG) = Empty
        dict(wsI) = Empty
        dict(wsJ) = Empty
        dict(wsK) = Empty

        ' 将第一行写入变量。
        Dim dRow As Long: dRow = DST_FIRST_ROW

        Dim sws As Worksheet, sRow As Long, n As Long

        For Each sws In wb.Worksheets
            ' 检查它是否不是字典中的工作表。
            If Not dict.Exists(sws.Name) Then
                ' 普通情况。
                For n = 0 To UBound(sCells)
                    dws.Range(dRow, dCols(n)).Value = sws.Range(sCells(n)).Value
                Next n
                ' ??? 特殊情况。
                sRow = fFindRowByCol(sws.Name, "I", "Div 26*")
                dws.Cells(dRow, "U").Value = sws.Cells(sRow, "P").Value 'DIV 26
                sRow = fFindRowByCol(sws.Name, "I", "Div 32*")
                dws.Cells(dRow, "W").Value = sws.Cells(sRow, "P").Value 'DIV 32
                dRow = dRow + 1 ' 为下一次迭代(工作表)增加
            End If
        Next sws

    End If

    ' ???
    Call ParaOn
    Call Happy

    MsgBox "摘要已更新。", vbInformation

End Sub

流程

  • 该代码首先为要复制的单元格范围和列定义了常数,并清除了目标工作表的内容。
  • 然后,它创建了一个要从复制过程中排除的工作表名称字典,并循环遍历工作簿中的所有工作表,将指定单元格中的数据复制并粘贴到目标工作表上相应的列中,除了要排除的工作表。
  • 还有两种特殊情况,其中数据从源工作表的特定单元格复制到目标工作表上的特定列。
英文:

Get Rid of Long Repetitive Lines

... by using variables and data structures (e.g. arrays, dictionaries (collections))...

Description

  • This code updates a summary worksheet in a workbook by copying and pasting data from other worksheets in the workbook.

The Code

<!-- language: lang-vb -->

Sub UpdateSummary()
    &#39; PROJECT, LOCATION,  DURATION, PROJECT TOTAL, MISC %,   MISC $, OTHER %,
    &#39; OTHER $, GC TOTAL,  GC %,     GC DAY,        GC MONTH, PM %,   PM HRS,
    &#39; SUPER %, SUPER HRS, PE %,     PE HRS,        CARP HRS
    
    &#39; Define constants.
    Dim sCells(): sCells = VBA.Array( _
        &quot;S1&quot;, &quot;J6&quot;, &quot;Q1&quot;, &quot;M1&quot;, &quot;S10&quot;, &quot;S11&quot;, &quot;N10&quot;, _
        &quot;N11&quot;, &quot;P13&quot;, &quot;K11&quot;, &quot;L11&quot;, &quot;M11&quot;, &quot;S14&quot;, &quot;K14&quot;, _
        &quot;S16&quot;, &quot;K16&quot;, &quot;S18&quot;, &quot;K18&quot;, &quot;Q10&quot;)
    Dim dCols(): dCols = VBA.Array( _
        &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, _
        &quot;I&quot;, &quot;J&quot;, &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;, &quot;O&quot;, _
        &quot;P&quot;, &quot;Q&quot;, &quot;R&quot;, &quot;S&quot;, &quot;T&quot;)
    Const DST_FIRST_ROW As Long = 6
    
    &#39; ???
    Call ParaOff
    Call HideX
    Call SortWorksheets
    
    &#39; Reference the workbook.
    Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
    
    &#39; Attempt to reference the destination worksheet (instead of the function).
    Dim dws As Worksheet
    On Error Resume Next
        Set dws = wb.Worksheets(wsF)
    On Error GoTo 0
    
    If dws Is Nothing Then
        
        MsgBox &quot;ERROR: Worksheet &#39;&quot; &amp; wsF &amp; &quot;&#39; is missing.&quot;, vbCritical
    
    Else
        
        dws.Range(&quot;B7:U41&quot;, &quot;W7:W41&quot;).ClearContents
        
        &#39; Write the names of the worksheets to be excluded to a dictionary.
        Dim dict As Object: Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
        dict.CompareMode = vbTextCompare
        &#39; These worksheet names should be in an array.
        dict(wsF) = Empty
        dict(wsG) = Empty
        dict(wsI) = Empty
        dict(wsJ) = Empty
        dict(wsK) = Empty
        
        &#39; Write the first row to a variable.
        Dim dRow As Long: dRow = DST_FIRST_ROW
        
        Dim sws As Worksheet, sRow As Long, n As Long
        
        For Each sws In wb.Worksheets
            &#39; Check if it&#39;s not a worksheet from the dictionary.
            If Not dict.Exists(sws.Name) Then
                &#39; Normal case.
                For n = 0 To UBound(sCells)
                    dws.Range(dRow, dCols(n)).Value = sws.Range(sCells(n)).Value
                Next n
                &#39; ??? Special case.
                sRow = fFindRowByCol(sws.Name, &quot;I&quot;, &quot;Div 26*&quot;)
                dws.Cells(dRow, &quot;U&quot;).Value = sws.Cells(sRow, &quot;P&quot;).Value &#39;DIV 26
                sRow = fFindRowByCol(sws.Name, &quot;I&quot;, &quot;Div 32*&quot;)
                dws.Cells(dRow, &quot;W&quot;).Value = sws.Cells(sRow, &quot;P&quot;).Value &#39;DIV 32
                dRow = dRow + 1 &#39; increment for the next iteration (worksheet)
            End If
        Next sws
    
    End If
    
    &#39; ???
    Call ParaOn
    Call Happy

    MsgBox &quot;Summary updated.&quot;, vbInformation

End Sub

The Flow

  • The code first defines constants for the cell ranges and columns to be copied and clears the contents of the destination worksheet.
  • It then creates a dictionary of worksheet names to be excluded from the copying process, and loops through all worksheets in the workbook, copying and pasting the data from the specified cells into the corresponding columns on the destination worksheet, except for the excluded worksheets.
  • There are also two special cases where data is copied from specific cells on the source worksheet to specific columns on the destination worksheet.

答案2

得分: 0

OK,但解决方案或答案只是在评论中,没有复选标记可供选作答案。这里有太多规则!在这里发布我的问题并获得解决方案既非常有帮助,也是我的编码学习机会。

英文:

OK but the solution, or the answer was just in a comment, there was no checkmark to check as an answer. So many rules here! Posting my problem here and getting a solution was both incredibly helpful and a learning opportunity for my coding.

huangapple
  • 本文由 发表于 2023年4月4日 08:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定