Using Column Numbers to define Range (VBA)

NBVC

Well-known Member
Joined
Aug 31, 2005
Messages
5,828
Hi again,

How can I replace

Code:
.Columns("A:AI").EntireColumn.Hidden = True
within this code with numbered columns?

Code:
Set WBook = ActiveWorkbook

For Each WSheet In WBook.Worksheets
    With WSheet
       ' autofit column widths
       .Columns("A:AI").EntireColumn.AutoFit
       .Rows("2:" & LastRow & "").RowHeight = 17
    End With
Next

Tried this but doesn't work.

Code:
.Range(Columns(1), Columns(LastCol)).EntireColumn.Hidden = True
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
Range(Columns(1), Columns(LastCol)).EntireColumn.Hidden = True

This should work, works for me at any rate. What is LastCol returning? An integer? What's it doing? Are you getting an error?

Regards
Jon
 
Upvote 0
Hi,

some possibilities
Code:
Sub test()
Dim a As Integer
Dim z As Integer

a = 3
z = 5

Columns(a).Resize(, z - a + 1).Hidden = True
'or
Range(Cells(1, a), Cells(1, z)).EntireColumn.Hidden = True
'or
Range(Columns(a), Columns(z)).Hidden = True
End Sub
kind regards,
Erik
 
Upvote 0
Code:
Range(Columns(1), Columns(LastCol)).EntireColumn.Hidden = True

This should work, works for me at any rate. What is LastCol returning? An integer? What's it doing? Are you getting an error?

Regards
Jon

But, my dear Baron, only on the ActiveSheet as written!!
 
Upvote 0
So in other words you need to activate the sheet first?
Code:
For Each wsheet In WBook.Worksheets
    wsheet.Activate

EDIT: BFG, 3838 posts already? Do you actually work for a living? :LOL:
 
Upvote 0
sorry, NBVC,

I missed the meaning of your question

this will work
Code:
       .Range(.Columns(1), .Columns(LastCol)).EntireColumn.Hidden = True
check the DOTS !!!
 
Upvote 0
sorry, NBVC,

I missed the meaning of your question

this will work
Code:
       .Range(.Columns(1), .Columns(LastCol)).EntireColumn.Hidden = True
check the DOTS !!!

Those, dang DOTS!!

Thanks, Erik, very much.
 
Upvote 0

Forum statistics

Threads
1,213,539
Messages
6,114,221
Members
448,554
Latest member
Gleisner2

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