need some VBA code!

Ron99

Active Member
Joined
Feb 10, 2010
Messages
347
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I have a spreadsheet where I have 40 tabs, I would like a code which can extract all the names of this tab and put it in one sheet. Is this possible ?

Regards,
Ron.....
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,

Very simple version (I'm sure there'll be other ways):

Code:
Sub test()
    i = 1
    For Each sht In Sheets
        Cells(i, 1).Value = sht.Name
        i = i + 1
    Next sht
End Sub
 
Upvote 0
Here's another solution (perhaps it's needlesly complex...)

Code:
Sub ListWkshts()
Dim shts() As String
Dim sheetCount As Integer, i As Integer
Dim ws As Worksheet
Set ws = Worksheets.Add
ws.Name = "SheetNames"
sheetCount = ActiveWorkbook.Sheets.Count
i = 1
ReDim shts(1 To sheetCount)
For Each sheet In ActiveWorkbook.Sheets
    shts(i) = sheet.Name
    i = i + 1
Next sheet
ws.Range("a1").Resize(sheetCount) = _
    Application.WorksheetFunction.Transpose(shts)
End Sub

it will create a sheet called "SheetNames" and add the names there
 
Upvote 0
I'm not sure what do you mean with "names of this tab" but here's a couple of different things:
Code:
Sub AddSheetsWithNames()
'Adds new sheets and names them from the selected cells

Dim Rng As Range
Dim c As Range
Dim ws As Worksheet
Dim i As Integer
Dim WSName As String

Set Rng = Selection

For Each c In Rng
    If c.Value <> "" Then
        WSName = c.Value
        Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
        
        If WorksheetExists(WSName) = True Then 'The name already exists (needs the following function to work)
            i = 1
            Do While WorksheetExists(WSName & i) = True
                i = i + 1
            Loop
            WSName = WSName & i
        End If
        
        ws.Name = WSName
    End If
    
Next c

'Goes back to the original sheet:
Rng.Parent.Activate

End Sub

Function WorksheetExists(ByVal WorksheetName As String) As Boolean

On Error Resume Next
    WorksheetExists = (Sheets(WorksheetName).Name <> "")
On Error GoTo 0

End Function


Sub TabNames()
'Lists the sheet names under the active cell

Dim i As Integer

For i = 1 To Worksheets.Count
    ActiveCell.Offset(i).Value = Sheets(i).Name
Next i

End Sub

Sub NamedRangesOnTab()
'Lists the named ranges that refer to this sheet from the activecell on

Dim N As Name
Dim i As Integer

For Each N In ActiveWorkbook.Names
    If InStr(N, ActiveSheet.Name) > 0 Then
        ActiveCell.Offset(i).Value = N.Name
        ActiveCell.Offset(i, 1).Value = Replace(N.RefersTo, "=", "")
        i = i + 1
    End If
Next N
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

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