VBA Formula

%3CLINGO-SUB%20id%3D%22lingo-sub-1930755%22%20slang%3D%22en-US%22%3EVBA%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1930755%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20have%20a%20spreadsheet%20that%20uses%20VBA%20to%20hide%20rows%20upon%20opening.%20I%20would%20like%20the%20workbook%20to%20open%20on%20the%20first%20tab.%20I%20have%20seen%20the%20formula%20to%20use%20for%20this%3A%3C%2FP%3E%3CDIV%20class%3D%22line%20number1%20index0%20alt2%22%3EPrivate%20Sub%20Workbook_Open()%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number2%20index1%20alt1%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number3%20index2%20alt2%22%3EWorksheets(%22Example%22).Activate%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number4%20index3%20alt1%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number5%20index4%20alt2%22%3EEnd%20Sub%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number5%20index4%20alt2%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number5%20index4%20alt2%22%3EHowever%20as%20my%20workbook%20page%20already%20uses%20a%20formula%20to%20collapse%20%2F%20hide%20rows%20in%20other%20sheet%20upon%20opening%20I'm%20not%20sure%20where%20to%20fit%20this%20in%20to%20my%20set%20of%20instructions.%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number5%20index4%20alt2%22%3EThese%20are%20the%20instruction%20I%20have%20and%20I%20have%20put%20the%20new%20one%20at%20the%20top%20but%20it%20doesn't%20work.%20Please%20help!%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number5%20index4%20alt2%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22line%20number5%20index4%20alt2%22%3E%3CP%3EPrivate%20Sub%20Workbook_Open()%3C%2FP%3E%3CP%3EWorksheets(%22General%20Instructions%22).Activate%3C%2FP%3E%3CP%3ESheets(%22Americas%22).Activate%3CBR%20%2F%3ERows(%228%3A18%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2221%3A30%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2233%3A42%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2245%3A49%22).EntireRow.Hidden%20%3D%20True%3C%2FP%3E%3CP%3ESheets(%22ANZASAF%22).Activate%3CBR%20%2F%3ERows(%228%3A18%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2221%3A24%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2227%3A36%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2239%3A45%22).EntireRow.Hidden%20%3D%20True%3C%2FP%3E%3CP%3ESheets(%22CEU%22).Activate%3CBR%20%2F%3ERows(%229%3A18%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2221%3A30%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2233%3A42%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2245%3A50%22).EntireRow.Hidden%20%3D%20True%3C%2FP%3E%3CP%3ESheets(%22GB%22).Activate%3CBR%20%2F%3ERows(%228%3A16%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2219%3A27%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2230%3A39%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3ERows(%2242%3A47%22).EntireRow.Hidden%20%3D%20True%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1930755%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1930822%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1930822%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F714088%22%20target%3D%22_blank%22%3E%40North_Yorks%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EPrivate%20Sub%20Workbook_Open()%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EWorksheets(%22General%20Instructions%22).Activate%3C%2FP%3E%3CP%3EWith%20Sheets(%22Americas%22)%3CBR%20%2F%3E.Rows(%228%3A18%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2221%3A30%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2233%3A42%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2245%3A49%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3EEnd%20With%3C%2FP%3E%3CP%3EWith%20Sheets(%22ANZASAF%22)%3CBR%20%2F%3E.Rows(%228%3A18%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2221%3A24%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2227%3A36%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2239%3A45%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3EEnd%20With%3C%2FP%3E%3CP%3EWith%20Sheets(%22CEU%22)%3CBR%20%2F%3E.Rows(%229%3A18%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2221%3A30%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2233%3A42%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2245%3A50%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3EEnd%20With%3C%2FP%3E%3CP%3EWith%20Sheets(%22GB%22)%3CBR%20%2F%3E.Rows(%228%3A16%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2219%3A27%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2230%3A39%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3E.Rows(%2242%3A47%22).EntireRow.Hidden%20%3D%20True%3CBR%20%2F%3EEnd%20With%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1930850%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1930850%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F234290%22%20target%3D%22_blank%22%3E%40Craig%20Hatmaker%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20response%20i%20have%20added%20the%20End%20With%20that%20I%20was%20missing%20and%20I%20get%20this%20error%20message%20when%20I%20open%20the%20file%20now%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22North_Yorks_0-1606312833521.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F235991iA503B30F3011BBC6%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22North_Yorks_0-1606312833521.png%22%20alt%3D%22North_Yorks_0-1606312833521.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi, I have a spreadsheet that uses VBA to hide rows upon opening. I would like the workbook to open on the first tab. I have seen the formula to use for this:

Private Sub Workbook_Open()
 
Worksheets("Example").Activate
 
End Sub
 
However as my workbook page already uses a formula to collapse / hide rows in other sheet upon opening I'm not sure where to fit this in to my set of instructions.
These are the instruction I have and I have put the new one at the top but it doesn't work. Please help!
 

Private Sub Workbook_Open()

Worksheets("General Instructions").Activate

Sheets("Americas").Activate
Rows("8:18").EntireRow.Hidden = True
Rows("21:30").EntireRow.Hidden = True
Rows("33:42").EntireRow.Hidden = True
Rows("45:49").EntireRow.Hidden = True

Sheets("ANZASAF").Activate
Rows("8:18").EntireRow.Hidden = True
Rows("21:24").EntireRow.Hidden = True
Rows("27:36").EntireRow.Hidden = True
Rows("39:45").EntireRow.Hidden = True

Sheets("CEU").Activate
Rows("9:18").EntireRow.Hidden = True
Rows("21:30").EntireRow.Hidden = True
Rows("33:42").EntireRow.Hidden = True
Rows("45:50").EntireRow.Hidden = True

Sheets("GB").Activate
Rows("8:16").EntireRow.Hidden = True
Rows("19:27").EntireRow.Hidden = True
Rows("30:39").EntireRow.Hidden = True
Rows("42:47").EntireRow.Hidden = True

End Sub

5 Replies

@North_Yorks 

 

Private Sub Workbook_Open()

Worksheets("General Instructions").Activate

With Sheets("Americas")
.Rows("8:18").EntireRow.Hidden = True
.Rows("21:30").EntireRow.Hidden = True
.Rows("33:42").EntireRow.Hidden = True
.Rows("45:49").EntireRow.Hidden = True
End With

With Sheets("ANZASAF")
.Rows("8:18").EntireRow.Hidden = True
.Rows("21:24").EntireRow.Hidden = True
.Rows("27:36").EntireRow.Hidden = True
.Rows("39:45").EntireRow.Hidden = True
End With

With Sheets("CEU")
.Rows("9:18").EntireRow.Hidden = True
.Rows("21:30").EntireRow.Hidden = True
.Rows("33:42").EntireRow.Hidden = True
.Rows("45:50").EntireRow.Hidden = True
End With

With Sheets("GB")
.Rows("8:16").EntireRow.Hidden = True
.Rows("19:27").EntireRow.Hidden = True
.Rows("30:39").EntireRow.Hidden = True
.Rows("42:47").EntireRow.Hidden = True
End With

End Sub

@Craig Hatmaker 

Thank you for your response i have added the End With that I was missing and I get this error message when I open the file now

North_Yorks_0-1606312833521.png

 

@North_Yorks 
I'm not sure I understand what you mean by " added the End With that I was missing". Perhaps you are missing that instead of 

 

Sheets("Americas").Activate

 

I'm using 

 

With Sheets("Americas")

 

and where you have something like 

 

Rows("8:18").EntireRow.Hidden = True

 

I have added a period to the front so it is now:

 

.Rows("8:18").EntireRow.Hidden = True

 

I'm using an entirely different approach that happens to look very similar. You are trying to activate the sheet and hoping that the rows will reference the active sheet. I'm explicitly telling Excel to link the rows to the specific worksheet and I'm not activating each sheet to do it. By not activating each worksheet, your first activate remains the active worksheet.

Without the WITH statement the new code would look like this (which may make things clearer),

 

Sheets("Americas").Rows("8:18").EntireRow.Hidden = True
Sheets("Americas").Rows("21:30").EntireRow.Hidden = True
Sheets("Americas").Rows("33:42").EntireRow.Hidden = True
Sheets("Americas").Rows("45:49").EntireRow.Hidden = True

Sheets("ANZASAF").Rows("8:18").EntireRow.Hidden = True
Sheets("ANZASAF").Rows("21:24").EntireRow.Hidden = True
Sheets("ANZASAF").Rows("27:36").EntireRow.Hidden = True
Sheets("ANZASAF").Rows("39:45").EntireRow.Hidden = True

 

 

The WITH statement allows us to not duplicate the Sheets("sheet name") on every row.

@Craig Hatmaker apologies I see my formula was missing more than I thought.

I have now copied your original instructions and it has worked. Many thanks

@North_Yorks 

If you want to make the same changes on multiple sheets, a more succinct version might be

Option Explicit

Private Sub Workbook_Open()
    HideRows
End Sub

Sub HideRows()
Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "GeneralInstructions" Then
            ws.Rows("8:18").Hidden = True
            ws.Rows("21:30").Hidden = True
        End If
    Next
    Sheets("GeneralInstructions").Select
End Sub

It is the final (or in this case the only) Select that determines what you see when control is returned from the macro. 

[I didn't bother about EntireRow because the Row object is already the entire row]

Sub UnhideRows()
Dim ws As Worksheet
    For Each ws In Sheets
        ws.Rows.Hidden = False
    Next
End Sub

 

Hi @Craig Hatmaker  Good to see you around!  Do not hesitate to improve my programming style,  It has a long way to go!