将粘贴工作表名称复制到另一个工作簿

Copy Paste Worksheets names to another workbook

我无法执行此操作。当我启动宏时,它不会产生任何错误,但也不会抛出任何结果。

Sub EmpresasCubiertas()


Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set x = Workbooks.Open("G:\Estudios\Biblioteca\Mercado Accionario Chileno\InsertarEmpresa.xlsm")
Set y = Workbooks.Open("G:\Estudios\Biblioteca\Mercado Accionario Chileno\Procesamiento.xlsm")

For i = 3 To x.Sheets.Count

'Now, paste to y worksheet:

y.Sheets(1).Range(“A” & i) = x.Sheets(i).Name

Next i


End Sub

请注意,工作簿 "y" 是我首先打开 运行 宏的工作簿。即它已经打开,如果它有任何帮助。

问题在于循环中范围旁边缺少值 属性。它应该是,

y.Sheets(1).Range(“A” & i).Value = x.Sheets(i).Name

我假设您故意选择从 i = 3 开始复制工作表名称。如果您需要所有工作表,只需将 i 的值更改为 1。

试试这个:

Sub CopyWorkBookNames()
Application.ScreenUpdating = False 'To avoid screen flickering

Dim y As Workbook
Dim x As Workbook

Set y = Application.ActiveWorkbook
Set x = Application.Workbooks.Open("FilePathToCopyFrom.xlsx")

'Copy and paste worksheet names, in the workbook running the code, starting from cell A3.
'If You want to paste into cell A1, decrement i in the range below accordingly

For i = 3 To x.Sheets.Count
    y.ActiveSheet.Range("A" & i).Value = x.Sheets(i).Name
Next i

x.Close  'Add SaveChanges Options as needed
Application.ScreenUpdating = True
End Sub