简体   繁体   English

宏创建一个新的工作台,而不是添加工作表

[英]Macro creating a new workbork instead of adding a sheet

The following Macro was intended to get specific data for a date range. 下面的宏旨在获取日期范围内的特定数据。 While it does this, I wanted it displayed within the same workbook on another sheet, instead a new workbook is created. 在执行此操作时,我希望它显示在另一个工作表的同一工作簿中,而不是创建一个新的工作簿。 Any idea on how I can fix this? 关于如何解决此问题的任何想法吗?

Public Sub PromptUserForInputDates()

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

    strStart = InputBox("Please enter the start date")

    If Not IsDate(strStart) Then
        strPromptMessage = "Not Valid Date"

        MsgBox strPromptMessage

        Exit Sub
    End If

    strEnd = InputBox("Please enter the end date")

    If Not IsDate(strStart) Then
        strPromptMessage = "Not Valid Date"

        MsgBox strPromptMessage
        Exit Sub

   End If

   Call CreateSubsetWorkbook(strStart, strEnd)

   End Sub

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

    lngDateCol = 4
    Set wbkOutput = Workbooks.Add

    For Each wks In ThisWorkbook.Worksheets
        With wks

            Set wksOutput = wbkOutput.Sheets.Add
            wksOutput.Name = wks.Name

            Set rngTarget = wksOutput.Cells(1, 1)

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

            With rngFull
                .AutoFilter Field:=lngDateCol, _
                            Criteria1:=">=" & StartDate, _
                            Criteria2:="<=" & EndDate


                Set rngResult = rngFull.SpecialCells(xlCellTypeVisible)
                rngResult.Copy Destination:=rngTarget
            End With

            .AutoFilterMode = False
            If .FilterMode = True Then
                .ShowAllData

            End If
        End With
    Next wks


    MsgBox "Data Transferred!"

    End Sub

You're defining Set wbkOutput = Workbooks.Add which will always create a new workbook. 您正在定义Set wbkOutput = Workbooks.Add ,它将始终创建一个新的工作簿。 Instead, Set wbkOutput = the workbook where you want the output to be. 而是, Set wbkOutput =您要在其中输出的工作簿。

Note that your assignment of wksOutput.Name = wks.Name will fail (two worksheets cannot have same name), so I've commented it out for now and you can revise that statement as needed. 请注意,您分配的wksOutput.Name = wks.Name将失败(两个工作表不能具有相同的名称),因此我wksOutput.Name = wks.Name将其注释掉,您可以根据需要修改该语句。

Replace all references to wbkOutput with ThisWorkbook ThisWorkbook替换所有对wbkOutput引用

Public Sub CreateSubsetWorkbook(StartDate As String, EndDate As String)

    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

    lngDateCol = 4
    For Each wks In ThisWorkbook.Worksheets
        With wks
            Set wksOutput = ThisWorkbook.Sheets.Add
            ' This is not allowed, you can make some change to the name but it cannot be the same name worksheet
            ' >>> wksOutput.Name = wks.Name

            Set rngTarget = wksOutput.Cells(1, 1)

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

            With rngFull
                .AutoFilter Field:=lngDateCol, _
                            Criteria1:=">=" & StartDate, _
                            Criteria2:="<=" & EndDate


                Set rngResult = rngFull.SpecialCells(xlCellTypeVisible)
                rngResult.Copy Destination:=rngTarget
            End With

            .AutoFilterMode = False
            If .FilterMode = True Then
                .ShowAllData

            End If
        End With
    Next wks
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM