Excel VBA: Why does EntireColumn.Hidden work only towards end of my code

gertie193

Board Regular
Joined
Feb 28, 2008
Messages
82
Hello,

I'm using Excel 2013 and modified some code to help me filter a certain column, 7th column (the hub name), copy that filtered data, paste that to a new workbook, save it by the hub name being filtered, and format the new workbook.

The code works. I'm just wondering why, however, I had to move the line of code below from the beginning of the format section of the code to near the end for it to work. I guess this is a theory question.

Code:
 Range("A:B").EntireColumn.Hidden = True

Here is the full code, if needed:

Thank you.

Code:
Sub FilterHubCopyToWkbk()
'partially used https://www.pcreview.co.uk/threads/re-vba-question-canceling-a-save-as-command-from-a-user-form.976722/


    Dim rng As Range
    Dim LR As Long
    Dim inval As Variant
    Dim filesavename As Variant
        
    'LR is counting last row
    LR = Cells(Rows.Count, "H").End(xlUp).Row
    Set rng = Range("A3:AQ" & LR)
    inval = InputBox("Enter hub to create report.")
    
    If inval = vbNullString Then Exit Sub
        
        With rng
            .AutoFilter
            .AutoFilter field:=7, Criteria1:=inval
            .SpecialCells(xlCellTypeVisible).Copy
            Workbooks.Add
            ActiveWorkbook.ActiveSheet.Paste
            
            filesavename = Application.GetSaveAsFilename(InitialFileName:=inval & " 2017 Ticketing Schedule", _
                                                                            Filefilter:="Excel workbooks (*.xlsx), *xlsx, (*.xlsm), *.xlsm")
                                                              
            If Not filesavename = False Then
                'CStr converts different types of expressions (Integer, Boolean and Date) to a string
                ActiveWorkbook.SaveAs Filename:=CStr(filesavename)

                'FORMATTING SECTION----------------------

               [B] 'HERE IS WHERE I HAD HIDDEN COLUMNS BUT IT DID NOT WORK[/B]


                Range("A1").CurrentRegion.RowHeight = 15
                Columns.AutoFit
                Range("AD1").ColumnWidth = 50
                Range("AE1").ColumnWidth = 11.3
                Range("AJ1").ColumnWidth = 19.43
                Range("A1").AutoFilter


               [B] 'HERE HIDDEN COLUMNS WORKS[/B]
                Range("A:B").EntireColumn.Hidden = True
                
                ActiveSheet.Name = inval
                ActiveWindow.Zoom = 90
                Range("c1").Select
            Else
                'close False workbook added/created of filtered hub
                ActiveWorkbook.Close False
                Application.CutCopyMode = False
                ActiveSheet.ShowAllData
            End If
            
        End With


End Sub
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Because you had this line after it:
Code:
Columns.AutoFit
That tells Excel to make every column wide enough to display its content.
So that will unhide hidden columns.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,093
Messages
6,128,784
Members
449,468
Latest member
AGreen17

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