Random Excel Questions

Void

BAU BAU
<Gold Donor>
10,095
12,239
I used the googles to search for similar threads, but all I came up with were specific questions, so I figured I might as well make this one for anyone that has questions in the future. I'm trying to revamp a bunch of OLD excel sheets that we've been using at our company, but they need to be practically retard-proof. As such, I might have multiple different questions over time, and there is no sense making new threads for them since they are probably of little use to anyone else.

With that out of the way, my first question relates to worksheet tabs. I know how to return the names of individual tabs to a cell. I can put them in order based on how they are now, but occasionally someone may need to insert a new tab or rearrange them, and I want the name of that tab to self-insert in the correct order.

Let's say that I have Tab 10, Tab 20, Tab 30, etc.

On my summary page, I would have something like this:

Cell A - Tab 10
Cell B - Tab 20
Cell C - Tab 30
etc.

If someone inserts Tab 11 between 10 and 20, I need it to now look like:

Cell A - Tab 10
Cell B - Tab 11
Cell C - Tab 20
Cell D - Tab 30
etc.

I cannot count on someone modifying the formulas for the cells manually; I need to have it happen automatically. They are capable of inserting new tabs, but anything beyond that is not going to reliably happen.

Assume that Tab 10 is always the starting tab, so we have a base to start from. The only way I know how to refer to tabs is by referring to the tab name itself, or by being on the actual tab. What I need is some way to, for instance, designate Tab 10 as 1, and then populate 2, 3, 4, etc. automatically no matter how they are changed around. But I don't see anywhere that tabs are referred to sequentially like that.

Is this something that is possible? Even with a macro? Or am I just screwed?

Depending upon the answers to this question, I will likely have another about the user selecting specific tabs to batch print. But without this step I'll have to rethink my approach a little.

Thanks in advance.
 

Void

BAU BAU
<Gold Donor>
10,095
12,239
Of course, after posting my question I found the answer. Clearly I didn't come up with this, because I'm retarded, but I did finally find it through some creative googling. I'll give the solution on the off chance anyone finds it useful.

First, you create a User Defined Function like so:
Code:
Function SHEETNAME(number As Long) As String
        SHEETNAME = Sheets(number).Name
End Function
That returns the name of the sheet number you enter, which is what I was looking for. However, it doesn't update if you rename the tab or move them around. It wouldn't even update if I manually pressed the recalculate button, the only thing that would work was actually clicking in the formula box for that cell and hitting enter. So you add this in:
Code:
Function SHEETNAME(number As Long) As String
    Application.Volatile
    SHEETNAME = Sheets(number).Name
End Function
That causes it to flag that function as needing to be updated any time something happens. Downside to that is it might start to lag if the sheet is big enough, but hopefully that's not an issue.

It appears that this completely solves my problem. It allows you to rename tabs, insert tabs, drag them around, etc. and it always returns the names in the correct order. Amazing!
 
  • 2Like
Reactions: 1 users