Sub abc()
Dim lrow As Long
Dim ws As Worksheet
Set ws = ActivsheetWorksheets("Completed")
Dim strFormulas(1 To 3) As Variant
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
With ws
.Range("Y2:Z" & lrow).FillDown
End With
With ThisWorkbook.Sheets("Completed")
strFormulas(1) = "=(N2-A2)+(R2-O2)+(V2-S2)"
strFormulas(2) = "=IFERROR(Y2/L2,Y2)"
.Range("Y2:Z2").Formula = strFormulas
End With
End Sub
Sub bcd()
Dim lrow As Long
Dim ws As Worksheet
Set ws = Worksheets("Follow-up")
Dim strFormulas(1 To 3) As Variant
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
With ws
.Range("Y2:Z" & lrow).FillDown
End With
With ThisWorkbook.Sheets("Follow-up")
strFormulas(1) = "=(N2-A2)+(R2-O2)+(V2-S2)"
strFormulas(2) = "=IFERROR(Y2/L2,Y2)"
.Range("Y2:Z2").Formula = strFormulas
End With
End Sub
上述代码可以简化为一个。它工作正常,但我需要简化代码,因为我必须在UserForm中调用它。在此先感谢。
答案 0 :(得分:0)
如果只需将代码移动到一个代码中,您就可以创建不同的变量。虽然我没有看到这个的任何主要优点。
Sub oneSub()
Dim completeRow As Long, followRow As Long
Dim wsComplete As Worksheet, wsFollow As Worksheet
Set wsComplete = Worksheets("Completed")
Set wsFollow = Worksheets("Follow-up")
Dim strFormulas(1 To 3) As Variant
completeRow = wsComplete.Cells(Rows.Count, 1).End(xlUp).Row
followRow = wsFollow.Cells(Rows.Count, 1).End(xlUp).Row
wsComplete.Range("Y2:Z" & completeRow).FillDown
wsFollow.Range("Y2:Z" & followRow).FillDown
strFormulas(1) = "=(N2-A2)+(R2-O2)+(V2-S2)"
strFormulas(2) = "=IFERROR(Y2/L2,Y2)"
ThisWorkbook.Sheets("Completed").Range("Y2:Z2").Formula = strFormulas
ThisWorkbook.Sheets("Follow-up").Range("Y2:Z2").Formula = strFormulas
End Sub
IMVHO致电Sub ABC
然后致电Sub BCD
,与Sub oneSub
相同。差别不大