Why is my VBA code giving me an error.

helpneeded2

Board Regular
Joined
Jun 25, 2021
Messages
110
Office Version
  1. 365
Platform
  1. Windows
I was using the code below fine for 2 days, and now it is giving me an error. Any suggestions as to why?

The error I am getting is "Run-time error '1004': PasteSpecial method of Range class failed

VBA Code:
Sub Obtain_OLG_Patron_Data()
    Dim wbkSource As Workbook
    Dim endOfRows As Long
 
    Set wbkSource = ThisWorkbook
   
    ' Open, find last row of data, and copy data from OLG MASTER CSV File
    Workbooks.Open ("G:\Department\Division\Unit\Protected Document\OLG Master Lists\OLG MASTER TO DATE.xlsx")
    endOfRows = ActiveSheet.Range("A:A").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Range("A1").Resize(endOfRows, 42).Select
    Selection.Copy
   
    ' Paste Data into OLG Sheet
    wbkSource.Worksheets("OLG_MASTER").Range("A2").PasteSpecial Paste:=xlPasteValues
   
    ' Copy cell value to quickly clear clipboard
    wbkSource.Worksheets("OLG_MASTER").Range("A2").Copy
   
    ' Close OLG MASTER CSV File
    Workbooks("OLG MASTER TO DATE.xlsx").Close SaveChanges:=False
   
    ' Return focus to OLG Sheet, and cancel copy mode
    wbkSource.Worksheets("OLG_MASTER").Range("A2").Select
    Application.CutCopyMode = False

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Do you get an error if you change
VBA Code:
   Range("A1").Resize(endOfRows, 42).Select
    Selection.Copy
   
    ' Paste Data into OLG Sheet
    wbkSource.Worksheets("OLG_MASTER").Range("A2").PasteSpecial Paste:=xlPasteValues
    
    ' Copy cell value to quickly clear clipboard
    wbkSource.Worksheets("OLG_MASTER").Range("A2").Copy
to
VBA Code:
    With Range("A1").Resize(endOfRows, 42)

        wbkSource.Worksheets("OLG_MASTER").Range("A2").Resize(.Rows.Count, .Columns.Count).Value = .Value
    
    End With
 
Upvote 0
Solution
Thanks Mark, that does make it work.

I've very new to VBA and have no familiarity with the use of "with" statements, so now I have something new to research and learn. Thank you.
 
Upvote 0
You're welcome

A With statement is just a way to shorten/tidy code when you have the same object repeated (it does also mean Excel only has to evaluate it once).
If I didn't use the With statement then the code would have been


VBA Code:
wbkSource.Worksheets("OLG_MASTER").Range("A2").Resize(Range("A1").Resize(endOfRows, 42).Rows.Count, Range("A1").Resize(endOfRows, 42).Columns.Count).Value = Range("A1").Resize(endOfRows, 42) .Value
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,532
Members
449,316
Latest member
sravya

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