Data extraction based on condition

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
152
Office Version
  1. 2021
Platform
  1. Windows
Code:
Sub transfer()
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim myname As String
lastrow1 = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row


Worksheets("Sheet1").Select
Application.ScreenUpdating = False


For i = 2 To lastrow1
myname = Sheets("sheet1").Cells(i, "A").Value


Sheets("sheet2").Activate
lastrow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row


For j = 2 To lastrow2


If Sheets("sheet2").Cells(j, "A").Value = myname Then
Sheets("sheet1").Activate
Sheets("sheet1").Range(Cells(i, "B"), Cells(i, "G")).Copy
Sheets("sheet2").Activate
Sheets("sheet2").Range(Cells(j, "B"), Cells(j, "C")).Select
ActiveSheet.Paste
End If


Next j
Application.CutCopyMode = False
Next i
Sheets("sheet2").Activate
Sheets("sheet2").Range("A1").Select
End Sub


can somebody help with these codes which extract data as per given names. i want help to copy values from anywhere from cells of data sheet and copy in another sheet any given cell. which part of codes i shall change. Thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Is this what you're after
Code:
Sub CopyFilter()
   Dim Cl As Range
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1)
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 1).Resize(, 6).Copy .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
thankx
the codes which i share above also do same data filter and copy. but i need when it filter data and copy it should copy as per my given cells where ever i want dnot copy as a row. and if the filter data doesnt find given name or number it should show message nothing found.
 
Upvote 0
I'm afraid I don't understand what you are asking.
Can you please give some examples.
 
Upvote 0
i mean your given codes work fine, but it copy range of cells to another sheet as range. whereas i want it should copy range of cells in another sheet but on different cells whatever cells i give. for example it copy values after filter from B to D cells and paste on D5 and E8 and G10 or any other cell i want.
it should show me pop message if the filter data not found in database.
 
Upvote 0
How will the code know which cells to copy & where to paste them?
 
Upvote 0
Ok, try this
Code:
Sub CopyFilter()
   Dim Cl As Range
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1)
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            Cl.Offset(, 1).Resize(, 3).Copy .Item(Cl.Value)
            Cl.Offset(, 4).Copy .Item(Cl.Value).Offset(, 4)
            Cl.Offset(, 5).Copy .Item(Cl.Value).Offset(, 6)
            Cl.Offset(, 6).Copy .Item(Cl.Value).Offset(, 8)
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
thanks this what i need. Thanks so much....since 3 hours i was trying myself... actually not gud with codes....1 last thing can you add codes which can clear previous cells and paste again. if they dont find value in sheet1 it should show pop message not found.
 
Upvote 0
Try
Code:
Sub CopyFilter()
   Dim Cl As Range
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   Dim Flg As Boolean
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws2.Range("A2", Ws2.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1)
      Next Cl
      For Each Cl In Ws1.Range("A2", Ws1.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then
            Flg = True
            Range("2:1048576").ClearContents
            Cl.Offset(, 1).Resize(, 3).Copy .Item(Cl.Value)
            Cl.Offset(, 4).Copy .Item(Cl.Value).Offset(, 4)
            Cl.Offset(, 5).Copy .Item(Cl.Value).Offset(, 6)
            Cl.Offset(, 6).Copy .Item(Cl.Value).Offset(, 8)
         End If
      Next Cl
   End With
   If Flg = False Then MsgBox "Nothing Found"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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