VBA to search column and copy entire rows to new sheet

Padthelad

Board Regular
Joined
May 13, 2016
Messages
64
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a problem where I want to be able to search a particular column and then copy that entire row to a new workbook based on the value found in the particular column.

I have the following worksheet called '2016':-

ABCDEFGHIJKLM
1DATENAMEDATE PAIDINV NONETVATTOTALDaily netGross Total OutstandingNet LCNet TBMonthly Totals
206/10/2016Customer 106/10/201638216130.6826.14156.82
306/10/2016TB - TB101 Customer 2deposit paid3821711,990.002,398.0014,388.007194.00Balance Remain11990.00
406/10/2016TB - TB102 Customer 3deposit paid382181,170.00234.001,404.00702.00Balance Remain1170.00
506/10/2016MM - MM101 Customer 4deposit paid382194,383.33876.675,260.002630.00Balance Remain4383.33
606/10/2016Customer 538220117.0023.40140.40
706/10/2016TB - TB103 Customer 6deposit paid38221258.3351.67310.0018,049.34155.00Balance Remain258.33
807/10/2016Customer 7382223.920.784.70
907/10/2016Customer 838223127.3725.47152.84
1007/10/2016Customer 907/10/201638224231.0546.21277.26
1107/10/2016Customer 1038225928.05185.611,113.66
1207/10/2016Customer 1138226238.8347.77286.60
1307/10/2016Customer 123822792.7318.55111.28

<tbody>
</tbody>


I would like VBA code to be able to search column C for "deposit paid", if this parameter is met, I would like to copy the entire row to a new workbook, including formatting.

I am currently using the following code, however it only copies the workbook headers and first row but without using the 'Column C' parameters I require.

Code:
Sub GenerateList()
Dim Cell As Range, cRange As Range
Dim LastRow As Long, LastRow2 As Long
Dim wb As Workbook, wb2 As Workbook



Set wb = ActiveWorkbook


LastRow = wb.Sheets("2016").Cells(Rows.Count, "A").End(xlUp).Row
Set cRange = wb.Sheets("2016").Range("M2:M" & LastRow)


If Application.WorksheetFunction.CountIf(cRange, "") > 0 Then
    Set wb2 = Workbooks.Add
    wb.Sheets("2016").Range("A1:M2").Copy wb2.Sheets(1).Range("A2")
    LastRow2 = wb2.Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    For Each Cell In cRange
       
        If Cell.Value = "deposit paid" Then
            Cell.EntireRow.Copy
            wb2.Sheets(1).Range("A" & LastRow2).PasteSpecial xlPasteFormats
            wb2.Sheets(1).Range("A" & LastRow2).PasteSpecial xlValues
            LastRow2 = LastRow2 + 1
        End If
    Next Cell
End If


wb2.Sheets(1).Range("A2:A" & LastRow2).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal


End Sub

Any help anyone can provide is very much appreciated.

Many thanks,

Pad
 
Are you using the Immediate Window? VB Editor > View > Immediate window

If you add the new lines of code I suggested, then when running / stepping through you will see a list of useful information that should help you understand why it's not working. You will have the following information:
1) the range you are looking at in each step along with its corresponding value
2) whether it was copied or not, and if so the row it was copied to (in case over-writing previous results)

If the value looks correct but it's not copying, then this will tell you there's something wrong with the value itself. Examples are hanging spaces at the end of the code, or case sensitivity. Check the text value itself, and add "Option Compare Text" at the top of the code module, before the start of the Sub (note this affects all code in the module). For further clarity you could amend the line
Code:
[COLOR=#ff0000]debug.print cell.address, cell.value[/COLOR][[COLOR=#ff0000][/COLOR]/CODE]
to
[CODE][COLOR=#ff0000]debug.print cell.address, "[" & cell.value & "]"[/COLOR]
as this will reveal hidden spaces

It's very common for code to not perform as expected and debug.print should be a standard approach that will help you to understand what's wrong
 
Last edited:
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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