Transferring specific cells to the first empty row in another sheet

hyd1956

New Member
Joined
Jun 26, 2020
Messages
47
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

As an example, I'm trying to copy cells from columns: B,C,Y and Z for every row where column AB is equal to Y in sheet 1, to the first blank row of another sheet whilst incrementing the number in column A. Can someone help with this please?

Thanks

VBA Code:
[/
Private Sub CommandButton1_Click()
Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer

Set lookUpSheet = Worksheets("Monthly Review Sheet")
Set updateSheet = Worksheets("Sheet2")

If MsgBox("Load records?", vbYesNo) = vbYes Then

lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row

     For t = 1 To lastRowLookup
        If lookUpSheet.Cells(t, 28) = "Y" Then
            updateSheet.Cells(i, 2) = lookUpSheet.Cells(t, 2)
            updateSheet.Cells(i, 3) = lookUpSheet.Cells(t, 3)
    
            Exit For
        End If
     Next t

Application.ScreenUpdating = True

Application.CutCopyMode = False
MsgBox ("Records updated")

End If
End Sub
]
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,206
Office Version
  1. 2007
Platform
  1. Windows
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim lr As Long
  
  Application.ScreenUpdating = False
  With Sheets("Monthly Review Sheet")
    If .AutoFilterMode Then .AutoFilterMode = False
    lr = .Range("B" & Rows.Count).End(3).Row
    .Range("A1:AB1").AutoFilter 28, "Y"
    .AutoFilter.Range.Offset(1).Range("A1:B" & lr & ",Y1:Z" & lr).Copy
    Sheets("Sheet2").Range("B" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
    .ShowAllData
  End With
  Application.CutCopyMode = False
End Sub
 

hyd1956

New Member
Joined
Jun 26, 2020
Messages
47
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thanks for the help.

I've tried the above but I receive a 1004 runtime error on the following line: .Range("A1:AB1").AutoFilter 28, "Y"

In case it helps, columns A:U are in table populated by power query, V-AB are formula columns
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,206
Office Version
  1. 2007
Platform
  1. Windows
So you want to copy cells that are inside the table (A and B) and cells that are outside the table (Y and Z)?
Try the following, change Table1 in the macro to the name of your table.

VBA Code:
Private Sub CommandButton1_Click()
  Dim lr As Long
  
  Application.ScreenUpdating = False
  With Sheets("Monthly Review Sheet")
    If .Range("Table1").AutoFilter Then .Range("Tabla1").AutoFilter
    If .AutoFilterMode Then .AutoFilterMode = False
    lr = .Range("AB" & Rows.Count).End(3).Row
    .Range("AB1:AB" & lr).AutoFilter 1, "Y"
    .Range("A1:B" & lr & ",Y1:Z" & lr).Offset(1).Copy
    Sheets("Sheet2").Range("B" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
    .ShowAllData
  End With
  Application.CutCopyMode = False
End Sub
 

hyd1956

New Member
Joined
Jun 26, 2020
Messages
47
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I want to add some information along with copying the data from the table and the formula calculations within each row on at the bottom of the other sheet. I've got the following code so far, but I'm having trouble in getting it to copy all the data from the "Review" sheet when the transfer column is set to Y. At the moment it works to move over a single line but it's not looping through. Any ideas on how to fix or to adapt your code?

VBA Code:
Public Sub mtreview()

Application.ScreenUpdating = False
Dim duplicatesheet As Worksheet, trackersheet As Worksheet
Dim lastRowupdateMT As Long, lastRowLookupMT As Long
Dim NextRow As Long, LastRowColumnAM As Long
Dim DatAa() As Variant
ReDim DatAa(1 To 1, 1 To 11)


Set trackersheet = ThisWorkbook.Worksheets("Tracker")
Set duplicatesheet = ThisWorkbook.Worksheets("Review Sheet")

lastRowLookupMT = duplicatesheet.Cells(Rows.Count, 1).End(xlUp).Row
lastRowupdateMT = trackersheet.Cells(Rows.Count, 1).End(xlUp).Row

    
With duplicatesheet
       .Range("V1:AB" & lastRowLookupMT).AutoFilter Field:=7, Criteria1:="Y"
       LastRowColumnMA = trackersheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
       duplicatesheet.Range("AO1").Value = duplicatesheet.Range("AO1").Value
       reportdate = duplicatesheet.Range("AO1").Value
For i = 1 To LastRowColumnMA
     valueToSearch = trackersheet.Cells(i, 1)
     For t = 1 To lastRowLookupMT
        If duplicatesheet.Cells(t, 28) = "Y" And "" = valueToSearch Then
            DatAa(1, 1) = lastRowupdateMT
            DatAa(1, 2) = "Open"
            DatAa(1, 3) = duplicatesheet.Cells(t, 4)
            DatAa(1, 4) = duplicatesheet.Cells(t, 2)
            DatAa(1, 5) = ""
            DatAa(1, 6) = duplicatesheet.Cells(t, 8)
            DatAa(1, 7) = duplicatesheet.Cells(t, 23)
            DatAa(1, 8) = duplicatesheet.Cells(t, 22)
            DatAa(1, 9) = reportdate
            DatAa(1, 10) = duplicatesheet.Cells(t, 27)
            DatAa(1, 11) = ""
            
            trackersheet.Range("A" & LastRowColumnMA).Offset(0, 0).Resize(UBound(DatAa, 1), UBound(DatAa, 2)).Value = DatAa
            
            DatAa(1, 1) = vbNullString
            DatAa(1, 2) = vbNullString
            DatAa(1, 3) = vbNullString
            DatAa(1, 4) = vbNullString
            DatAa(1, 5) = vbNullString
            DatAa(1, 6) = vbNullString
            DatAa(1, 1) = vbNullString
            DatAa(1, 7) = vbNullString
            DatAa(1, 8) = vbNullString
            DatAa(1, 9) = vbNullString
            DatAa(1, 10) = vbNullString
            DatAa(1, 11) = vbNullString
        
lastRowLookupMT = lastRowLookupMT
lastRowupdateMT = lastRowupdateMT

            Exit For
        End If
     Next t
Next i

End With
Application.ScreenUpdating = True
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,578
Messages
5,770,949
Members
425,653
Latest member
UNSING

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
Top