Results 1 to 10 of 10

Thread: VBA To Create A New Workbook

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Try an alternative

    Code:
    Sub TrimWorkbook()
    
        Dim colLetter As String, SavePath As String
        Dim lastValue As String
        Dim wb As Workbook
        Dim lng As Long
        Dim currentRow As Long
        colLetter = "K"
        SavePath = ThisWorkbook.Path
        'Sort the workbook.
        With ThisWorkbook.Worksheets(1)
            .Cells.AutoFilter field:=.Cells(1, colLetter).Column, Criteria1:="Y"
            lng = .Cells(.Rows.Count, colLetter).End(xlUp).Row
            Set wb = Application.Workbooks.Add(xlWorksheet)
            .Rows(1 & ":" & lng).Copy wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp)
            wb.SaveAs SavePath & "\" & "CPC_Weekend_Work_" & Format(Now(), "DD-MMM-YYYY"), 51
            wb.Close
            .AutoFilterMode = False
        End With
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    Works perfect. Thanks

  3. #3
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    15
    Try this revised version with save new workbook correction:

    Code:
    Sub lm()
    
        Dim wbkNew              As Workbook
        Dim strFileName         As String
        Dim rngRange            As Range
        Dim rngTemp             As Range
        Dim rngFirst            As Range
        Dim lngCount            As Long
        
        strFileName = "CPC_Weekend_Work_" & Format(Now(), "DD-MMM-YYYY") & ".XLSX"
        strFileName = ThisWorkbook.Path & Application.PathSeparator & strFileName
        With ThisWorkbook.Worksheets("Sheet1")
            Set rngRange = .Range("K1")
            Set rngRange = .Range(rngRange, .Cells(.Rows.Count, rngRange.Column).End(xlUp))
        End With
        Set wbkNew = Workbooks.Add
        With wbkNew
            With .Worksheets("Sheet1")
                With rngRange
                    lngCount = 0
                    Set rngTemp = .Find("Y", LookIn:=xlValues, LookAt:=xlWhole)
                    If Not rngTemp Is Nothing Then
                        Set rngFirst = rngTemp
                        Do
                            .Range("A1").Offset(lngCount).Value = rngTemp.Value
                            lngCount = lngCount + 1
                            Set rngTemp = .FindNext(rngTemp)
                        Loop While Not rngTemp Is Nothing And rngTemp.Address <> rngTemp.Address
                    End If
                End With
            End With
            .SaveAs strFileName, 51
            .Close
        End With
        
        Set wbkNew = Nothing
        strFileName = vbNullString
        Set rngRange = Nothing
        Set rngTemp = Nothing
        Set rngFirst = Nothing
        lngCount = Empty
        
    End Sub

Similar Threads

  1. Excel VBA Code to Add New Sheets
    By cdurfey in forum Excel Help
    Replies: 1
    Last Post: 06-25-2013, 08:05 AM
  2. VBA Code to create Pivot tables
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 08-05-2012, 02:41 AM
  3. Create Random Number Generator VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 12-01-2011, 10:51 AM
  4. VBA to Create Grouping (Difficult)
    By Biz in forum Excel Help
    Replies: 4
    Last Post: 07-12-2011, 03:25 AM
  5. Write/Create Text File VBA
    By Admin in forum Download Center
    Replies: 0
    Last Post: 06-20-2011, 01:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •