Results 1 to 10 of 10

Thread: VBA To Create A New Workbook

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0

    VBA To Create A New Workbook

    I need a vba code to create a new workbook and then copy over only the rows that have a "Y" in column K. I need this to save as "CPC Weekend Work" with the current date. Can anyone help? Thanks

  2. #2
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    14
    Try this code and change sheet name accordingly:

    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.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
            .SaveAs strFileName
            .Close
        End With
        
        Set wbkNew = Nothing
        strFileName = vbNullString
        Set rngRange = Nothing
        Set rngTemp = Nothing
        Set rngFirst = Nothing
        lngCount = Empty
        
    End Sub

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    I am getting an error on this line

    With ThisWorkbook.Worksheets("Sheet1")

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    As mentioned by Lalit, you should correct the name of the sheet you use in the code. In your post above, it is Sheet1
    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

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    Can you show me just the portion of code that will bring over the entire row if I have a "Y" in column K? I do have other cells that will also have a "Y" in them so I need this to be specific to column K. Thanks

  6. #6
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Code:
        With ThisWorkbook.Worksheets("Sheet1")
            Set rngRange = .Range("K1")
            Set rngRange = .Range(rngRange, .Cells(.Rows.Count, rngRange.Column).End(xlUp))
        End With
    does exactly target column K
    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

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    For some reason it will create a new workbook but it will not bring any information over. Can you help? I need it to bring over the 1st row completely since it has the column titles and then any row below that has a "Y" is column K. Thanks

  8. #8
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    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

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

  10. #10
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    14
    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
  •