简体   繁体   English

使用VBA(Excel)在其他工作表上运行宏

[英]Run a Macro on a different sheet using VBA (Excel)

I want to make a sheet that has different macro-buttons This sheet is named Buttons . 我想制作一个具有不同宏按钮的工作表。此工作表称为Buttons The macro-buttons in this sheet are linked to macros that should run on different sheets. 此工作表中的宏按钮链接到应在不同工作表上运行的宏。 I tried to make a macro-button for the sheet 1. Stock & Demand : 我试图为工作表1设置一个宏按钮。Stock&Demand

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

Now I have a Problem. 现在我有一个问题。 Every time I make a macro-button and let it run it only does his Job in the sheet Buttons and not in the sheet I want it to work. 每当我使一个宏按钮运行时,它只会在工作表Button中执行他的Job,而不是在工作表中工作。

I have to say I'm not really good at coding so please explain it to me like im five ;-). 我不得不说我不太擅长编码,所以请像我五;-)向我解释。

You have to specify worksheetname for the macro. 您必须为宏指定工作表名称。 For example you can try this: 例如,您可以尝试以下操作:

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

Normally, to keep things clear i do something like this: 通常,为了使事情保持清晰,我会执行以下操作:

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

Something minor - inside your With statements, you are mis-coding. 小事-在With语句中,您对代码进行了错误编码。 Take notice of the ".". 注意“。”。 By misplacing / omitting it, you will end up with results in the incorrect tab. 通过放错/忽略它,您最终将在错误的选项卡中得到结果。

For example 例如

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

Should be 应该

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

and this ... 和这个 ...

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

should be ... 应该 ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM