Select Last visible Sheet

Dannyh1

Well-known Member
Joined
Nov 18, 2009
Messages
1,144
Hi

I have code to select last sheet of a workbook

Code:
 Sheets(Sheets.Count).Select
This code will error if sheets a hidden.

Could someone please advise how to adapt this to select last visible sheet in workbook?

Many Thanks
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,934
This is one way:

Code:
for i = sheets.count to 1 step -1
if sheets(i).visible = -1 then
     sheets(i).select
     exit sub
end if
next i
Hope that helps and this is untested.
 

Dannyh1

Well-known Member
Joined
Nov 18, 2009
Messages
1,144
Thanks schielrn

I think I am positioning this incorrectly in my code though

This is what I have:

Code:
 Dim rng As Range
 Set rng = Sheets("Files").Range("A1:A61")
 
 For Each cl In rng
 
 Workbooks.Open Filename:=cl.Value
 
    For i = Sheets.Count To 1 Step -1
    If Sheets(i).Visible = True Then
     Sheets(i).Select
     Exit Sub
    End If
    Next i
 Range("U1").Select
 ActiveCell.FormulaR1C1 = _
        "=MID(CELL(""filename"",RC[-20]),FIND(""]"",CELL(""filename"",RC[-20]))+1,256)"
 
 ActiveWorkbook.Save
 ActiveWorkbook.Close
 
 Next cl
When I run this, it stops before ActiveWorkbook.Save (i.e. only does it for one file)

Can you see where I have gone wrong with my code?

Thanks again
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,934
Maybe where I have Exit Sub change to Exit For. This way it doesn't exit out of your routine.

Hope that helps.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,446
Messages
5,511,398
Members
408,846
Latest member
terraf

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top