user5492688
user5492688

Reputation: 11

Object variable or with block variable not set - Error

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.

Upvotes: 0

Views: 691

Answers (1)

PASUMPON V N
PASUMPON V N

Reputation: 1186

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)

Upvotes: 1

Related Questions