-1

I am trying to make something that would look like this:

enter image description here

In the table on the right there will be all the unique records which will be stored in a certain area. However some record may be existing in more areas, and this information can be taken from the list in column A and B. The macro should take each unique record in column D and search for it in Column A, every time it finds it, should copy the location/area in column B and pasted next to the unique record in the table. I think I could do this with a loop, but what I created in the code below does not really works.

The second challenge is to make it understand that in a location has been copy into the table, the new found location needs to be pasted in the next free cell of that same unique record.

I am aware my code is a little scare but I would appreciate even just advice on which direction I should be looking... Thanks in advance!

Sub searcharea()

    Dim UC As Variant, UCrng As Range, ra As Range

    Set UCrng = Range("F2:F6")

    For Each UC In UCrng

        Set ra = Cells.Find(What:=UC, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

        ra.Offset(0, 1).Copy Destination:=Range("E2")

    Next

End Sub
Error 1004
  • 7,877
  • 3
  • 23
  • 46
G_TTI
  • 37
  • 7

3 Answers3

0

I would suggest looping through all Rows (Columns A + B), e.g.:

For i = 1 to Rows.Count
'DoStuff
Next i

For each row, you copy the value of A into D, if it is not there already. You can access the values like this:

Cells(i, "A").Value
Cells(i, "B").Value

For finding values in a column, see here. If you found a duplicate, use another loop to check which column (E, F, G,..) in your specific row is the first empty one, and past the value of column B there.

PaulS
  • 850
  • 3
  • 17
  • Hi Paul, I am not sure I get it... I need to loop through column D and for each of them, look in column A for all duplicates. hence my attempt to use the find method. Also I need to copy the B column values in the table and not A column values. – G_TTI Jan 30 '20 at 13:21
  • 1
    Alright, I misunderstood your question. You can use the For loop to loop through all rows, and extract the Value of Column D. With the link I posted, you can search for each Value of Column D in Column A. The next step would be to paste the Value of Column B in the first empty Column behind D. I hope that clarifies my answer. – PaulS Jan 30 '20 at 13:25
0

Take a try:

Option Explicit

Sub test()

    Dim LastRowA As Long, LastRowD As Long, i As Long, rngColumn As Long
    Dim rng As Range

    With ThisWorkbook.Worksheets("Sheet1")

        LastRowD = .Cells(.Rows.Count, "D").End(xlUp).Row

        .Range("D2:J" & LastRowD).ClearContents

        LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 2 To LastRowA

            LastRowD = .Cells(.Rows.Count, "D").End(xlUp).Row

            Set rng = .Range("D1:D" & LastRowD).Find(.Range("A" & i).Value, LookIn:=xlValues, lookat:=xlWhole)

            If Not rng Is Nothing Then
                rngColumn = .Cells(rng.Row, .Columns.Count).End(xlToLeft).Column
                Cells(rng.Row, rngColumn + 1).Value = .Range("B" & i).Value
            Else
                .Range("D" & LastRowD + 1).Value = .Range("A" & i).Value
                .Range("E" & LastRowD + 1).Value = .Range("B" & i).Value
            End If

        Next i

    End With

End Sub
Error 1004
  • 7,877
  • 3
  • 23
  • 46
  • 1
    Thanks to both @error1004 I will give it a spin before the weekend! I will be back for feedbacks :) – G_TTI Jan 30 '20 at 15:10
0

I think this code will do what you want. Please try it.

Option Explicit

Sub SortToColumns()
    ' Variatus @STO 30 Jan 2020

    Dim WsS As Worksheet                    ' Source
    Dim WsT As Worksheet                    ' Target
    Dim Rng As Range
    Dim Fn As String, An As String          ' File name, Area name
    Dim Rls As Long
    Dim Rs As Long
    Dim Rt As Long, Ct As Long

    With ThisWorkbook                       ' change as required
        Set WsS = .Worksheets("Sheet1")     ' change as required
        Set WsT = .Worksheets("Sheet2")     ' change as required
    End With

    With WsT
        ' delete all but the caption row
        .Range(.Cells(2, 1), .Cells(.Rows.Count, "A").End(xlUp)).EntireRow.ClearContents
    End With

    Application.ScreenUpdating = False
    With WsS
        ' find last row of source data
        Rls = .Cells(.Rows.Count, "A").End(xlUp).Row

        For Rs = 2 To Rls                   ' start from row 2 (row 1 is caption)
            Fn = .Cells(Rs, "A").Value
            An = .Cells(Rs, "B").Value
            If FileNameRow(Fn, WsT, Rt) Then
                ' add to existing item
                With WsT
                    Ct = .Cells(Rt, .Columns.Count).End(xlToLeft).Column
                    Set Rng = .Range(.Cells(Rt, "B"), .Cells(Rt, Ct))
                End With
                With Rng
                    Set Rng = .Find(An, .Cells(.Cells.Count), xlValues, xlWhole, xlByRows, xlNext)
                End With
                ' skip if Area exists
                If Rng Is Nothing Then WsT.Cells(Rt, Ct + 1).Value = An
            Else
                ' is new item
                WsT.Cells(Rt, "A").Value = Fn
                WsT.Cells(Rt, "B").Value = An
            End If
        Next Rs
    End With
    Application.ScreenUpdating = True
End Sub

Private Function FileNameRow(Fn As String, _
                             WsT As Worksheet, _
                             Rt As Long) As Boolean
    ' Rt is a return Long
    ' return True if item exists (found)

    Dim Fnd As Range
    Dim Rng As Range
    Dim R As Long

    With WsT
        R = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set Rng = .Range(.Cells(2, "A"), .Cells(R, "A"))
        Set Fnd = Rng.Find(Fn, Rng.Cells(Rng.Cells.Count), xlValues, xlWhole, xlByRows, xlNext)

        If Fnd Is Nothing Then
            Rt = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, 2)
        Else
            Rt = Fnd.Row
            FileNameRow = True
        End If
    End With
End Function
Variatus
  • 14,293
  • 2
  • 14
  • 30
  • Thanks to both @variatus I will give it a spin before the weekend! I will be back for feedbacks :) – G_TTI Jan 30 '20 at 15:09