Excel 将宏从XLS移动到Personal.xlsb时出现“重复名称”错误

Excel 将宏从XLS移动到Personal.xlsb时出现“重复名称”错误,excel,vba,Excel,Vba,因此,我在本地运行了这段代码,并使用了一个数据表,该数据表需要获取特定范围内的日期以及所有行,并将其放入一个新工作簿中进行处理。它在本地级别工作得非常好,我对它没有任何问题,但是当我将模块移动到PERSONAL.XLSB时,它会在下面的代码中内联列出一条错误消息,如果我修复了该问题,还会显示另一条错误消息。我的问题是如何创建它,以便在我拥有的每个电子表格中全局使用它,而不必复制和粘贴代码来正常工作 Option Explicit 'This subroutine prompts the use

因此,我在本地运行了这段代码,并使用了一个数据表,该数据表需要获取特定范围内的日期以及所有行,并将其放入一个新工作簿中进行处理。它在本地级别工作得非常好,我对它没有任何问题,但是当我将模块移动到PERSONAL.XLSB时,它会在下面的代码中内联列出一条错误消息,如果我修复了该问题,还会显示另一条错误消息。我的问题是如何创建它,以便在我拥有的每个电子表格中全局使用它,而不必复制和粘贴代码来正常工作

Option Explicit

'This subroutine prompts the user to select dates
Public Sub PromptUserForInputDates()

Dim strStart As String, strEnd As String, strPromptMessage As String

'Prompt the user to input the start date
strStart = InputBox("Please enter the start date")

'Validate the input string
If Not IsDate(strStart) Then
    strPromptMessage = "Oops! It looks like your entry is not a valid " & _
                       "date. Please retry with a valid date..."
    MsgBox strPromptMessage
    Exit Sub
End If

'Prompt the user to input the end date
strEnd = InputBox("Please enter the end date")

'Validate the input string
If Not IsDate(strStart) Then
    strPromptMessage = "Oops! It looks like your entry is not a valid " & _
                       "date. Please retry with a valid date..."
    MsgBox strPromptMessage
    Exit Sub
End If

'Call the next subroutine, which will do produce the output workbook
Call CreateSubsetWorkbook(strStart, strEnd)

End Sub

'This subroutine creates the new workbook based on input from the prompts
Public Sub CreateSubsetWorkbook(StartDate As String, EndDate As String)

Dim wbkOutput As Workbook
Dim wksOutput As Worksheet, wks As Worksheet
Dim lngLastRow As Long, lngLastCol As Long, lngDateCol As Long
Dim rngFull As Range, rngResult As Range, rngTarget As Range

'Set references up-front
lngDateCol = 1 '<~ we know dates are in column A
Set wbkOutput = Workbooks.Add

'Loop through each worksheet
For Each wks In ThisWorkbook.Worksheets
    With wks

        'Create a new worksheet in the output workbook
        Set wksOutput = wbkOutput.Sheets.Add
        wksOutput.Name = wks.Name

       '------> I receive the first error here:
       'Run-Time error '1004':
       'That name is already taken. Try a different One
       'If I change the = wks.Name = "Sheet1" it gives another error of:
       'Run-time erro '91':
       'Object variable or With block variable not set

        'Create a destination range on the new worksheet that we
        'will copy our filtered data to
        Set rngTarget = wksOutput.Cells(1, 1)

        'Identify the data range on this sheet for the autofilter step
        'by finding the last row and the last column
        lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                             SearchOrder:=xlByRows, _
                             SearchDirection:=xlPrevious).Row

      '---------->Error Message here for the 2nd Error message

        lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
                             SearchOrder:=xlByColumns, _
                             SearchDirection:=xlPrevious).Column
        Set rngFull = .Range(.Cells(1, 1), .Cells(lngLastRow, lngLastCol))

        'Apply a filter to the full range to get only rows that
        'are in between the input dates
        With rngFull
            .AutoFilter Field:=lngDateCol, _
                        Criteria1:=">=" & StartDate, _
                        Criteria2:="<=" & EndDate

            'Copy only the visible cells and paste to the
            'new worksheet in our output workbook
            Set rngResult = rngFull.SpecialCells(xlCellTypeVisible)
            rngResult.Copy Destination:=rngTarget
        End With

        'Clear the autofilter safely
        .AutoFilterMode = False
        If .FilterMode = True Then
            .ShowAllData
        End If
    End With
Next wks

'Let the user know our macro has finished!
MsgBox "Data transferred!"

End Sub
我还是VBA新手,所以请耐心听我说,我只是想了解为什么它在本地级别可以很好地工作,但是当我尝试在PERSONAL.XLSB中创建模块时,它开始给出错误并且无法工作。任何帮助都会很好

原创的 将此工作簿更改为Active工作簿。Per:

此工作簿将始终引用代码所在的工作簿

ActiveWorkbook将引用处于活动状态的工作簿

因此,当您将代码移动到PERSONAL.XLSB时,对该工作簿的所有引用突然指向PERSONAL.XLSB,而不是包含您的数据的工作簿。PERSONAL.XLSB当然没有您期望的输入工作表结构

编辑 还有一条皱纹!调用Workbooks.Add时,新工作簿将变为ActiveWorkbook。因此,您需要的是:

...
Dim wbkInput As Workbook   ' ### New
Dim wbkOutput As Workbook
... 
'Set references up-front
lngDateCol = 1 '<~ we know dates are in column A
set wbkInput = ActiveWorkbook ' ### New - **before** creating the new workbook
Set wbkOutput = Workbooks.Add

For Each wks in wbkInput.Worksheets  ' ### Don't use ActiveWorkbook from here on down
    ...

欢迎来到这个网站!查看和,了解有关提问的更多信息,这些问题将吸引高质量的答案。您可以选择包含更多信息。无论如何,在这种情况下,使用ActiveWorkbook而不是ThisWorkbook,您应该可以。“此工作簿是保存宏的工作簿。@cxw您可以将其作为问题的答案,而不仅仅是注释。@cxw我将此工作簿更改为“活动工作簿”,但在运行脚本时仍会弹出相同的错误。”循环浏览此工作簿中每个工作的每个工作表。活动工作簿中每个工作的工作表-->工作表。工作表这是您所指的内容,对吗?如果是这样,我仍然会收到相同的错误消息。ActiveWorkbook使您知道如何将脚本运行到您正在使用的活动工作表中。如果要将特定工作簿作为目标,请在WorkbookWorkbookName here.xls.Worksheets中的每个工作表中使用。我在代码中更改了此部分,“循环查看Active工作簿中每个工作的每个工作表。工作表中有工作表”在输出工作簿集wksOutput=wbkOutput.Sheets.Add wksOutput.Name=wks.Name中创建一个新工作表。这仍然会产生相同的错误。希望我能正确理解ActiveWorkbook背后的上下文