VBA Copy From Columns If Value Equal

ir121973

Active Member
Joined
Feb 9, 2008
Messages
371
Hi, I wonder whether someone could help me please.

I've put together the code below which copies data from non sequential columns.

Code:
Dim lRow, lCol As Integer


Sheets("Amalgamation of Search").Select
lRow = Range("A" & Rows.Count).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column


For Each cell In Range(Cells(, "C"), Cells(1, lCol))
   With .Cells(lRow, "C")
    If .Value = "SC" Then
    Union(Range("A1:A" & lRow), Range("C1:C" & lRow), Range(Cells(1, cell.Column), Cells(lRow, cell.Column))).Copy
    Workbooks.Add
    Range("A1").PasteSpecial
    ActiveWorkbook.SaveAs Filename:= _
    "\\c\s\CAF1\Digital Delivery Group\DDCOPS\Data Security\SC Data.xls"
    ActiveWorkbook.Close
    End With
Next cell


Application.CutCopyMode = False
End Sub

The code works except for this piece of code
Code:
   With .Cells(lRow, "C")
    If .Value = "SC" Then

What I'm trying to say is if the value = "SC" then I want it to copy the data, but when I run this I receive a "Invalid or unqualified reference" higlighting this line as the cause

With .Cells

Could someone perhaps look at this please and let me know where I've gone wrong.

Many thanks and kind regards

Chris
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try removing the . before the word Cells
 
Upvote 0
Hi Trevor G, thank you for this, but unfortunately, although I don't receive an error message, it doesn't copy any data.

Many thanks

Chris
 
Upvote 0

Forum statistics

Threads
1,216,753
Messages
6,132,514
Members
449,731
Latest member
dasda34

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