Help on a copy/paste issue

judgejustin

Board Regular
Joined
Mar 3, 2014
Messages
139
I am running this code and everything works up to the first line where it's suppose to paste. The next couple of lines after the first paste line may not work either but I haven't gotten that far in testing. I have played around with everything I know and can't figure this out.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 14 Then Exit Sub
Dim FilteToOpen As Variant
Dim OpenBook As Workbook
sPath = ThisWorkbook.Path & "\"
Application.ScreenUpdating = False

Workbooks.Open Filename:=sPath & ActiveCell.Offset(, -11) & ".xlsm", UpdateLinks:=True




Sheets("AGR").Range("K11").Copy
ThisWorkbook("County Data File.xlsm").Worksheets("Statistics").Range(ActiveCell).PasteSpecial xlPasteValues
OpenBook.Sheets("AGR").Range("K9").Copy
ThisWorkbook.Worksheets("Statistics").Range(ActiveCell.Offset(, 1)).PasteSpecial xlPasteValues
OpenBook.Close True


Application.ScreenUpdating = True

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
ActiveCell is already a range so Range(ActiveCell) should just be ActiveCell, but if you are using the activecell why would you need the
VBA Code:
ThisWorkbook("County Data File.xlsm").Worksheets("Statistics")
part?
Can you please explain exactly where you are pasting and also let us know what workbook and worksheet are currently active please?
 
Upvote 0
You're probably looking for something like this ...
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim OpenBook    As Workbook
    Dim spath       As String
    
    If Target.Column <> 14 Then Exit Sub
    spath = ThisWorkbook.Path & "\"
    Application.ScreenUpdating = False
    
    Set OpenBook = Workbooks.Open(Filename:=spath & Target.Offset(, -11) & ".xlsm", UpdateLinks:=True)
    With OpenBook
        .Sheets("AGR").Range("K11").Copy
        ThisWorkbook.Worksheets("Statistics").Target.PasteSpecial xlPasteValues
        .Sheets("AGR").Range("K9").Copy
        ThisWorkbook.Worksheets("Statistics").Target.Offset(, 1).PasteSpecial xlPasteValues
        .Close True
    End With
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
It still will not paste starting at the first line of code.
Code:
ThisWorkbook.Worksheets("Statistics").Target.PasteSpecial xlPasteValues
It is doing everything up to that point.
I get a runtime error 438 "Object doesn't support this property or method"
 
Last edited:
Upvote 0
Was a bit too enthusiastic with the use of Find & Replace :eek:
Indeed, these two lines of code will never work.
Rich (BB code):
       ThisWorkbook.Worksheets("Statistics").Target.PasteSpecial xlPasteValues
       ThisWorkbook.Worksheets("Statistics").Target.Offset(, 1).PasteSpecial xlPasteValues

In both lines Target should be replaced by the destination Cell (eg like Range("K10")) in which you're pasting.
ThisWorkbook refers to the workbook in which this code is running, so worksheet "Statistics" must exist.
If it doesn't work for you, explain us exactly what you're trying to do.
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top