object variable or with block variable not set

murthyspd

Board Regular
Joined
Dec 7, 2006
Messages
50
Hi
I am trying to open an excel sheet from MS Access and using UNION to copy some columns. Here is the code


Dim App As Excel.Application
Dim rng As Excel.Range

Set App = New Excel.Application
App.Visible = True

App.Workbooks.Open CurrentProject.Path & "\Oracle_11i_Access_Request_Approvers.xlsx"

App.Workbooks.Open CurrentProject.Path & "\Approval_Details.xlsx"

Set rng = App.Union(App.ActiveSheet.Range("A:A"), App.ActiveSheet.Range("F:F"))

It gives me the following error in the last line
"Object variable or With block variable not set"

Regards
Murthy
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Murthy

Does it work if you purposely restrict the ranges eg to:

Set rng = App.Union(App.ActiveSheet.Range("A1:A100"), App.ActiveSheet.Range("F1:F100"))
 
Upvote 0
I think Union only works with SQL which is probably why it throws the error as it thinks it is looking at a variable

If you want to combine Excel ranges in a VBA statement it is simply Range("A:A,F:F,Z:Z").Select or whatever:biggrin:
 
Upvote 0
Hey Richard
Your suggestion worked. I put specific rows - range("A1:A10"). It worked. I changed it backed to range("A:A"). That worked too..

Not sure why that happened. Any idea.

Thanks for the assistance.

Regards
Murthy
 
Upvote 0
Murthy

Why are you using Union anyway?

Where are you copying the columns to?
 
Upvote 0
I need to copy 5 columns from one workbook to another. I could have done one at a time. Just wanted to use UNION
 
Upvote 0
Code:
Set rng = App.ActiveSheet.Range("A:F")
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

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