+ Reply to Thread
Results 1 to 1 of 1

VBA Copy & Paste Creates over 2000 blank rows - Looking for options.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-28-2013
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2010, 2013
    Posts
    69

    VBA Copy & Paste Creates over 2000 blank rows - Looking for options.

    Good afternoon!

    I have two spreadsheets (FakeEmployees, FakeTerms) where I am trying to remove employee absence data and employee records for employees that have been terminated from the FakeEmployees workbook to FakeTerms workbook. It is successful, and I am so thankful for the help here. I am looking for a way to combine the two separate macros so that it purges employee data first (Data Tab) then employee records (Employee Tab) from FakeEmployees to the same worksheets in FakeTerms. I'm also wanting to figure out why it is leaving thousands of blank rows before the data is populated on the FakeTerms workbook.

    Any assistance is appreciated!

    Term Data Macro:
    Option Explicit
    
    Sub TermData()
        Dim wb As Workbook, wb1 As Workbook
        Dim ws As Worksheet, ws1 As Worksheet
        Dim cel As Range
        Dim Lr As Long, LR1 As Long, cnt As Long
        Dim MyPath As String
    
        MyPath = ThisWorkbook.Path
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Data")
    
        With ws
            .Unprotect Password:="password"
            Lr = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
        End With
    
        With ws.ListObjects("Data")
            .Range.AutoFilter Field:=16, Criteria1:="<>"
            cnt = .AutoFilter.Range.Columns(9).SpecialCells(xlCellTypeVisible).Cells.Count - 1
            If cnt >= 1 Then
                Application.Workbooks.Open (MyPath & "\" & "FakeTerms.xlsm")
                Set wb1 = ActiveWorkbook
                Set ws1 = wb1.Sheets("Data")
    
                With ws1
                    LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious).Row
                End With
                For Each cel In .AutoFilter.Range.Offset(1, 0).Columns(16).SpecialCells(xlCellTypeVisible)
                    If Not IsEmpty(cel.Value) Then
                        ws.Range(ws.Cells(cel.Row, "A"), ws.Cells(cel.Row, "P")).Copy
                        ws1.Range("A2" & LR1).PasteSpecial (xlPasteValues)
                        LR1 = LR1 + 1
                    End If
                Next cel
            End If
            wb1.Close True
            Application.DisplayAlerts = False
            With ws
                .Range("A2:P" & Lr).SpecialCells(xlCellTypeVisible).Delete
            End With
            Application.DisplayAlerts = True
            .Range.AutoFilter Field:=16
        End With
    End Sub
    Term Employees Macro:
    Option Explicit
    
    Sub TermEmps()
        Dim wb As Workbook, wb1 As Workbook
        Dim ws As Worksheet, ws2 As Worksheet
        Dim cel As Range
        Dim Lr As Long, LR1 As Long, cnt As Long
        Dim MyPath As String
    
        MyPath = ThisWorkbook.Path
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Employees")
    
        With ws
            .Unprotect Password:="password"
            Lr = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
        End With
    
        With ws.ListObjects("Employees")
            .Range.AutoFilter Field:=6, Criteria1:="<>"
            cnt = .AutoFilter.Range.Columns(9).SpecialCells(xlCellTypeVisible).Cells.Count - 1
            If cnt >= 1 Then
                Application.Workbooks.Open (MyPath & "\" & "FakeTerms.xlsm")
                Set wb1 = ActiveWorkbook
                Set ws2 = wb1.Sheets("Employees")
    
                With ws2
                    LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious).Row
                End With
                For Each cel In .AutoFilter.Range.Offset(1, 0).Columns(9).SpecialCells(xlCellTypeVisible)
                    If Not IsEmpty(cel.Value) Then
                        ws.Range(ws.Cells(cel.Row, "A"), ws.Cells(cel.Row, "I")).Copy
                        ws2.Range("A2" & LR1).PasteSpecial (xlPasteValues)
                        LR1 = LR1 + 1
                    End If
                Next cel
            End If
            wb1.Close True
            Application.DisplayAlerts = False
            With ws
                .Range("A2:I" & Lr).SpecialCells(xlCellTypeVisible).Delete
            End With
            Application.DisplayAlerts = True
            .Range.AutoFilter Field:=6
        End With
    End Sub
    FakeTerms.zip
    Last edited by jcaynes; 03-05-2014 at 05:26 PM. Reason: generic password added

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy Formula from set cells, paste in blank rows below as value
    By cappie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2013, 02:45 PM
  2. Copy Paste Formula on Each Blank Rows
    By triaji in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-26-2010, 10:53 AM
  3. Output CSV file creates commas down blank rows
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-05-2010, 05:54 AM
  4. Copy and paste non blank rows from a range
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 10-12-2010, 09:29 AM
  5. Copy rows untill blank row then paste into new sheet
    By Chris Salcedo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-07-2005, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1