How to copy all sheet names to a particular sheet

2019-08-19 05:48发布

I'm kinda new to stackoverflow platform and particularly to VBA, just making my first steps. So I have the following problem: There are several sheets in my workbook and I need to copy names of these sheets except name of one sheet to which I'm copying these names to. Names should be copied to a particular place, too. So far I came up with this:

Sub passport_combining()

Dim i As Worksheet

For Each i In ActiveWorkbook

If i.Name <> "Sheetname_I_dont_need_to_copy_to" Then

    Range("G").Value = Range("G" & Rows.Count).End(xlUp).Offset(1).Select.i.Name

    ActiveSheet.paste

End If    

Next i

End Sub

Code needs to loop through every sheet except the one I'm copying to, derive names from these sheets and copy it to the lastrow + 1 of column G of last sheet I understand technically what I need to do to come up with the solution but I got really stuck in a problem! I even tried to record a macro where I copied sheet names but unfortunately all VBA gives me is bunch of Select options which are advised to be avoided. Any advice would be helpful! Thanks!

标签: excel vba
3条回答
ら.Afraid
2楼-- · 2019-08-19 06:11

The below code import all sheets name in Sheet 1 column A.

Option Explicit

Sub Get_Sheets_Name()

    Dim ws As Worksheet
    Dim Lastrow As Long

    'Loop all sheets in this workbook
    For Each ws In ThisWorkbook.Worksheets

        With ThisWorkbook.Worksheets("Sheet1")
            'Find of Sheet1 & Column A lastrow
            Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

            'Import Sheet name in Sheet 1 & Column A
            .Cells(Lastrow + 1, 1).Value = ws.Name

        End With

    Next

End Sub
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-19 06:16

Here is one option:

With ThisWorkbook.Sheets("Summary")
    Set nextEntry = .Range("G4")
    For Each wsheet In ThisWorkbook.Sheets
        If wsheet.Name <> "Summary" Then
            nextEntry.Value = wsheet.Name
            Set nextEntry = nextEntry.Offset(1, 0)
        End If
    Next wsheet
End With

Where "Summary" is the name of sheet on which you want all the names and .Range("G4") is the starting place for data

Update: Below code will check for value in "Column G" before inserting Sheet Names

Ref: https://stackoverflow.com/a/12648557/9167318

With ThisWorkbook.Sheets("Summary")
    For Each wsheet In ThisWorkbook.Sheets
        If wsheet.Name <> "Summary" Then
            Set nextEntry = .Cells(.Rows.Count, "G").End(xlUp).Offset(1, 0)
            If IsError(Application.Match(wsheet.Name, .Range("G:G"), 0)) Then nextEntry.Value = wsheet.Name
        End If
    Next wsheet
End With
查看更多
等我变得足够好
4楼-- · 2019-08-19 06:24

In simple try following

Sub AllSheets()
Dim sht As Worksheet
Dim i As Long

    i = 1
    For Each sht In ThisWorkbook.Worksheets
        If sht.Name <> ActiveSheet.Name Then
            Cells(i, "G") = sht.Name
            i = i + 1
        End If
    Next sht

End Sub

Then adjust codes for your need.

查看更多
登录 后发表回答