英文:
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 "ERROR: Worksheet '" & wsF & "' is missing."
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 'PROJECT
Worksheets(wsF).Range("B" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("J6").Copy 'LOCATION
Worksheets(wsF).Range("C" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("Q1").Copy 'DURATION
Worksheets(wsF).Range("D" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("M1").Copy 'PROJECT TOTAL
Worksheets(wsF).Range("E" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("S10").Copy 'MISC %
Worksheets(wsF).Range("F" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("S11").Copy 'MISC $
Worksheets(wsF).Range("G" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("N10").Copy 'OTHER %
Worksheets(wsF).Range("H" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("N11").Copy 'OTHER $
Worksheets(wsF).Range("I" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("P13").Copy 'GC TOTAL
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 DAY
Worksheets(wsF).Range("L" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("M11").Copy 'GC MONTH
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 HRS
Worksheets(wsF).Range("O" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("S16").Copy 'SUPER %
Worksheets(wsF).Range("P" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("K16").Copy 'SUPER HRS
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 HRS
Worksheets(wsF).Range("S" & j + i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets(i).Range("Q10").Copy 'CARP HRS
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
答案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()
' PROJECT, LOCATION, DURATION, PROJECT TOTAL, MISC %, MISC $, OTHER %,
' OTHER $, GC TOTAL, GC %, GC DAY, GC MONTH, PM %, PM HRS,
' SUPER %, SUPER HRS, PE %, PE HRS, CARP HRS
' Define constants.
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
' Reference the workbook.
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' 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 "ERROR: Worksheet '" & wsF & "' is missing.", vbCritical
Else
dws.Range("B7:U41", "W7:W41").ClearContents
' Write the names of the worksheets to be excluded to a dictionary.
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
' These worksheet names should be in an array.
dict(wsF) = Empty
dict(wsG) = Empty
dict(wsI) = Empty
dict(wsJ) = Empty
dict(wsK) = Empty
' 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
' Check if it's not a worksheet from the dictionary.
If Not dict.Exists(sws.Name) Then
' Normal case.
For n = 0 To UBound(sCells)
dws.Range(dRow, dCols(n)).Value = sws.Range(sCells(n)).Value
Next n
' ??? Special case.
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 ' increment for the next iteration (worksheet)
End If
Next sws
End If
' ???
Call ParaOn
Call Happy
MsgBox "Summary updated.", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论