0

I was running a simple VBA code as below:

Sub TransferData()

'transfer stuff from workbook 1 to workbook 2

    Dim strPath1 As String

    Dim strPath2 As String

    Dim wbkWorkbook1 As Workbook

    Dim wbkWorkbook2 As Workbook

    'define paths and filenames

    strPath1 = "C:\blp\data\grid1.xls"

    strPath2 = "Z:\24AM\Risk Managemen\Risk Management Processes.xlsm"

    'copy the values across

    wbkWorkbook2.Worksheets("FXDUMP").Range("A1:Z2000").Value = wbkWorkbook1.Worksheets("Book1").Range("A1:Z2000").Value

    'close the two workbooks

    wbkWorkbook1.Close (False)

    wbkWorkbook2.Close (True)
End Sub

But when I try to run the macro, I got this error message:

Object variable or with block variable not set

Can you please give me an input to figure out this issue?

Thank in advance.

2
  • You haven't assigned anything to either workbook variable. Commented Oct 27, 2015 at 8:49
  • Your workbook variables haven't been assigned... Commented Oct 27, 2015 at 8:49

1 Answer 1

1

Include the below statements in your code, It will open the excel sheet and assign the variables to excel sheet

Set wbkWorkbook1 = Workbooks.Open(Filename:=strPath1)
Set wbkWorkbook2 = Workbooks.Open(Filename:=strPath2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.