I have this code to check duplicates, If it find duplicates (or more) in cell L, is it possible to copy the values from cells in the K column into ONE cell?
Sub check_duplicates()
Dim x As Long
Dim LastRow As Long
Dim rng As String
LastRow = Range("L65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("L2:L" & x), Range("L" & x).Value) > 1 Then
Range("L" & x).Copy
End If
Next x
End Sub
I hope is what you want to do, let me know.
Sub Test()
Dim lastrow As Long
lastrow = Range("L" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
lastrow = Range("L" & Rows.Count).End(xlUp).Row
For j = i + 1 To lastrow
If Range("L" & j).Value = Range("L" & i).Value Then
If Not IsEmpty(Range("K" & i)) Then
Range("K" & i) = Range("K" & i) & "," & " " & Range("L" & j)
Rows(j).EntireRow.Delete
Else
Range("K" & i) = Range("L" & j)
End If
j = j - 1
End If
Next j
Next i
End Sub
Related
Hi Stackoverflow troops,
My time has finally come to ask a question rather than rely on all the answers as I'm stuck!
I've got a dataset showing the input table on the left & the output table on the right. The first date range interates as I'd expect, however I can't get the next rows to iterate their date ranges or indeed the other cells to be populated. Step 1 is to get the dates to iterate.
https://i.stack.imgur.com/NzO4l.jpg
The code:
Sub GenerateDates()
Application.ScreenUpdating = False
Dim i, j As Integer
Dim finalRow, finalRow2, startRow As Long
finalRow = Range("A" & Rows.Count).End(xlUp).Row
finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
For i = 2 To 4 'finalRow
finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
startRow = finalRow2 + 1
Range("M" & startRow) = Range("A" & i)
j = i
Days = Int((Range("B" & i) - Range("A" & i)))
Do While j < 2 + Days
j = j + 1
Range("M" & j) = Range("M" & j - 1) + 1
Loop
Range("N" & startRow) = Range("C" & i)
Range("O" & startRow) = ""
Range("P" & startRow) = Range("F" & i)
Range("Q" & startRow) = Range("G" & i)
Next i
End Sub
Target output sample:
https://i.stack.imgur.com/LLaO2.jpg
Resolved the conundrum myself. :-)
Sub GenerateDates()
Application.ScreenUpdating = False
Dim i, j, numDays As Integer
Dim finalRow, finalRow2 As Long
Dim startDate, endDate As Date
Dim strType, strName, strCountry As String
finalRow = Range("A" & Rows.Count).End(xlUp).Row
finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
If finalRow2 > 1 Then
Range("M2:Q" & finalRow2).Select
Selection.ClearContents
End If
Range("A1").Select
For i = 2 To finalRow
startDate = Range("A" & i).Value
endDate = Range("B" & i).Value
startDate = startDate - 1
strType = Range("C" & i).Value
numDays = Range("D" & i).Value
strName = Range("F" & i).Value
strCountry = Range("G" & i).Value
Do While startDate < endDate
finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
finalRow2 = finalRow2 + 1
startDate = DateAdd("d", 1, startDate)
Range("M" & finalRow2).Value = startDate
Range("N" & finalRow2).Value = strType
'Function to split decimal numDays
Range("P" & finalRow2).Value = strName
Range("Q" & finalRow2).Value = strCountry
Loop
Next i
Application.ScreenUpdating = True
End Sub
I have a table which has around 50 columns. Below function works fine. But how can I put a loop across the columns? I cannot write formulas for each column. I cannot use WorksheetFunction.Sum. I require Sum formula to be added in the cell.
Sub Sum_Last_Row()
With Worksheets("Revenue_forecast_table")
Dim LastRow As Long
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("E" & LastRow + 1).Formula = "=SUM(E5:E" & LastRow & ")"
.Range("F" & LastRow + 1).Formula = "=SUM(F5:F" & LastRow & ")"
.Range("G" & LastRow + 1).Formula = "=SUM(G5:G" & LastRow & ")"
.Range("H" & LastRow + 1).Formula = "=SUM(H5:H" & LastRow & ")"
.Range("I" & LastRow + 1).Formula = "=SUM(I5:I" & LastRow & ")"
.Range("J" & LastRow + 1).Formula = "=SUM(J5:J" & LastRow & ")"
End With
End Sub
You can populate the whole row at once and the formula will auto-adjust:
Sub Sum_Last_Row()
Dim NextRow As Long
With Worksheets("Revenue_forecast_table")
NextRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
.Range("E" & NextRow & ":J" & NextRow).Formula = _
"=SUM(E5:E" & NextRow - 1 & ")"
End With
End Sub
I want to write a Macro to Identify duplicate of one value with different values in excel .
if you see the image below there are 2 cluster which have different state & Cities highlighted in yellow colour. i want these cluster # should come in Sheet2 in A Column.
You could try:
Option Explicit
Sub test()
Dim i As Long, y As Long, w As Long, LastRow As Long, LastRow2 As Long
Dim Cluster1 As String, Cluster2 As String, FullDesc1 As String, FullDesc2 As String
Dim rng As Range
Dim Diff As Boolean
'Change sheet name if needed
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = .Range("A2:A" & LastRow)
For i = LastRow To 2 Step -1
If .Range("A" & i).Value <> "" Then
Cluster1 = .Range("A" & i).Value
If WorksheetFunction.CountIf(rng, Cluster1) > 1 Then
FullDesc1 = Cluster1 & "_" & .Range("B" & i).Value & "_" & .Range("C" & i).Value
Diff = False
For y = LastRow To 2 Step -1
If y < i Then
Cluster2 = .Range("A" & y).Value
FullDesc2 = Cluster2 & "_" & .Range("B" & y).Value & "_" & .Range("C" & y).Value
If (Cluster1 = Cluster2) And (FullDesc1 <> FullDesc2) Then
Diff = True
Exit For
Else
Diff = False
End If
End If
Next y
If Diff = True Then
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For w = LastRow To 2 Step -1
If .Range("A" & w).Value = Cluster1 Then
LastRow2 = ThisWorkbook.Worksheets("Sheet2").Cells(ThisWorkbook.Worksheets("Sheet2").Rows.Count, "A").End(xlUp).Row
.Range("A" & w & ":C" & w).Cut ThisWorkbook.Worksheets("Sheet2").Range("A" & LastRow2 + 1)
.Rows(w).EntireRow.Delete
End If
Next w
End If
End If
End If
Next i
End With
End Sub
This is a follow on from How do I get all the different unique combinations of 3 columns using VBA in Excel?
It almost what i need, however, my requirements is that it sums the third column which will contain figures instead of yes/no
Sub sample()
Dim ws As Worksheet
Dim lRow As Long, i As Long, j As Long
Dim col As New Collection
Dim Itm
Dim cField As String
Const deLim As String = "#"
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lRow
cField = .Range("A" & i).Value & deLim & _
.Range("B" & i).Value & deLim & _
.Range("C" & i).Value
On Error Resume Next
col.Add cField, CStr(cField)
On Error GoTo 0
Next i
i = 2
.Range("A1:C1").Copy .Range("F1")
.Range("I1").Value = "Count"
For Each Itm In col
.Range("F" & i).Value = Split(Itm, deLim)(0)
.Range("G" & i).Value = Split(Itm, deLim)(1)
.Range("H" & i).Value = Split(Itm, deLim)(2)
For j = 2 To lRow
cField = .Range("A" & j).Value & deLim & _
.Range("B" & j).Value & deLim & _
.Range("C" & j).Value
If Itm = cField Then nCount = nCount + 1
Next
.Range("I" & i).Value = nCount
i = i + 1
nCount = 0
Next Itm
End With
End Sub
This code was originally added by
Siddharth Rout
try this (follows comments)
Option Explicit
Sub Main()
Dim i As Long
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
For i = 4 To Range("A" & Rows.Count).End(xlUp).Row '<-- change 4 and "A" to your data actual upleftmost cell row and column
dict(cells(i, 1).Value & "|" & cells(i, 2).Value) = dict(cells(i, 1).Value & "|" & cells(i, 2).Value) + cells(i, 3).Value '<--| change 3 to your actual "column to sum up" index
Next
With Range("G3").Resize(dict.Count) '<-- change "G3" to your actual upleftmost cell to start writing output data from
.Value = Application.Transpose(dict.Keys)
.TextToColumns Destination:=.cells, DataType:=xlDelimited, Other:=True, OtherChar:="|"
.Offset(, 2).Resize(dict.Count).Value = Application.Transpose(dict.Items) '<--| change 2 to your actual column offset where to start writing summed values form
End With
End Sub
I have a sheet:
I am trying to write code to be able to combine multiple values into one row, I need to sum the values from columns, B, C and D.
My aim is to be able to press a button and I have all of my duplicate values removed, but before this, the numerical values in the adjacent columns are summed into the single version.
So far I have removed the duplicates from the column:
Sheets("Sheet4").Select
With Columns("A:A")
.Replace What:="mobile", Replacement:=""
End With
Previous code should do your job. It may need a fine tuning but idea would work. Do not forget to make proper addressing of worksheets for your ranges. I did not do it. This will work on the active sheet currently.
Update: Updated with worksheet addresses.
Dim ws As Worksheet
Dim LastRow As Long
Dim S_Value As String
Set ws = Sheets("Sheet1")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
i = 2
While i <= LastRow
S_Value = ws.Range("A" & i).Value
j = i + 1
While j <= LastRow
If ws.Range("A" & j).Value = S_Value Then
ws.Range("B" & i).Value = ws.Range("B" & i).Value + ws.Range("B" & j).Value
ws.Range("C" & i).Value = ws.Range("C" & i).Value + ws.Range("C" & j).Value
ws.Range("D" & i).Value = ws.Range("D" & i).Value + ws.Range("D" & j).Value
ws.Rows(j & ":" & j).EntireRow.Delete
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
j = j - 1
End If
j = j + 1
Wend
i = i + 1
Wend
Here you go,
Sub SumCount()
Dim s, c, sm
Dim Rws As Long, Rng As Range
Rws = Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range(Cells(2, 2), Cells(Rws, 4))
s = InputBox("What Number to Find?")
c = Application.WorksheetFunction.CountIf(Rng, s)
sm = s * c
MsgBox sm
End Sub