在工作簿中合并工作表-字符数限制

时间:2018-09-18 23:15:49

标签: excel vba excel-vba

我正在使用以下代码在一张纸上添加来自不同工作簿的数据。我的文件名之一是33个字符,由于表名的字符数限制为30个,因此没有添加到我的合并工作表中。

如何修改以下代码以容纳组合工作簿中的第一张纸?

Sub GetSheets()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Dim WrdArray() As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

directory = Application.ActiveWorkbook.path & "\"

fileName = Dir(directory & "*.xlsx")

Do While fileName <> ""
    Workbooks.Open (directory & fileName)
        WrdArray() = Split(fileName, ".")
        For Each sheet In Workbooks(fileName).Worksheets
        Workbooks(fileName).ActiveSheet.Name = WrdArray(0)
            total = Workbooks("Template.xls").Worksheets.Count
            Workbooks(fileName).Worksheets(sheet.Name).Copy After:=Workbooks("Template.xls").Worksheets(total)

            GoTo exitFor:

        Next sheet

exitFor:
    Workbooks(fileName).Close
    fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:0)

使用此方法,当字符串长度大于30时,您只需捕获前30个字符即可。

If Len(Split(filename, ".")) > 30 Then
    WrdArray() = Left(filename, 30)
Else
    WrdArray() = Split(filename, ".")
End if