code copy & paste filtered data doesn't work well

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i have this code issuing by mr. mumps and i amended what i need but it copy incorrectly when i choose specific client it doesn't all data relating the client
main data:
xlfilter.xlsm
ABCDEFGH
1data
2itdateclientinv nototaloldnewnet
3112/5/2020 12:00 صhossan1105002507005,642
4213/5/2020 12:00 صmohammed1116002305002,134
5314/5/2020 12:00 صyosef1127005005001,654
6415/5/2020 12:00 صhossan1135004003002,145
7516/5/2020 12:00 صhossan114600525200151
8617/5/2020 12:00 صmohammed1156105978025,142
9718/5/2020 12:00 صyosef11662066912452
10819/5/2020 12:00 صhossan11763074125,412,415
11920/5/2020 12:00 صhossan1186408135,412154,124
121021/5/2020 12:00 صmohammed11965088551,215125
131122/5/2020 12:00 صyosef12066095745,4122
141223/5/2020 12:00 صhossan1216701,0294521,245,124
151324/5/2020 12:00 صhossan1226801,1015442,154
sheet1




the wrong result



xlfilter.xlsm
ABCDEFGHIJ
2clientitdateclientinv nototaloldnewnet
3112/5/2020 12:00 صhossan1105002507005,642
4hossan213/5/2020 12:00 صmohammed1116002305002,134
5
sheet2



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim fnd As Range, LastRow As Long, lRow As Long, bottomC As Long
    bottomC = Sheets("sheet1").Range("C" & Rows.Count).End(xlUp).Row
    LastRow = Sheets("sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lRow = Sheets("sheet2").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("c3:j" & lRow).ClearContents
    Set fnd = Sheets("sheet1").Range("C:C").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        If fnd.Row = bottomC Then
            lRow = LastRow
        Else
            lRow = Sheets("sheet1").Range("C" & fnd.Row + 1 & ":C" & LastRow).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlNext).Row - 1
        End If
        With Sheets("sheet1")
         .Range("a" & fnd.Row & ":a" & lRow).Copy Cells(Rows.Count, "c").End(xlUp).Offset(1)
        .Range("b" & fnd.Row & ":b" & lRow).Copy Cells(Rows.Count, "D").End(xlUp).Offset(1)
        .Range("c" & fnd.Row & ":c" & lRow).Copy Cells(Rows.Count, "e").End(xlUp).Offset(1)
        .Range("d" & fnd.Row & ":d" & lRow).Copy Cells(Rows.Count, "f").End(xlUp).Offset(1)
        .Range("e" & fnd.Row & ":e" & lRow).Copy Cells(Rows.Count, "g").End(xlUp).Offset(1)
        .Range("f" & fnd.Row & ":f" & lRow).Copy Cells(Rows.Count, "h").End(xlUp).Offset(1)
        .Range("g" & fnd.Row & ":g" & lRow).Copy Cells(Rows.Count, "i").End(xlUp).Offset(1)
        .Range("h" & fnd.Row & ":h" & lRow).Copy Cells(Rows.Count, "j").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What exactly are you trying to do?
 
Upvote 0
hi, fluff when i write in cell a4 in sheet 2 the name based on column c in sheet1 then copy data relating the name to sheet2
 
Upvote 0
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "A4" Then
      Range("C2:J10000").ClearContents
      With Sheets("Sheet1")
         .Range("A2:H2").AutoFilter 3, Target
         .AutoFilter.Range.Copy Me.Range("C2")
         .AutoFilterMode = False
      End With
   End If
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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