Excel/Copy

Advertisement


Question
Hello

I am trying to put this following macro, which works fine and does what i want, but i want to put it in a cell, doubleclick that even though its protect and run the macro

can u tell me what i am doing wrong>

this is the code that works as a macro

Sub ACopyCustomer()
   Workbooks.Open FileName:= _
   "C:\Users\Administrator\Dropbox\ES Software\Book2.xlsm"
   ActiveSheet.Unprotect
   Windows("Book1.xlsm").Activate
   Range("a10:G10").Select
   Selection.Copy
   Windows("Book2.xlsm").Activate
   NextRow = Sheets(1).Range("A65536").End(xlUp).Row + 1
   Cells(NextRow, 1).PasteSpecial
   ActiveSheet.Cells(NextRow, 1).PasteSpecial
   ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
   ActiveWorkbook.Save
   ActiveWindow.Close
End Sub

here is the code i have placed on the sheet for H7 and it keeps going back to highlighting
the row which is the next week, see below

VBA


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Not Intersect(Target, Range("h7")) Is Nothing Then
   Workbooks.Open FileName:= _
   "C:\Users\Administrator\Dropbox\ES Software\Book2.xlsm"
   ActiveSheet.Unprotect
   Windows("Book1.xlsm").Activate
   Range("a10:G10").Select
   Selection.Copy
   Windows("Book2.xlsm").Activate
   NextRow = Sheets(1).Range("A65536").End(xlUp).Row + 1
   Cells(NextRow, 1).PasteSpecial
   ActiveSheet.Cells(NextRow, 1).PasteSpecial
   ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
   ActiveWorkbook.Save
   ActiveWindow.Close
   End If
End Sub

Answer
Anthony,

I believe your primary problem is that when you have code in a sheet module - which you must do to use the BeforeDoubleClick event, then unqualified range references refer to the sheet associated with the code Module

Assume I have code in the Sheet2 code module
Assume sheet1 is the activesheet.

In a general module (say in a module like Module1), then

Range("A1") = 3

would be equivalent to ActiveSheet.Range("A1") = 3

but if you have that same code in an event procedure in the Sheet2 code module, then'

Range("A1") = 3  

is equivalent to
Worksheets("Sheet2").Range("A1") = 3   - not the activesheet.

So I believe this is at the root of your problem.  I have put in object references so I can fully qualify your references:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Dim sh2 As Worksheet, sh1 As Worksheet
   Dim bk2 As Workbook, bk1 As Worksheet
   If Not Intersect(Target, Range("h7")) Is Nothing Then
   Set bk1 = Workbooks("Book1.xlsm")
   bk1.Activate
   Set sh1 = b1.ActiveSheet
   Set r1 = sh1.Range("A10:G10")
   Workbooks.Open Filename:= _
   "C:\Users\Administrator\Dropbox\ES Software\Book2.xlsm"
   Set bk2 = ActiveWorkbook
   Set sh2 = bk2.Worksheets(1)
   sh2.Unprotect
   NextRow = sh2.Range("A65536").End(xlUp).Row + 1
   sh1.Range("a10:G10").Copy
   sh2.Cells(NextRow, 1).PasteSpecial xlValues
   sh2.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
   bk2.Close SaveChanges:=True
   Set bk2 = Nothing
   End If
End Sub


I believe that will do what you want - if not, it should at least point the way (I could have misinterpreted what your code was intended to do, but I think I have captured it).

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.