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?
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
Jean-Pierre Oosthuizen is correct. adding commas to your ranges will fix the 1004 issue for you. 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. As @Jean-PierreOosthuizen said , you want .Range("B3")
.
Also unless you have a WorkSheet named "(7b)" (with the parens "()" included), your reference to Worksheets("(7b)")
will fail next.
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:
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("(7b)").Activate
- Same as above Else: MsgBox ("No Data")
and replace it with two lines of code - it's much more readable. * 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.