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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi & welcome to MrExcel.
Declare srcValue as variant instead of range.
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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