我使用此VBA从不同文件中导入了大量图纸名称:

Sub ImportSheets()

    Dim sPath As String
    Dim sFname As String
    Dim wBk As Workbook
    Dim wSht As Variant


    Application.EnableEvents = False
    Application.ScreenUpdating = False
    sPath = InputBox("Enter a full path to workbooks")
    ChDir sPath
    sFname = InputBox("Enter a filename pattern")
    sFname = Dir(sPath & "\" & sFname & ".xl*", vbNormal)
    wSht = InputBox("Enter a worksheet name to copy")
    Do Until sFname = ""
        Set wBk = Workbooks.Open(sFname)
        Windows(sFname).Activate
        Sheets(wSht).Copy After:=ThisWorkbook.Sheets(1)
       ActiveSheet.Name = ActiveSheet.Range("A9")
        wBk.Close False
        sFname = Dir()
    Loop
    ActiveWorkbook.Save
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub


现在,net sheetname是A9中写的任何值,有没有办法我可以更改它,因此工作表将重命名为其导入的文件名?
替代的解决方案是将其重命名为“导入”和后缀,但是我不确定如何添加后缀1-1000 ect。

最佳答案

您的工作簿名称为sFname。剥离扩展名并使用它。

ActiveSheet.Name = left(sFname, instrrev(sFname, chr(46))-1)

08-05 15:20