An issue with the object model?

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
I wanted to perform a selection of some columns on a worksheet and then do some other things with that selection. The worksheet in question isn't active.
To select the columns, I did the following:

Code:
  Sheets("ACertainSheet").Activate
    Columns("H:K").Select

This worked well, but I didn't really want to make the sheet active if I could avoid it, so I tried this

Code:
  Sheets("ACertainSheet").Columns("H:K").Select

It didn't work. I am sure that you object model gurus out there can see immediately what is wrong (or perhaps you have to have the sheet active in order to select columns). So, how to solve the problem. Grateful for any help.

Thanks

Bengt
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I wanted to perform a selection of some columns on a worksheet and then do some other things with that selection.

in VBA you don't need to select ranges in order to work with them
 
Upvote 0
What do you actually want to do with the columns ??
The sheet must be active to select a range / columns / rows
 
Upvote 0
in VBA you don't need to select ranges in order to work with them

Thanks, but if the columns that I want to work with is on another sheet than the one that is currently active, I guess I must be able to point to the columns to tell VBA what area I want to work with. If I want to do stuff with a set of columns, how do I specify on what sheet these columns are?

Bengt
 
Upvote 0
What do you actually want to do with the columns ??
The sheet must be active to select a range / columns / rows
I was just going to do an Autofit after having written values to them, but your second sentence actually gave me the information I needed, that the sheet must be active in order for me to select the columns. Thanks veru much for this insight.

Bengt
 
Upvote 0
you could use...
Code:
    Sheets("ACertainSheet").Columns("H:K").EntireColumn.AutoFit
 
Upvote 0
The key point is you don't need to select them at all:
Code:
Sheets("ACertainSheet").Columns("H:K").Autofit
for example, rather than:
Code:
Sheets("ACertainSheet").Activate
Columns("H:K").Select
Selection.EntireColumn.Autofit
 
Upvote 0
:LOL:...:LOL:
one step ahead of you Rory...for a change...(y)
 
Upvote 0
:)

(right, more coffee... ;))
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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