Patriot2879
Well-known Member
- Joined
- Feb 1, 2018
- Messages
- 1,259
- Office Version
- 2010
- Platform
- Windows
Hi please can you help me? i have the code below where the first part works where it removes the duplicates. But with the cells left over with data i am trying to combine these into 1 cell, but this is not working. Please can you help? This is the snippet of code where i am trying to combine the data from the cells into one
The below is the whole code.
Code:
Range("A6").cell.Value = Range("A1").cell.Value & Range("B1").cell.Value & Range("C1").cell.Value & Range("D1").cell.Value & Range("E1").cell.Value
VBA Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim cell As Range
Dim duplicate As Boolean
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet's name
' Loop through each cell in the range A1 to E1
For Each cell In ws.Range("A1:E1")
duplicate = False ' Reset duplicate flag for each cell
' Check if the current cell is not empty
If cell.Value <> "" Then
' Loop through each cell in the same row
For Each c In ws.Range(cell.Offset(0, 1), cell.Offset(0, 4))
' Check if the value matches with any other cell in the row
If c.Value = cell.Value Then
duplicate = True ' Flag as duplicate
Exit For ' Exit the loop once a duplicate is found
End If
Next c
' If duplicate found, clear the current cell
If duplicate Then cell.ClearContents
End If
Next cell
Range("A6").cell.Value = Range("A1").cell.Value & Range("B1").cell.Value & Range("C1").cell.Value & Range("D1").cell.Value & Range("E1").cell.Value
End Sub