Copy cells from one sheet to another if condition met

Hyakkivn

Board Regular
Joined
Jul 28, 2021
Messages
81
Office Version
  1. 2010
Platform
  1. Windows
Hello brothers/sisters.
I would like to ask for any solution for a task please.
I have 2 sheets in 1 workbook as below... The first one is Export, and second one is Bill.
I want that when I change the Bill No in sheet("Bill"), excel will list all product at Name of Product column in sheet("Export") with same Bill No to sheet Bill. Paste location start from cell C9

1.png


2.png
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your "Bill" sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a Bill No in cell G2 and press the RETURN key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "G2" Then Exit Sub
    Application.ScreenUpdating = False
    With Sheets("Export")
        .Range("A3", .Range("A" & .Rows.Count).End(xlUp)).AutoFilter 1, Target
        .Range("D4", .Range("D" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy Cells(Rows.Count, "C").End(xlUp).Offset(1)
        .Range("A3").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your "Bill" sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a Bill No in cell G2 and press the RETURN key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "G2" Then Exit Sub
    Application.ScreenUpdating = False
    With Sheets("Export")
        .Range("A3", .Range("A" & .Rows.Count).End(xlUp)).AutoFilter 1, Target
error on this line >>>        .Range("D4", .Range("D" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy Cells(Rows.Count, "C").End(xlUp).Offset(1)
        .Range("A3").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
Thanks for your code. But it has some problem with copy. VBA notice that Copy method of range class failed.
Btw, which key is RETURN key???
 
Last edited:
Upvote 0
I tested the macro using some dummy data and it worked properly. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not pictures) of your two sheets. Alternately, you could upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. The RETURN key is the ENTER key.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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