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
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    15
    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

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

    With ThisWorkbook.Worksheets("Sheet1")

  3. #3
    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

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
  •