选择多个工作表,然后取消选择并进一步选择另一个工作表

问题描述 投票:0回答:1

我正在各种工作表上运行一个宏,看起来像

Sub Cost_Reconciliation()
'
' Cost_Reconciliation Macro
' This macro will reconciliate the Premium cost added to cost basis.

     Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Select
    Sheets("Sheet1").Activate
    Range("D69").Copy
    Range("D70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
         Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Select
    Sheets("Sheet1").Activate
    Range("D69").Copy
    Range("D70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
          Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Select
    Sheets("Sheet1").Activate
    Range("D69").Copy
    Range("D70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
          Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Select
    Sheets("Sheet1").Activate
    Range("D69").Copy
    Range("D70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
        Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Select
    Sheets("Sheet1").Activate
    Range("D69").Copy
    Range("D70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

        Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Select
    Sheets("Sheet1").Activate
    Range("D69").Copy
    Range("D70").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
End Sub

我多次运行相同的脚本,以达到每张工作表的范围(“D71”)中没有引用的误差增量。但我现在可以成功编写“直到”脚本 当我运行此代码时,它会正确完成计算,但会选择所有工作表。我宁愿取消选择所有这些并选择 输入表

我尝试将以下代码集成到最后,但它不起作用。

Sheets(Array("Sheet1", "Sheet2", _
        "Sheet3", "Sheet4", "Sheet5", _
        "Sheet6")).Deselect
    
Sheets("Inputs").Select.Activate
    Range("A1").Select

有人可以帮忙简化完整的脚本吗!

excel vba
1个回答
0
投票

没有任何选择:

Sub Cost_Reconciliation()
    
    Dim el, wb As Workbook, n As Long, arrSheets
    
    Set wb = ThisWorkbook
    arrSheets = Array("Sheet1", "Sheet2", "Sheet3", _
                      "Sheet4", "Sheet5", "Sheet6")
    
    For n = 1 To 6 'not clear why this is done 6 times?
        For Each el In arrSheets
            With wb.Worksheets(el)
                .Range("D70").Value = .Range("D69").Value
            End With
        Next el
    Next n
    
    wb.Worksheets("Input").Select
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.