简体   繁体   English

VBA Excel 2016-运行时错误'1004'

[英]VBA Excel 2016 - Run-time error '1004'

I am trying to write code to copy and paste data from one sheet to the master sheet in the same workbook based on 5 drop down lists (using data validation) on the master sheet and have written this code for it as below, however every time I get the run time 1004 error and don't know how to solve it, is there a way to fix this? 我正在尝试基于母版上的5个下拉列表(使用数据验证)将代码从一张纸复制和粘贴到同一工作簿中的母版的代码,并为此编写了以下代码,但是每次我收到运行时1004错误,不知道如何解决,有没有办法解决?

Private Sub GO_Click()
Worksheets("Dashboard").Select
If Worksheets("Dashboard").Range(B3) = "National Gallery" And 
Worksheets("Dashboard").Range(B4) = "unframed" And 
Worksheets("Dashboard").Range(B7) = "Product Costings" And 
Worksheets("Dashboard").Range(B5) = "N/A" And 
Worksheets("Dashboard").Range(B6) = "N/A" Then
Worksheets("(7b)").Activate
Worksheets("(7b)").Range(A8, F23).Copy_
Destination = Worksheets("Dashboard").Range(D11)

Else: MsgBox ("No Data")

End If

End Sub

after copy the line put this : 复制行后,将以下内容放入:

Worksheets("Dashboard").activate
range("D11").pasteSpecial xlpastevalues 'or just paste depends on your need

don't forget to put " inside range("x") hope it works 别忘了将“放在range(” x“)内希望它能起作用

Jean-Pierre Oosthuizen is correct. Jean-Pierre Oosthuizen是正确的。 adding commas to your ranges will fix the 1004 issue for you. 在范围内添加逗号将为您解决1004问题。 The below code should now work for you. 现在,以下代码将为您工作。

Private Sub GO_Click()
Worksheets("Dashboard").Activate
If Worksheets("Dashboard").Range("B3") = "National Gallery" And _
Worksheets("Dashboard").Range("B4") = "unframed" And _
Worksheets("Dashboard").Range("B7") = "Product Costings" And _
Worksheets("Dashboard").Range("B5") = "N/A" And _
Worksheets("Dashboard").Range("B6") = "N/A" Then
Worksheets("(7b)").Activate
Worksheets("(7b)").Range("A8", "F232").Copy_
Destination = Worksheets("Dashboard").Range("D11")

Else: MsgBox ("No Data")

End If

End Sub

Unless B3 , B4 , etc are global variables, you're asking for .Range(null) which isn't valid. 除非B3B4等是全局变量,否则您要求的是.Range(null)无效。 As @Jean-PierreOosthuizen said , you want .Range("B3") . 正如@ Jean-PierreOosthuizen所说 ,您需要.Range("B3")

Also unless you have a WorkSheet named "(7b)" (with the parens "()" included), your reference to Worksheets("(7b)") will fail next. 同样,除非您有一个名为 “(7b)”的Worksheets("(7b)")包括括号“()”),否则对Worksheets("(7b)")引用将接下来失败。


Bonus code review: 奖金代码审查:

Please work on proper indentation - it makes your code much more readable! 正确的缩进工作-它使你的代码更具可读性! Future you (like you a week from now) will thank present you for doing so. 未来的您(从现在开始一个星期便会如此)将感谢您的光临。 Rubberduck VBA * will do that for you as well as pointing out lots of other things that will make your code better, like: Rubberduck VBA *将为您做到这一点,并指出许多其他可以使您的代码更好的东西,例如:

  • Eliminate Worksheets("Dashboard").Select - You're doing an excellent job of explicitly specifying all your worksheet references, so you don't need to .Select one. 消除Worksheets("Dashboard").Select出色地完成了显式指定所有工作表引用的工作,因此您无需.Select一个。
  • Eliminate Worksheets("(7b)").Activate - Same as above 消除Worksheets("(7b)").Activate -与上面相同
  • Eliminate the multiline Else: MsgBox ("No Data") and replace it with two lines of code - it's much more readable. 消除多行Else: MsgBox ("No Data") ,并用两行代码替换它-更具可读性。

* I'm not yet a contributor to the Rubberduck project, but I'm a happy user and have learned quite a bit about better coding from them *我还不是Rubberduck项目的贡献者,但是我是一个快乐的用户,并且从他们那里学到了很多关于更好编码的知识

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

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