Shortening VBA

CVDK

New Member
Joined
Jul 22, 2009
Messages
35
Hi
In pursuit of making code shorter with fewer lines etc. there's something I don't understand. It's propably easy to explain for a couple of guys in here.

Why does this work?

Code:
 Activeworkbook.Sheets("A").Range(Departments, Departments).Copy

While this does not work (I have to split the line below in two in order to make it work)?

Code:
 Activeworkbook.Sheets("A").Departments.Copy

I've defined/dimmed "Departments" as range.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Yes, but that only works if the workbook is active. I just wrote activeworkbook instead of the actual workbook name wich i gues was not the smartest thing to do.
 
Upvote 0
Yes, but that only works if the workbook is active.

No, the range refers to its own workbook.

For ex., I define a range:

Code:
Dim r As Range
 
Set r = Workbooks("Test.xlsm").Worksheets("Sheet1").Range("a1")

Now, when another workbook is active, if you use:

Code:
r.Copy

its the range in the workbook Test.xlsm that is copied. There's no confusion becase the range r refers to the range in Test.xlsm, it doesn't matter which workbook is active.
 
Upvote 0
No. This code assumes the workbook is open.

There are ways to get info from closed workbooks, but usually it's not worth it. It's easy to open the workbook, get the information and close it. You can use the Application.ScreenUpdating to False during the execution of the code to speed up the process.

If, anyway, you really want to get the info from a closed workbook, you can use the old ExecuteExcel4Macro() if it's just a couple of cells, or create a connection to the workbook and use ADO.

If you are not familiar with this, maybe this helps:

http://www.exceltip.com/st/Read_inf...orkbook_using_VBA_in_Microsoft_Excel/473.html
 
Upvote 0
The reason I ask is, on our machines, when I close a file (yes, EVERY friggin file) it asks me if everything is correct.

Sendkeys can't be used to close it automatically.
 
Upvote 0
If that's the problem open the files with ReadOnly to make sure you change nothing and when you want to close it use:

Code:
Application.DisplayAlerts = False
' close the workbook
Application.DisplayAlerts = True
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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