Establishing a column number from a variable range

Red over White

Board Regular
Joined
Jul 16, 2011
Messages
123
Office Version
  1. 365
Platform
  1. MacOS
I'm hoping someone will be able to help me with the following code

Sub HideCols()

Sheets("Sheet1").Activate

Dim Int1 As Integer
With Sheets("Sheet1")
.Columns("L:CR").EntireColumn.Hidden = False
For Int1 = 12 To 96
.Columns(Int1).Hidden = Cells(3, Int1).Value <> 0
Next Int1
End With

Range(Cells(2, 47), Cells(2, 96)).SpecialCells(xlCellTypeBlanks)(1).Select

' Hide columns that aren't required

Dim rng As Range
Dim Col1 As Integer
Col1 = ActiveSheet.Columns(ActiveCell.Column).Rows(1)

' set rng to refer to required range

Set rng = Range(3 & ":" & Col1 - 1)

rng.Select
Selection.EntireColumn.Hidden = True

End Sub

I'm assuming the issue is surrounding Col1, in that it will be in the "A1" format, and should be a number. However, I don't know how to define Col1 as the identified column as a number.

Thanks
 
See if this does what you want:
VBA Code:
Sub MyHideCols()

    Dim c As Long

'   Find last column to hide
'   Check first to see if column N is blank in row 2
    If Cells(2, 14) = "" Then
'       If so, make column M the last column to hide
        c = 13
    Else
'       Otherwise, find the last populated column before the first blank column
'       after column M in row 2
        c = Cells(2, 13).End(xlToRight).Column
    End If
    
'   Hide columns 3 to last column above
    Range(Cells(2, 3), Cells(2, c)).EntireColumn.Hidden = True
    
End Sub
 
Upvote 0
Solution

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Joe4

Thanks that's worked, and for your forebearance. I just had to add an extra line to finish it off on the actual file rather than the one I shared

Sub MyHideCols()

Dim c As Long

' Range(Cells(2, 47), Cells(2, 96)).SpecialCells(xlCellTypeBlanks)(1).Select
' Find last column to hide
' Check first to see if column N is blank in row 2
' Replaced 14 with 47 and 46 with 13

If Cells(2, 47) = "" Then
' If so, make column M the last column to hide
c = 46
Else
' Otherwise, find the last populated column before the first blank column
' after column M in row 2

c = Cells(2, 46).End(xlToRight).Column
End If

' Hide columns 3 to last column above
Range(Cells(2, 3), Cells(2, c)).EntireColumn.Hidden = True

Range(Cells(2, 47), Cells(2, 96)).SpecialCells(xlCellTypeBlanks)(1).Select

End Sub

I have learnt two things, the solution you have provided and a lot more preparation work is required before posting and perhaps provide an example
 
Upvote 0
You are welcome. Glad to hear that it worked for you.

Yes, the thing to remember is that while the question is very familiar to you, all that we know about it is what you share with us. Posting examples really helps in explaining and showing what you want. Once I saw your example, then it made sense to me what you were after.
 
Upvote 0

Forum statistics

Threads
1,214,878
Messages
6,122,062
Members
449,064
Latest member
scottdog129

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