How to copy particular columns and paste in another sheet on vba

carac

New Member
Joined
Jun 22, 2017
Messages
1
Hi,

I have been trying to copy certain columns of two spreadsheets based on whether or not they match a particular cell, and then paste them in the another worksheet. This is the code I have so far:

Sub search()
Dim i As Integer
Dim j As Integer
Dim k As Long
Dim counter As Integer
Dim Name As String
counter = 2
For i = 2 To 5
Name = Sheets("Sold").Cells(i, "B").Value
For j = 1 To 2278
If Name = Sheets("Case Data").Cells(j, "D") Then
Sheets("Sheet1").Cells(counter, "A").Value = Name
With Sheets("Case Data")
Worksheets("Sheet2").Range(.Cells(j, "C"), .Cells(j, "AE")).Copy _
Destination:=Worksheets("Sheet4").Range(Sheets("Sheet4").Cells(counter, "B"))
counter = counter + 1
End With
End If
Next j

For k = CLng(2) To 40525
If Name = Sheets("Claims").Cells(k, "G") Then
Sheets("Sheet1").Cells(counter, "A").Value = Name
Sheets("Claims").Range(Cells(k, "S")).Copy _
Sheets("Sheet1").Range(Cells(counter, "AE"))
Sheets("Claims").Range(Cells(k, "X")).Copy _
Sheets("Sheet1").Range(Cells(counter, "AF"))
counter = counter + 1
End If
Next k
Next i


End Sub



I keep getting a 1004 error under the With statement. Any thoughts or suggestions?
Thank you!!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
With Sheets("Case Data")
Worksheets("Sheet2").Range(.cells(j, "C"), .cells(j, "AE")).copy _

These dots mean the range belong "With Sheets("Case Data")".
So this line says Worksheets("Sheet2").Range(Sheets("Case Data").cells(j, "C"), Sheets("Case Data").cells(j, "AE")).copy _
This is non-existent range.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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