Paste Special Not Working This Time

monkey_arms

New Member
Joined
Feb 23, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone!

I got some vba code from here to copy the same 2 cells from multiple workbooks and paste them in one workbook. It works, however I need to paste only the values, as the cells I'm copying from have formulas in them. I tried adding some different ".PasteSpecial..." at the end of the copy statement from snippets I found online, but I get a variety of compile errors depending on the snippets used. It seems so close to being done, but my mind is fried. o_O Help!

VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Users\monkey_arms\Desktop\Local Dev\EXCEL\SourceData\"
    ChDir strPath
    strExtension = Dir("*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            .Sheets("PWB").Range("D2").Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Sheets("PWB").Range("B6").Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Haven't got Excel at the moment, so UNTESTED !!
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Users\monkey_arms\Desktop\Local Dev\EXCEL\SourceData\"
    ChDir strPath
    strExtension = Dir("*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            .Sheets("PWB").Range("D2").Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            .Sheets("PWB").Range("B6").Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
With wkbDest
    .Cells(Rows.Count, "A").End(xlUp).Value = .Cells(Rows.Count, "A").End(xlUp).Value
    .Cells(Rows.Count, "B").End(xlUp).Value = .Cells(Rows.Count, "B").End(xlUp).Value
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Michael,

Thanks for replying. Unfortunately, that didn't work. The formulas and links are still coming over. :confused:
 
Upvote 0
Try then
VBA Code:
With wkbDest
    .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Value = .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Value
    .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value
End With
 
Upvote 0
Same result, however with an added "Run-time error code '91', Object variable or With block variable not set", and the debugger stops at the copy/paste statement in the loop.
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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