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
 
yes before i was copying 3 columns from database now i added 1 more column.

before 1st column was description 2nd quantity and 3rd unit price
now i added one more column in database now it is
1st column is serial no. 2nd description 3rd quantity and 4th unit price.
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
yes before i was copying 3 columns from database now i added 1 more column.

before 1st column was description 2nd quantity and 3rd unit price
now i added one more column in database now it is
1st column is serial no. 2nd description 3rd quantity and 4th unit price.

after work around in codes. now i know "Ubound" is making problem bcause if i write serial no. & description & qty & unit price than there is no error and data get copy.
but if i write serial no. and leave description cell empty or i write serial no. and description and leave qty cell empty than error show up.

i want these cells should not bound with each other.
 
Upvote 0
Ok, try this
Code:
   Ws2.Range("J65").Value = Fnd.Offset(, 5).Value
   j = 20
   For i = 1 To Ws1.Cells(Fnd.Row, Columns.Count).End(xlToLeft).Column Step 4
      Ws2.Range("a" & j).Value = Ws1.Cells(Fnd.Row, i + 6)
      Ws2.Range("b" & j).Value = Ws1.Cells(Fnd.Row, i + 7)
      Ws2.Range("H" & j).Value = Ws1.Cells(Fnd.Row, i + 8)
      Ws2.Range("I" & j).Value = Ws1.Cells(Fnd.Row, i + 9)
      j = j + 1
   Next i
 
Last edited:
Upvote 0
Ok, try this
Code:
   Ws2.Range("J65").Value = Fnd.Offset(, 5).Value
   j = 20
   For i = 1 To Ws1.Cells(Fnd.Row, Columns.Count).End(xlToLeft).Column Step 4
      Ws2.Range("a" & j).Value = Ws1.Cells(Fnd.Row, i + 6)
      Ws2.Range("b" & j).Value = Ws1.Cells(Fnd.Row, i + 7)
      Ws2.Range("H" & j).Value = Ws1.Cells(Fnd.Row, i + 8)
      Ws2.Range("I" & j).Value = Ws1.Cells(Fnd.Row, i + 9)
      j = j + 1
   Next i

Thnx now no error and data get copy also....really your r awesome in VBA.:cool:
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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