【问题标题】:Codes unable to execute VBA Excel无法执行 VBA Excel 的代码
【发布时间】:2015-05-16 19:30:21
【问题描述】:
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 中调用它。提前致谢。

【问题讨论】:

  • 你只想把它们放在一个子里吗?
  • Paul,代码似乎没有任何更改。你能告诉我具体的变化是什么吗..
  • 是的,保罗,我希望它在一个子之下。

标签: vba excel


【解决方案1】:

如果只需要将代码合二为一,您可以创建不同的变量。虽然我没有看到任何主要的优势。

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 相同。差别不大

【讨论】:

  • No Paul 它对我来说工作不正常......代码大部分时间都没有被复制到下一个单元格。感谢您的快速回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 2017-07-04
  • 2022-10-07
  • 2012-03-20
  • 2017-05-26
  • 1970-01-01
相关资源
最近更新 更多