Using VBA Macro +buttons to move back and forward through excel sheets, hidden sheets in sequence disrupting functionality -


i have pair of buttons ('continue' , 'back') move user forward , worksheets.

the forward button seems able move forward through visible sheets no trouble, skipping hidden sheets fine.

the button however, fails if there hidden sheet between active sheet , previous visible sheets.

i'm confused because code 2 practically identical:

forward: (works fine)

sub movenext() on error resume next sheets(activesheet.index + 1).activate if err.number <> 0 sheets(1).activate end sub 

back: (fails @ hidden sheet)

sub moveback() on error resume next sheets(activesheet.index - 1).activate if err.number <> 0 sheets(1).activate end sub 

each button bound subs above suitable.

interesting! looks if try activate hidden worksheet, defaults next highest index. that's why works going up, not going down. wrote routine checks whether there's lower visible sheet, , if so, moves it:

sub moveback() dim prevvisibleindex long  prevvisibleindex = activesheet.index thisworkbook             prevvisibleindex = application.worksheetfunction.max(prevvisibleindex - 1, 1)     loop until .worksheets(prevvisibleindex).visible = true or prevvisibleindex = 1     .worksheets(prevvisibleindex).activate end end sub 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -