2

i tried on more ways to border the result of the variable V2 in Column E but it doenst work. You can see my tryings as a comment within the code. Has anyone an idea? Thanks


By the way is it possible to figure out End(xlUp) & End(xlDown) just by a Macro?

Update

This i could figure out by pressing shift +Cntrl + ArrowDown


Sub Duplicate()
    
    Dim nA As Long, nD As Long, i As Long, rc As Long
    Dim s As String, j As Long
    'Dim LastRow As Long
    'Dim rng2 As Range
    
    
    
        Range("A:A").Copy Range("D1")
        Range("B1").Copy Range("E1")
        Range("D:D").RemoveDuplicates Columns:=1, Header:=xlYes
        rc = Rows.Count
        nA = Cells(rc, 2).End(xlUp).Row 'grün
        nD = Cells(rc, 4).End(xlUp).Row 'gelb
       
           For i = 2 To nD 'gelb
            v = Cells(i, 4) 'gelb
            V2 = "" 'rot
            For j = 2 To nA 'grün
                If v = Cells(j, 1) Then 'orange
                    V2 = V2 & "," & Cells(j, 2) 'rot / ZU UMRANDEN
                End If
            Next j
            Cells(i, 5) = Mid(V2, 1) 'rot / 1 = Start erstes Zeichen
        Next i
        
        'LastRow = Cells(Rows.Count, 5).End(xlUp).Row
        'Range("E:E" & LastRow).Borders (xlInsideHorizontal)
        
     
        'Set rng2 = ActiveSheet.Range("E:E", ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp))
        '   rng2.HorizontalAlignment = xlLeft
    
        'With rng2.Borders()
        '       .LineStyle = xlContinuous
        '      .ColorIndex = 0
        '     .TintAndShade = 0
        '    .Weight = xlThin
            
    'End With
    
    Debug.Print
        
End Sub

Update

How it is right now

enter image description here

How it shall be

enter image description here

Update

final need had been like this

enter image description here

Sven
  • 101
  • 10
  • 1
    can you show a screenshot of how you expect it to look? – Pᴇʜ May 21 '21 at 09:28
  • 1
    sure i just added the pics, thanks. Important the sheets change by data ..so i can not just say border "E2:E6" – Sven May 21 '21 at 09:32
  • 2
    If I understand what you want to achieve then there is a better way to achieve it... – Siddharth Rout May 21 '21 at 09:34
  • you mean the code at all or specific my problem? – Sven May 21 '21 at 09:44
  • Posting an asnwer. One moment – Siddharth Rout May 21 '21 at 09:49
  • Do you only needs adding borders to the cells in the range "E2:E6"? Range to dynamically be determined, of course... If yes, your question is extremely wrong formulated and that's why I suppose that it is something else I am missing... Can you clarify this aspect? – FaneDuru May 21 '21 at 09:53
  • i think you dont miss something. Here in this exapmle yes E2:E6 has "just" do be bordered. But its not working. The point is that is doesnt seem to work just by what is usual like with x = Range("E:E").End(xlDown), and then Wth, End With...by my point of view but im also kind of new in this field – Sven May 21 '21 at 10:00
  • 1
    `Range("E:E").End(xlDown)` has no meaning in VBA. You should use `Range("E" & rows.count).End(xlUp).row` to determine the last row and build the range as `Range("E2:E" & lastRow)`. – FaneDuru May 21 '21 at 10:03

1 Answers1

3

From what I have understood, you are using remove duplicates on Col A values pasted in Column D (Code not there in question I guess) and then match the values with Col A to collate the values from Col B to create a summary kind of thing.

If my understanding is correct then there is a simpler way to do it.

LOGIC

  1. Identify your range and store the values in an array. This is to speed things up. To identify the range, you can find the last row as shown HERE and then use that range.

  2. Create a unique collection of values from Col A.

  3. Define a second array based on unique values.

  4. Compare the unique values with values in Col A and collate the values from Col B.

  5. Clear Column D and E for output and finally output the array there.

  6. Identify the final range to work with. You can then add color, border etc to that range.

CODE

I have commented the code but if you still have problems understanding it then do let me know.

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long, j As Long
    Dim MyAr As Variant, OutputAr As Variant
    Dim col As New Collection
    Dim itm As Variant
    Dim tmpString As String
    Dim rng As Range
    
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
    
    With ws
        '~~> Find the last row in column A
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        '~~> Store the Col A and B values in an array
        MyAr = .Range("A1:B" & lRow).Value2
        
        '~~> Loop through the array and get unique values from Col A
        For i = LBound(MyAr) To UBound(MyAr)
            On Error Resume Next
            col.Add MyAr(i, 1), CStr(MyAr(i, 1))
            On Error GoTo 0
        Next i
    End With
    
    '~~> Define your output array based on unique values found
    ReDim OutputAr(1 To col.Count, 1 To 2)
      
    j = 1
    '~~> Compare the unique values with values in Col `A`
    '~~> and collate the values from Col `B`
    For Each itm In col
        OutputAr(j, 1) = itm
        
        tmpString = ""
        
        For i = LBound(MyAr) To UBound(MyAr)
            If MyAr(i, 1) = itm Then
                tmpString = tmpString & "," & MyAr(i, 2)
            End If
        Next i
        
        OutputAr(j, 2) = "'" & Mid(tmpString, 2)
        j = j + 1
    Next itm
    
    With ws
        '~~> Clear Col D and E for output
        .Columns("D:E").Clear
        
        '~~> Output the array
        .Range("D1").Resize(col.Count, 2).Value = OutputAr
        
        '~~> This is the final range
        Set rng = .Range("D1:E" & col.Count)
        
        With rng
            MsgBox .Address
            '
            '~~> Do what you want with the range here
            '
        End With
    End With
End Sub

IN ACTION

enter image description here

An example to add borders

With rng
    .Borders(xlDiagonalDown).LineStyle = xlNone
    .Borders(xlDiagonalUp).LineStyle = xlNone
    
    For i = 7 To 12
        With .Borders(i)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
    Next i
End With

Output

enter image description here

Similarly to center align the text

With rng
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

Output

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 1
    @Siddarth Rout....well what shall i say..its perfect, solves also the problem with the comma before the first entry...i'm blasted i mean i dont get it how you can do something likes this in 10 mins. I just changed ws to Activesheet ..and now i'm figuring out your updates with the frame..Thanks, really dont know what to say – Sven May 21 '21 at 10:12
  • 1
    Glad it worked out. I would recommend not using `ActiveSheet`. Use the actual sheet name like `Set ws = Sheets("Sheet1")` or the codename like `Set ws = Sheet1`. Because if the relevant sheet is not active then you will not get the desired results. – Siddharth Rout May 21 '21 at 10:16
  • it works perfectally,not joking also with the Center & Borders. The problem about if i do not use ActiveSheet is 1. that in the end the sheet has to be used by a couple of persons, who shall 2. load external data and modify these in Excel for which i create 3 Buttons = 3 Steps...this code here was the 2nd Step. I have to solve this how its easiest but i think rewrite the code in VBA to which each time this code has be assigned to a specific sheet is not that good idea? The 3rd Step is that submit the modyfied data to a Word sheet – Sven May 21 '21 at 10:28
  • 1
    If the data is loaded in the relevant sheet and it is active then there shouldn't be a problem in using activesheet especially if the code is called from a button from that sheet. – Siddharth Rout May 21 '21 at 10:32
  • i have a last question if i have further values in column C&D, next to column A & B, would i understand it right that i "just" have to collate further values to A ? '~~> and collate the values from Col `B` so i just copy the code under the comment, modify it to the specific range and this i do 2 times (one time for C, one time for D)? – Sven May 21 '21 at 10:46
  • If you want values from there as well then yes. If not, then ignore those columns. But remember to specify the correct columns for output else if you use `.Columns("D:E").Clear` then existing data will be lost. – Siddharth Rout May 21 '21 at 10:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232693/discussion-between-sven-and-siddharth-rout). – Sven May 21 '21 at 11:14