Copy Select Cells in For Each loop - VBA

Jlopez21887

New Member
Joined
Oct 31, 2016
Messages
8
I have macro that loops though a WS to find a value of Late in Column W. I copy that row to a new Sheet currently Called Destination Cells.

Currently when the word "Late" is found the action is to copy the entire row to the Destination sheet.

For this report I only need 6-8 Columns to be copied to the destination sheet instead of the entire row. Columns are D-G, R-T, W and AD

AS you can see my last step is to delete unneeded columns but that is becoming cumbersome as the sheet is quite large.

I need to copy only the select cells in that row and then paste to the Destination sheet.

The code part that needs editing is
Code:
"[COLOR=#574123]txt.EntireRow.Copy Worksheets("DestinationCells").Range("A" & rowValue)"
[/COLOR]

Thank you for any help.


Code:
Sub LateStatusReport()

 Dim lastRow As Double
 Dim rowValue As Integer
 Dim txt As Range
 Dim shname As String
 Dim newWS As Worksheet ' eventually set up to create new sheet every time



 Application.ScreenUpdating = False
 Application.DisplayAlerts = False


 
    Worksheets("DestinationCells").Activate
    Worksheets("DestinationCells").Range("A3:AJ50").Delete
    DestinationCellRow = Range("A" & Rows.Count).End(xlUp).Row


[COLOR=#008000]   ' Eventually move to deleting worksheet and recreating new every run
    'Sheets("DestinationCells").Delete
    ' Set newWS = Sheets.Add(After:=Sheets(Worksheets.count))
    'ActiveSheet.Name =  "DestinationCells"[/COLOR]
    
    


    
[COLOR=#008000]   'Find if user input sheet exists[/COLOR]
  
   Do Until WorksheetExists(shname)
       shname = InputBox("Enter sheet name")
       If Not WorksheetExists(shname) Then MsgBox shname & " doesn't exist!", vbExclamation
    Loop




[COLOR=#008000]'select user input sheet and get last row[/COLOR]
    Sheets(shname).Select
    lastRow = Range("A" & Rows.Count).End(xlUp).Row


[COLOR=#008000]'copy header rows[/COLOR]
    Worksheets(shname).Range("A1:AJ3").Copy _
    Destination:=Worksheets("DestinationCells").Range("A1")


[COLOR=#008000]'set paste row to row 4 to avoid header[/COLOR]
    rowValue = 4
    
[COLOR=#008000]
'find if the value "Late" in Column W, if yes paste data to new sheet[/COLOR]
    
    For Each txt In Sheets(shname).Range("W2:W" & lastRow)
        If txt.Value = "Late" Then
            [COLOR=#008000] 'copy and paste to Destination sheet[/COLOR]
            txt.EntireRow.Copy Worksheets("DestinationCells").Range("A" & rowValue)
            rowValue = rowValue + 1
        End If
    Next txt
    

[COLOR=#008000] 'Delete Columns that are not needed[/COLOR]
  Sheets("DestinationCells").Range("A:A,B:B,C:C,H:H,I:I,J:J,K:K,L:L,M:M,N:N,O:O,P:P,Q:Q,U:U,X:X,Y:Y,Z:Z,AA:AA,AB:AB,AC:AC").EntireColumn.Delete
    
 Application.ScreenUpdating = True
 Application.DisplayAlerts = True
 


ActiveWorkbook.Save



 End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Rather than looping through the cells, youl'd be better using an autofilter
Code:
Sub LateStatusReport()

 Dim lastRow As Double
 Dim rowValue As Integer
 Dim txt As Range
 Dim shname As String
 Dim newWS As Worksheet ' eventually set up to create new sheet every time



 Application.ScreenUpdating = False
 Application.DisplayAlerts = False


 
    Worksheets("DestinationCells").Activate
    Worksheets("DestinationCells").Range("A3:AJ50").Delete
    DestinationCellRow = Range("A" & Rows.Count).End(xlUp).Row


   ' Eventually move to deleting worksheet and recreating new every run
    'Sheets("DestinationCells").Delete
    ' Set newWS = Sheets.Add(After:=Sheets(Worksheets.count))
    'ActiveSheet.Name =  "DestinationCells"
    
    


    
   'Find if user input sheet exists
  
   Do Until WorksheetExists(shname)
       shname = InputBox("Enter sheet name")
       If Not WorksheetExists(shname) Then MsgBox shname & " doesn't exist!", vbExclamation
    Loop




'select user input sheet and get last row
    Sheets(shname).Select
    lastRow = Range("A" & Rows.Count).End(xlUp).Row


'copy header rows
    Worksheets(shname).Range("A1:AJ3").Copy _
    Destination:=Worksheets("DestinationCells").Range("A1")


'set paste row to row 4 to avoid header
    rowValue = 4
    

'find if the value "Late" in Column W, if yes paste data to new sheet
    
   With Sheets(shname)
      .Range("A3:AJ3").AutoFilter 23, "Late"
      .AutoFilter.Range.Offset(1).EntireRow.Copy Worksheets("DestinationCells").Range("A4")
      .AutoFilterMode = False
   End With
    

 'Delete Columns that are not needed
  Sheets("DestinationCells").Range("A:C,H:Q,U:U,X:AC").EntireColumn.Delete
    
 Application.ScreenUpdating = True
 Application.DisplayAlerts = True
 


ActiveWorkbook.Save



 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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