[英]VBA Excel Macro to subtract cell from different sheet using VLOOKUP
[英]Run a Macro on a different sheet using VBA (Excel)
我想制作一个具有不同宏按钮的工作表。此工作表称为Buttons 。 此工作表中的宏按钮链接到应在不同工作表上运行的宏。 我试图为工作表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
现在我有一个问题。 每当我使一个宏按钮运行时,它只会在工作表Button中执行他的Job,而不是在工作表中工作。
我不得不说我不太擅长编码,所以请像我五;-)向我解释。
您必须为宏指定工作表名称。 例如,您可以尝试以下操作:
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
小事-在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.