How to avoid copying data

ExcelPupper

Board Regular
Joined
Mar 2, 2020
Messages
112
Office Version
  1. 2019
Platform
  1. Windows
Hello. To begin with, when I ran this code earlier, I noticed that the last row of my data contained the column headers of "ProcessX" sheet and as I checked this sheet (ProcessX) it does not contain any data for now.
My question is what modifications should I do for this code to avoid copying from sheets that does not contain data? Many thanks.


VBA Code:
Sub TestProcess()
    Application.ScreenUpdating = False
    Dim lastrow As Long, srcWB As Workbook, desWS As Worksheet

    Dim MyLoc As String, MyFile As String
    Dim wb As Workbook

    MyLoc = "C:\Users\ADMIN\Desktop\TestFolder\" & ThisWorkbook.Sheets("Sample").Range("A1").Value & "\TestData\"
    MyFile = MyLoc & ThisWorkbook.Sheets("data").Range("B5").Value
   
    Set wb = Workbooks.Open(MyFile)
       
    Set srcWB = wb
    Set desWS = ThisWorkbook.Sheets("ProcessTree")
    With srcWB
       
    With .Sheets("Process1")
            lastrow = .Range("E" & .Rows.Count).End(xlUp).Row
            .Range("A2:A" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("E2:E" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "E").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("F2:F" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "F").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("I2:I" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "G").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("O2:O" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "J").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("D2:D" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "K").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With
       
        With .Sheets("Process2")
            lastrow = .Range("C" & .Rows.Count).End(xlUp).Row
            .Range("A2:A" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("C2:C" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "E").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("D2:D" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "F").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("G2:G" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "G").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("M2:M" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "J").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("H2:H" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "K").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With
  
        With .Sheets("ProcessX")
            lastrow = .Range("C" & .Rows.Count).End(xlUp).Row
            .Range("A2:A" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("C2:C" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "E").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("D2:D" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "F").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("G2:G" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "G").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("M2:M" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "J").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Range("H2:H" & lastrow).Copy
            desWS.Cells(desWS.Rows.Count, "K").End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With

End With
       
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    wb.Close SaveChanges:=False

   
   
 
   
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You can do something like this in each With block:
VBA Code:
        With .Sheets("Process1")
            lastRow = .Range("E" & .Rows.Count).End(xlUp).Row
            If lastRow > 1 Then
                .Range("A2:A" & lastRow).Copy
                desWS.Cells(desWS.Rows.Count, "D").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                .Range("E2:E" & lastRow).Copy
                desWS.Cells(desWS.Rows.Count, "E").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                .Range("F2:F" & lastRow).Copy
                desWS.Cells(desWS.Rows.Count, "F").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                .Range("I2:I" & lastRow).Copy
                desWS.Cells(desWS.Rows.Count, "G").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                .Range("O2:O" & lastRow).Copy
                desWS.Cells(desWS.Rows.Count, "J").End(xlUp).Offset(1).PasteSpecial xlPasteValues
                .Range("D2:D" & lastRow).Copy
                desWS.Cells(desWS.Rows.Count, "K").End(xlUp).Offset(1).PasteSpecial xlPasteValues
            End If
        End With
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top