user18792001
user18792001

Reputation:

Copy sheet name to row with VBA

I need to copy the worksheets name into Row 1 of another worksheet of the same file. Here is what I have/need:

Can you help me? :)

Upvotes: 1

Views: 850

Answers (2)

VBasic2008
VBasic2008

Reputation: 54807

List Worksheet Names in a Row

Sub ListNames()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim dCell As Range: Set dCell = wb.Worksheets(2).Range("B1")
    
    Dim n As Long
    For n = 3 To wb.Worksheets.Count
        dCell.Value = wb.Worksheets(n).Name
        Set dCell = dCell.Offset(, 1)
    Next n
    
End Sub

Upvotes: 3

Bruno Bieri
Bruno Bieri

Reputation: 10236

From what you write it's really hard to tell what you actually need.

The following VBA code writes the name of the 3rd worksheet to the B1 cell of sheet 2. If you don't have 3 sheet's nothing happens:

Sub WriteTheNameOfWorksheet3IntoWorksheet2CellB1()
   If ActiveWorkbook.Worksheets.Count >= 3 Then
      ActiveWorkbook.Worksheets(2).Range("B1") = ActiveWorkbook.Worksheets(3).Name
   End If
End Sub

Upvotes: 1

Related Questions