【问题标题】:Run a Macro on a different sheet using VBA (Excel)使用 VBA (Excel) 在不同的工作表上运行宏
【发布时间】:2016-05-04 06:21:52
【问题描述】:

我想制作一个具有不同宏按钮的工作表 此工作表名为 Buttons。此工作表中的宏按钮链接到应在不同工作表上运行的宏。我试图为工作表 1 制作一个宏按钮。库存和需求

Sub NeuerTag()

'Abfrage ob der Tag eingefügt werden soll, No = QUIT'
If MsgBox("Möchtest du die Tabelle vorbereiten?", vbYesNo) = vbNo Then Exit Sub

'Copies the last three coloumns of the Worksheet 1. Stock & Demand'
With Sheets("1. Stock & Demand")
Lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
Columns(Lastcol - 1).Resize(, 1).Select
Selection.Copy

'Selects the first empty cell in 1. Stock & Demand and pastes'
Sheets("1. Stock & Demand").Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste

'Pastes the Today()'
Sheets("1. Stock & Demand").Range("F3:ZZ3").End(xlToRight).Offset(-1, 0).Select
Selection.Value = Date

'Paste Special - Values'
With Sheets("1. Stock & Demand")
Lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
Columns(Lastcol - 3).Resize(, 1).PasteSpecial Paste:=xlPasteValues

End With
End With

End Sub

现在我有一个问题。每次我制作一个宏按钮并让它运行时,它只会在工作表 Buttons 中完成他的工作,而不是在我希望它工作的工作表中。

我不得不说我不太擅长编码,所以请像我五岁一样向我解释一下;-)。

【问题讨论】:

    标签: vba excel ms-office


    【解决方案1】:

    您必须为宏指定工作表名称。例如你可以试试这个:

     Workbooks("Your_worksheet_name_here").Sheets("1. Stock & Demand").Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste
    

    通常,为了清楚起见,我会这样做:

     Set targetSheet = Workbooks("Your_worksheet_name_here").Sheets("1. Stock & Demand")
     targetSheet.Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste
    

    【讨论】:

    • 我必须使用Workbooks("Your_worksheet_name_here")吗?这是同一个工作簿,但不同的工作表。 targetSheet.Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste 创建错误。
    • 我误解了你的问题,看看OldUgly的回复。我认为他关于代码中的点的观点是正确的。
    【解决方案2】:

    一些小问题 - 在您的 With 语句中,您编码错误。注意“.”。通过放错/省略它,您最终会在不正确的选项卡中得到结果。

    例如

    With Sheets("1. Stock & Demand")
        Lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
        Columns(Lastcol - 3).Resize(, 1).PasteSpecial Paste:=xlPasteValues
    End With
    

    应该是

    With Sheets("1. Stock & Demand")
        Lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        .Columns(Lastcol - 3).Resize(, 1).PasteSpecial Paste:=xlPasteValues
    End With
    

    还有这个……

    With Sheets("1. Stock & Demand")
        Lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
        Columns(Lastcol - 1).Resize(, 1).Select
        Selection.Copy
    
    'Selects the first empty cell in 1. Stock & Demand and pastes'
        Sheets("1. Stock & Demand").Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste
    
    'Pastes the Today()'
        Sheets("1. Stock & Demand").Range("F3:ZZ3").End(xlToRight).Offset(-1, 0).Select
        Selection.Value = Date
    

    应该是……

    With Sheets("1. Stock & Demand")
        Lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        .Columns(Lastcol - 1).Resize(, 1).Copy
    
    'Selects the first empty cell in 1. Stock & Demand and pastes'
        .Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste
    
    'Pastes the Today()'
        .Range("F3:ZZ3").End(xlToRight).Offset(-1, 0).Value = Date
    

    【讨论】:

    • 谢谢!但是现在'Selects the first empty cell in 1. Stock & Demand and pastes' .Range("F3:ZZ3").End(xlToRight).Offset(-2, 1).Paste 行给了我一个错误。
    • .Range("F3:ZZ3").End(xlToRight) 计算为单元格XFD3,即工作表中的最后一列。 .Offset(-2,1) 尝试向上两排,向右排一排 - 就在纸上。我不知道您要做什么 - 所以我无法告诉您解决方案。
    • 宏应该做但没有做的事情。 Visual Basic 向我显示一条黄线:/。 运行时错误“438”:对象不正确支持此方法。
    • 宏就是这样做的。您正在尝试粘贴到不存在的单元格,因此会引发错误。黄线只是告诉你错误在哪里。
    • 但是这个宏确实适用于1。库存和需求。但是当我让它从另一张纸上运行时不会。这让我很困惑。
    猜你喜欢
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多