Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 31

Thread: Solved: Copy cells in select columns that match other cells in select columns

  1. #1

    Question Solved: Copy cells in select columns that match other cells in select columns

    I tried this code to copy the item in the columns 1,2 ,7,8,9,10 on condition that belong to match cells in columns 3,4,5,6 in same row as it show in the pic. but it didn't succeed with me.

    [vba]
    Dim MyRange1 As Range
    Dim MyRange2 As Range
    Dim cell As Range
    Dim cellA As Range
    Dim i As Long
    Set MyRange1 = Sheets(1).Range("C1")
    Set MyRange2 = Sheets(1).Range("L1")
    For i = 1 To UsedRange.Rows.Count
    For Each cell In MyRange1
    For Each cellA In MyRange2
    If cell.Value = TextBox1.Text And cell.Offset(0, 1).Value = TextBox2.Text _
    And cell.Offset(0, 2).Value = TextBox3.Text And cell.Offset(0, 3).Value = TextBox4.Text Then
    cellA(i, 12).Value = cell.Offset(i, -2).Value
    cellA(i, 13).Value = cell.Offset(i, -1).Value
    cellA(i, 14).Value = cell.Offset(i, 7).Value
    cellA(i, 15).Value = cell.Offset(i, 8).Value
    cellA(i, 16).Value = cell.Offset(i, 9).Value
    cellA(i, 17).Value = cell.Offset(i, 10).Value
    End If

    Next cellA
    Next cell
    Next i

    [/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Guessing, but maybe

    [vba]

    Dim MyRange1 As Range
    Dim MyRange2 As Range
    Dim cell As Range
    Dim cellA As Range
    Dim i As Long
    Set MyRange1 = Sheets(1).Range("C1")
    Set MyRange2 = Sheets(1).Range("L1")
    For i = 1 To UsedRange.Rows.Count
    For Each cell In MyRange1
    For Each cellA In MyRange2
    If cell.Value = TextBox1.Text And cell.Offset(0, 1).Value = TextBox2.Text _
    And cell.Offset(0, 2).Value = TextBox3.Text And cell.Offset(0, 3).Value = TextBox4.Text Then
    cells(i, 12).Value = cell.Offset(i, -2).Value
    cells(i, 13).Value = cell.Offset(i, -1).Value
    cells(i, 14).Value = cell.Offset(i, 7).Value
    cells(i, 15).Value = cell.Offset(i, 8).Value
    cells(i, 16).Value = cell.Offset(i, 9).Value
    cells(i, 17).Value = cell.Offset(i, 10).Value
    End If

    Next cellA
    Next cell
    Next i
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    I tried but didn't work. by the way I made it like this:
    [VBA]
    Dim MyRange1 As Range
    Dim cell As Range
    Dim i As Long
    Set MyRange1 = Sheets(1).Range("C1")
    For i = 1 To UsedRange.Rows.Count
    For Each cell In MyRange1
    If cell.Value = TextBox1.Text And cell.Offset(0, 1).Value = TextBox2.Text _
    And cell.Offset(0, 2).Value = TextBox3.Text And cell.Offset(0, 3).Value = TextBox4.Text Then
    Cells(i, 12).Value = cell.Offset(i, -2).Value
    Cells(i, 13).Value = cell.Offset(i, -1).Value
    Cells(i, 14).Value = cell.Offset(i, 7).Value
    Cells(i, 15).Value = cell.Offset(i, 8).Value
    Cells(i, 16).Value = cell.Offset(i, 9).Value
    Cells(i, 17).Value = cell.Offset(i, 10).Value
    End If


    Next cell
    Next i
    [/VBA]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Then I have little idea what you are trying to achieve.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    If you took a look at the pic. each cells after the last red cell and before the red cell they arange in the columns as I signed by arangement. on condition they belong to rwos that contain 4 columns match (C,D,E,F).

    The red cells in the coulumns are match so the code will copy the cells in the columns : A,B,G,H,I,J . as it show in the end of the pic. I hope I'm clear.
    Thank you for help

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Red cells? Don't see any.

  7. #7
    I mean red item (K,D,D,K). I'll try to explain again:

    this code is to arrange the items in the columns 1,2,7,8.9.10 on condition that match in 4 columns 3,4,5,6.

    Example: I set in the textbox the item that belong to the columns 3,4,5,6. (K,D,D,K). So the code must copy the item in the columns 1,2,7,8.9.10 in three rows ( that the columns 3,4,5,6. contain these item K,D,D,K) as ite show in the end of the pic ( I signed by "Arrangement"). I hope I'm clear.

  8. #8
    I tred this code and it works well but only for the items which don't contain blank rows between them. so the problem that I faced now is that arrange the output items in the same rwos with input items and I want to make this code or another code work with if there are a balnk rows between the items as it show in the pic above.

    [VBA]Dim i As Long
    For i = 1 To UsedRange.Rows.Count

    For j = 1 To UsedRange.Rows.Count

    Cells(i, 12).Value = Cells(j, 1).Value
    Cells(i, 13).Value = Cells(j, 2).Value
    Cells(i, 14).Value = Cells(j, 7).Value
    Cells(i, 15).Value = Cells(j, 8).Value
    Cells(i, 16).Value = Cells(j, 9).Value
    Cells(i, 17).Value = Cells(j, 10).Value


    Next j
    Next i[/VBA]

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Nader, would you be so kind as to post your workbook? There is some difficulty in understanding what you are chasing here.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  10. #10
    Here is the workbook. and I'm sorry

  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Please stop cross-posting...

    http://www.xtremevbtalk.com/showthread.php?t=294442
    http://www.vbforums.com/showthread.php?t=513747

    If I keep catching you do this I will start deleting your posts. If you post the same question on another forum, at least post the link, so that our members know what they are getting into.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this

    [vba]

    Private Sub CommandButton1_Click()
    Dim mpStartRow As Long
    Dim mpRow As Range
    Dim MyRange1 As Range
    Dim MyRange2 As Range
    Dim cell As Range
    Dim cellA As Range
    Dim i As Long, j As Long

    Set MyRange1 = Sheets(1).Range("C1")
    Set MyRange2 = Sheets(1).Range("L1")

    mpStartRow = UsedRange.Cells(1, 1).Row
    For i = 1 To UsedRange.Rows.Count
    If Cells(mpStartRow - 1 + i, "C").Value = TextBox1.Text And _
    Cells(mpStartRow - 1 + i, "D").Value = TextBox2.Text And _
    Cells(mpStartRow - 1 + i, "E").Value = TextBox3.Text And _
    Cells(mpStartRow - 1 + i, "F").Value = TextBox4.Text Then
    Cells(mpStartRow - 1 + i, "L").Value = Cells(mpStartRow - 1 + i, "A").Value
    Cells(mpStartRow - 1 + i, "M").Value = Cells(mpStartRow - 1 + i, "B").Value
    Cells(mpStartRow - 1 + i, "N").Value = Cells(mpStartRow - 1 + i, "G").Value
    Cells(mpStartRow - 1 + i, "O").Value = Cells(mpStartRow - 1 + i, "H").Value
    Cells(mpStartRow - 1 + i, "P").Value = Cells(mpStartRow - 1 + i, "I").Value
    Cells(mpStartRow - 1 + i, "Q").Value = Cells(mpStartRow - 1 + i, "J").Value
    End If
    Next i
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Quote Originally Posted by xld
    Try this

    [vba]

    Private Sub CommandButton1_Click()
    Dim mpStartRow As Long
    Dim mpRow As Range
    Dim MyRange1 As Range
    Dim MyRange2 As Range
    Dim cell As Range
    Dim cellA As Range
    Dim i As Long, j As Long

    Set MyRange1 = Sheets(1).Range("C1")
    Set MyRange2 = Sheets(1).Range("L1")

    mpStartRow = UsedRange.Cells(1, 1).Row
    For i = 1 To UsedRange.Rows.Count
    If Cells(mpStartRow - 1 + i, "C").Value = TextBox1.Text And _
    Cells(mpStartRow - 1 + i, "D").Value = TextBox2.Text And _
    Cells(mpStartRow - 1 + i, "E").Value = TextBox3.Text And _
    Cells(mpStartRow - 1 + i, "F").Value = TextBox4.Text Then
    Cells(mpStartRow - 1 + i, "L").Value = Cells(mpStartRow - 1 + i, "A").Value
    Cells(mpStartRow - 1 + i, "M").Value = Cells(mpStartRow - 1 + i, "B").Value
    Cells(mpStartRow - 1 + i, "N").Value = Cells(mpStartRow - 1 + i, "G").Value
    Cells(mpStartRow - 1 + i, "O").Value = Cells(mpStartRow - 1 + i, "H").Value
    Cells(mpStartRow - 1 + i, "P").Value = Cells(mpStartRow - 1 + i, "I").Value
    Cells(mpStartRow - 1 + i, "Q").Value = Cells(mpStartRow - 1 + i, "J").Value
    End If
    Next i
    End Sub
    [/vba]
    It work well but the new problem that I faced is the output item arrange in separate rows,as it show in the pic below. I want there are no any blank rows between them as I signed in the pic below.

  14. #14
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Nader, no acknowledgement to Firefytr? you are not new to this forum or any other for that matter so you know the rules by now!

    You are very lucky to be having continued help!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    He only got it because I didn't know. Now I am applying my policy.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    I nthe first for the cross posting I did that for to reasons:
    1- I didn't have a soulition on this forum.
    2- I didn't understand how to send cross posting althouhg I read this article of cross postin http://www.excelguru.ca/node/7

    I know you are nerveous because of my ignoring. I didn't ignore anything but as I told you. I'm so sorry, but may you help me please and tell me how to cross posting when I have like this problem it's mean what I must to do by this topic to make it cross posting.




    the second:
    I made some details on the code ( by add j counter to the output item), and it succeed with me.

    [vba]
    Dim mpStartRow As Long
    Dim i As Long, j As Long
    j = 1
    mpStartRow = UsedRange.Cells(1, 1).Row
    For i = 1 To UsedRange.Rows.Count
    If Cells(mpStartRow - 1 + i, "C").Value = TextBox1.Text And _
    Cells(mpStartRow - 1 + i, "D").Value = TextBox2.Text And _
    Cells(mpStartRow - 1 + i, "E").Value = TextBox3.Text And _
    Cells(mpStartRow - 1 + i, "F").Value = TextBox4.Text Then

    Cells(j, "L").Value = Cells(mpStartRow - 1 + i, "A").Value
    Cells(j, "M").Value = Cells(mpStartRow - 1 + i, "B").Value
    Cells(j, "N").Value = Cells(mpStartRow - 1 + i, "G").Value
    Cells(j, "O").Value = Cells(mpStartRow - 1 + i, "H").Value
    Cells(j, "P").Value = Cells(mpStartRow - 1 + i, "I").Value
    Cells(j, "Q").Value = Cells(mpStartRow - 1 + i, "J").Value
    j = j + 1
    End If
    Next i
    [/vba]

    By the way xld why: mpStartRow and why mpStartRow - 1 + i

    Thank you again for help.

  17. #17
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Simon Lloyd
    Nader, no acknowledgement to Firefytr? you are not new to this forum or any other for that matter so you know the rules by now!

    You are very lucky to be having continued help!
    LOL! No worries Simon, s/he did in fact acknowledge me via pm. Even if s/he did not, that is fine, the information was given, which I'm plenty cool with. And yeah, don't make me break off a can of whoop a$$!! LOL!

    I don't think Nader was aware of the ramifications of cross-posting(s), which s/he seems to be well aware of now. Like I said, I'm very comfortable with Nader.

  18. #18
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Sorry Zack but I don't agree so easily with your point of view. The issue of cross posting without a linkage, has been raised with increasing frequency over the last couple of months. We all know why we should post the link...

    Either we value the assistance given by the members of this forum or we don't.

    Personally I believe that, those who cross post do so for the primary reason that they are dis-satisfied with the progress of their post. In this case, Nader's post was difficult to understand, from either its intent or layout, before the workbook was provided. Maybe if it had been presented in a different layout, the responce may have been different.

    I strongly believe that those who cross post without indicating a linkage should not be apologising the gunslinger ( ), but offer an apology to those members of the forum who have gone out of their way to assist.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  19. #19
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Quote Originally Posted by Aussiebear
    those who cross post do so for the primary reason that they are dis-satisfied with the progress of their post
    I have to disagree, when i started out with this terrible addiction i would post in a couple of forums because i wanted the answer NOW!, but finding that the world doesn't work like that and the fact that i had my wrists slapped (thanks Bob!) i stopped and realised why, from then on i only ever posed one question in one forum!
    Quote Originally Posted by Aussiebear
    I strongly believe that those who cross post without indicating a linkage should not be apologising the gunslinger ( ), but offer an apology to those members of the forum who have gone out of their way to assist.
    this i do agree with, i have nothing against crossposting but it does really annoy me when the appropriate protocol isn't followed, i'm a mod at another forum and new posters get a polite "shove" in the right direction, the more seasoned get a warning and the habitual get their posts locked (i rarely have to do the latter!).
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  20. #20
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Fair enough
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •