我想运行一个宏,该宏从XLS文件中读取所有工作表名称,然后能够从运行命令中运行该宏。这怎么可能?目前,我有以下代码,但我想将其推广到其他文件(基本上在此宏和命令中输入文件名作为参数)。
Sub FnGetSheetsName()
Dim mainworkBook As Workbook
Set mainworkBook = ActiveWorkbook
For i = 1 To mainworkBook.Sheets.Count
‘Either we can put all names in an array , here we are printing all the names in Sheet 2
mainworkBook.Sheets(“Sheet2”).Range(“A” & i) = mainworkBook.Sheets(i).Name
Next i
End Sub
您需要像这样将工作簿作为参数添加到子菜单中
Option Explicit
Sub FnGetSheetsName(ByRef mainworkBook As Workbook)
Dim i As Long
For i = 1 To mainworkBook.Sheets.Count
'Either we can put all names in an array , here we are printing all the names in Sheet 2
mainworkBook.Sheets("Sheet2").Range("A" & i) = mainworkBook.Sheets(i).Name
Next i
End Sub
Sub TestIt()
FnGetSheetsName ActiveWorkbook
End Sub