Easy macro doesnt work

lionginass

New Member
Joined
Jul 29, 2016
Messages
24
Hello.

Have 2 sheets: "Data" and "Suppliers"

Trying to create a vba code, which scans Sheet"Data" column C and if there is value "No" in a cell, then copy columns A:B (same row where value "No" was found)
Then the code should paste that range in a sheet "Suppliers" Column B:C (last row)

Attaching photos of both sheets.
My point is to copy from sheet "Data" Range A1:B2 and paste it to sheet "Suppliers" Range B4:C5

My file is actually much bigger, but i made it simplier to test the code.
My code is here:

Sub Add()


Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim LR1 As Integer
Dim LR2 As Integer

Set ws1 = Worksheets("Data")
Set ws2 = Worksheets("Suppliers")

LR1 = ws1.UsedRange.Rows.Count
LR2 = ws2.Cells.SpecialCells(xlCellTypeLastCell).Row + 1

For i = 1 To LR1

If ws1.Range("C" & i).Value = "No" Then
ws1.Range(Cells(i, 2), Cells(i, 3)).Copy ws2.Cells(LR2, 2)
LR2 = LR2 + 1
End If

Next i

Getting error : Method range of object Worksheet failed

Any ideas what is wrong?
 

Attachments

  • Suppliers.PNG
    Suppliers.PNG
    3.4 KB · Views: 1
  • Data.PNG
    Data.PNG
    2.8 KB · Views: 1

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You need to qualify the cells like
VBA Code:
 ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, 2)).Copy ws2.Cells(LR2, 2)
or
VBA Code:
 ws1.Range("A"&i).Resize(,2).Copy ws2.Cells(LR2, 2)
Also your code is copying col B & C, not A & B
 
Upvote 0
Although I would use Autofilter to copy them in one go try the changes in red

Rich (BB code):
If ws1.Range("C" & i).Value = "No" Then
ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, 2)).Copy ws2.Cells(LR2, 2)
LR2 = LR2 + 1
End If
or you can lose the first ws1
Rich (BB code):
If ws1.Range("C" & i).Value = "No" Then
Range(ws1.Cells(i, 1), ws1.Cells(i, 2)).Copy ws2.Cells(LR2, 2)
LR2 = LR2 + 1
End If
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Just as I mentioned it earlier the way that I would normally do this (although on very large data I do sometimes use an array) is using the Autofilter.
The advantage is it only pastes once rather than pasting every time the loop finds a match and so should be faster.
It might be worth you looking at some time (sample code below).

VBA Code:
Sub Filter_lioinginass()
    Dim ws1 As Worksheet, ws2 As Worksheet

    Application.ScreenUpdating = False
    
    Set ws1 = Worksheets("Data")
    Set ws2 = Worksheets("Suppliers")
    
    With ws1.Range("C1:C" & ws1.Range("C" & Rows.Count).End(xlUp).Row)
    
        .AutoFilter 1, "No"
        
        On Error Resume Next
        .Offset(1, -2).Resize(.Rows.Count - 1, 2).SpecialCells(12).Copy _
        ws2.Range("B" & Rows.Count).End(xlUp).Offset(1)
        On Error GoTo 0
        
    End With
    
    ws1.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub

Happy that you now have your code working.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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