Run-time error 91 when copying cells from one workbook to another

aa42

New Member
Joined
Dec 5, 2020
Messages
2
Office Version
  1. 2019
  2. 2013
  3. 2010
Platform
  1. Windows
Hi All

I have some code that I am trying to use to copy the contents of a sheet in a workbook to another workbook. However , when the code is run I get "Runtime error 91: Object variable or With block variable" not set error. I have pasted my code below. Any ideas what is causing the error. Thanks in advance.

VBA Code:
Sub Button1_Click()

Dim wb As Workbook
Dim wsDest As Worksheet
Dim wsSource As Worksheet
Dim srcValue As Range
Dim Dlg As FileDialog
Dim myFile As String

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)

With Dlg
    .Title = "Select the Dashboard"
    .AllowMultiSelect = False
    .Filters.Clear

    If .Show = -1 Then
        myFile = Dlg.SelectedItems(1)
    Else
        Exit Sub
    End If
End With

Set wb = Workbooks.Open(myFile)

Set wsDest = ThisWorkbook.Worksheets("Learner List")
Set wsSource = wb.Worksheets("Learner List")

srcValue = Range(wsSource.Cells(4, 2), wsSource.Cells(11, 11))

Range(wsDest.Cells(4, 2), wsDest.Cells(11, 11)) = srcValue

ActiveWorkbook.Close SaveChanges:=True


End Sub
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,071
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
Declare srcValue as variant instead of range.
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,071
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,851
Messages
5,598,452
Members
414,240
Latest member
xnanx

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
Top