Combine duplicate rows in a loop vba - excel

I want to combine duplicate rows with the same A and C columns values and sum their cells values for the column B (by adding the value of the textbox2 from the duplicate to the original). My problem is about the condition of the "If" in the Loop. It doesn't consider those conditions when I have duplicates and just add a new row. Is there a better way to do this?
Private Sub CommandButton1_Enter()
ActiveSheet.Name = "Sheet1"
Dim lastrow As Long
With Sheets("Sheet2")
lastrow = .Cells(Rows.Count, "H").End(xlUp).Row
For x = lastrow To 3 Step -1
For y = 3 To lastrow
If .Cells(x, 1).Value = .Cells(y, 1).Value And .Cells(x, 3).Value = .Cells(y, 3).Value And x > y Then
.Cells(y, 8).Value = .Cells(y, 8).Value + TextBox2.Text
.Cells(y, 2).Value = .Cells(y, 2).Value + TextBox2.Text
.Rows(lastrow).EntireRow.Delete
Else
.Cells(lastrow + 1, 8).Value = TextBox2.Text
.Cells(lastrow + 1, 2).Value = TextBox2.Text
.Cells(lastrow + 1, 1).Value = TextBox1.Text
.Cells(lastrow + 1, 3).Value = TextBox3.Text
Exit For
End If
Next y
Next x
End With
End Sub
Here's a picture of the data
There's no blank cell in the column H (I changed the color of the font to make it invisible).

Create a primary key by joining the 2 columns with tilde ~ and use a Dictionary Object to locate duplicates.
Option Explicit
Private Sub CommandButton1_Click()
Dim wb As Workbook, ws As Worksheet
Dim iLastRow As Long, iRow As Long, iTarget As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet2")
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
Dim dict As Object, sKey As String
Set dict = CreateObject("Scripting.Dictionary")
' build dictionary and
' consolidate any existing duplicates, scan up
For iRow = iLastRow To 3 Step -1
' create composite primary key
sKey = LCase(ws.Cells(iRow, 1).Value) & "~" & Format(ws.Cells(iRow, 3).Value, "yyyy-mm-dd")
If dict.exists(sKey) Then
iTarget = dict(sKey)
' summate and delete
ws.Cells(iTarget, 2) = ws.Cells(iTarget, 2) + ws.Cells(iRow, 2)
ws.Cells(iTarget, 8) = ws.Cells(iTarget, 8) + ws.Cells(iRow, 8)
ws.Rows(iRow).EntireRow.Delete
Else
dict(sKey) = iRow
End If
Next
' add new record from form using dictionary to locate any existing
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
sKey = LCase(TextBox1.Text) & "~" & Format(DateValue(TextBox3.Text), "yyyy-mm-dd")
If dict.exists(sKey) Then
iTarget = dict(sKey)
ws.Cells(iTarget, 2) = ws.Cells(iTarget, 2) + TextBox2.Text
ws.Cells(iTarget, 8) = ws.Cells(iTarget, 8) + TextBox2.Text
Else
iTarget = iLastRow + 1
ws.Cells(iTarget, 1) = TextBox1.Text
ws.Cells(iTarget, 2) = TextBox2.Text
ws.Cells(iTarget, 3) = TextBox3.Text
ws.Cells(iTarget, 8) = TextBox2.Text
End If
End Sub

Related

Match rows for value in specific columns and paste matched/unmatched rows in new sheet

I got data in sheet1 and sheet2, which i want to copy and paste in sheet3. That is already done. So next i want to match rows, by checking column C, D, E, H and I. The C and H column value is integer and the rest is text/strings.
If two rows match, then i want to copy and paste one of the lines in a new third sheet, and add the integer difference from column H in column H (The difference will be 0 if the lines match in all columns)
If the two rows dont match, copy and paste one of the lines in a new fourth sheet, and add the integer difference from column H in column H
The code so far:
Sub CopyPasteSheet()
Dim mySheet, arr
arr = Array("Sheet1", "Sheet2")
Const targetSheet = "Sheet3"
Application.ScreenUpdating = False
For Each mySheet In arr
Sheets(mySheet).Range("A1").CurrentRegion.Copy
With Sheets(targetSheet)
.Range("A1").Insert Shift:=xlDown
If mySheet <> arr(UBound(arr)) Then .Rows(1).Delete xlUp
End With
Next mySheet
Application.ScreenUpdating = True
End Sub
Code so far, but i receive a code error "Application-defined or object-defined error". It does copy the rows which match into a new sheet and state the difference as 0 in column H, but it doesn't work for the ones that dont match.
Sub MatchRows()
Dim a As Variant, b As Variant, c As Variant, d As Variant
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim dic As Object, ky As String
Set dic = CreateObject("Scripting.Dictionary")
a = Sheets("Sheet1").Range("A1:I" & Sheets("Sheet1").Range("H" & Rows.Count).End(3).Row).Value
b = Sheets("Sheet2").Range("A1:I" & Sheets("Sheet2").Range("H" & Rows.Count).End(3).Row).Value
ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
ReDim d(1 To UBound(a, 1), 1 To UBound(a, 2))
For i = 1 To UBound(b, 1)
ky = b(i, 3) & "|" & b(i, 4) & "|" & b(i, 5) & "|" & b(i, 9)
dic(ky) = i
Next
For i = 2 To UBound(a, 1)
ky = a(i, 3) & "|" & a(i, 4) & "|" & a(i, 5) & "|" & a(i, 9)
If dic.exists(ky) Then
j = dic(ky)
If a(i, 8) = b(j, 8) Then
k = k + 1
For n = 1 To UBound(a, 2)
c(k, n) = a(i, n)
Next
c(k, 8) = 0
Else
m = m + 1
For n = 1 To UBound(a, 2)
d(k, n) = a(i, n)
Next
d(k, 8) = a(i, 8) - b(j, 8)
End If
End If
Next
Sheets("Sheet3").Range("A" & Rows.Count).End(3)(2).Resize(k, UBound(a, 2)).Value = c
Sheets("Sheet4").Range("A" & Rows.Count).End(3)(2).Resize(m, UBound(a, 2)).Value = d
End Sub
Unless the performance is too slow remove the complexity of arrays by writing to the output sheets one line at a time.
Update - copy complete line
Option Explicit
Sub MatchRows2()
Dim dic As Object, key As String
Set dic = CreateObject("Scripting.Dictionary")
Dim wb As Workbook
Dim ws As Worksheet, ws3 As Worksheet, ws4 As Worksheet
Dim iLastRow As Long, s As String, diff As Long
Dim iRow3 As Long, iRow4 As Long, i As Long, t0 As Single
Dim rng As Range
t0 = Timer
s = "|"
Set wb = ThisWorkbook
' sheet 2
Set ws = wb.Sheets("Sheet2")
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
For i = 2 To iLastRow
key = ws.Cells(i, "C") & s & ws.Cells(i, "D") _
& s & ws.Cells(i, "E") & s & ws.Cells(i, "I")
If dic.exists(key) Then
MsgBox "Duplicate key '" & key & "'", vbCritical, "Sheet2 Row " & i
Exit Sub
Else
dic.Add key, ws.Cells(i, "H")
End If
Next
Debug.Print dic.Count
' results
Set ws3 = wb.Sheets("Sheet3")
iRow3 = ws3.Cells(Rows.Count, "A").End(xlUp).Row
Set ws4 = wb.Sheets("Sheet4")
iRow4 = ws4.Cells(Rows.Count, "A").End(xlUp).Row
'sheet 1
Application.ScreenUpdating = False
Set ws = wb.Sheets("Sheet1")
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
For i = 2 To iLastRow
key = ws.Cells(i, "C") & s & ws.Cells(i, "D") _
& s & ws.Cells(i, "E") & s & ws.Cells(i, "I")
If dic.exists(key) Then
diff = ws.Cells(i, "H") - dic(key)
If diff = 0 Then
iRow3 = iRow3 + 1
Set rng = ws3.Cells(iRow3, "A")
Else
iRow4 = iRow4 + 1
Set rng = ws4.Cells(iRow4, "A")
End If
ws.Rows(i).Copy rng
rng.Offset(0, 7).Value = diff ' col H
End If
Next
Application.ScreenUpdating = True
MsgBox "Done in " & Format(Timer - t0, "0.0 secs"), vbInformation
End Sub

For Each loop on filtered data returning 0 results, no errors

I need to generate a sheet of values out of a database between dates that the user selects. The date is in column 2 of the database, but I need the whole row for every date in this range. I got some advice to use a For Each instead to more easily use the SpecialCells(xlCellTypeVisible). While I am no longer getting any errors I also get no data in my product worksheet. Could someone tell me why I am not returning data?
Sub Generate()
Dim g As Integer
Dim h As Integer
Dim datemin As String
Dim datemax As String
datemin = CDbl(CDate(Sheets("start").Cells(15, 8)))
datemax = CDbl(CDate(Sheets("start").Cells(15, 9)))
Worksheets("Database").Range("A1").AutoFilter Field:=10, Criteria1:=">=" & datemin, _
Operator:=xlAnd, Criteria2:="<=" & datemax
g = 0
For Each Row In Worksheets("database").Range("A1")
g = g + 1
If Cells(g, 1).SpecialCells(xlCellTypeVisible) = True And Cells(g, 1) <> "" Then
Sheets("product").Activate
Dim NextRow As Long
NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 10
'fill KPI
Cells(NextRow, 1) = Format(Sheets("database").Cells(g, 1), "mm/dd/yyyy") 'Date1
Cells(NextRow, 2) = Format(Sheets("database").Cells(g, 2), "mm/dd/yyyy") 'Date2
Cells(NextRow, 3) = Sheets("database").Cells(g, 3) 'value1
Cells(NextRow, 4) = Sheets("database").Cells(g, 4) 'value2
Cells(NextRow, 6) = Sheets("database").Cells(g, 5) 'value3
Cells(NextRow, 9) = Sheets("database").Cells(g, 8) 'comment
Cells(NextRow, 13) = Sheets("database").Cells(g, 6) 'person
Else
Exit For
End If
Next
End Sub
You are only 'looping' through one cell - A1.
If you want to use a loop for this try looping through all the rows on the database and checking if they are visible or not.
If they are visible then copy the relevant data to the other sheet.
Sub Generate()
Dim rngDst As Range
Dim rngSrc As Range
Dim datemin As String
Dim datemax As String
Dim g As Integer
Dim h As Integer
datemin = CDbl(CDate(Sheets("start").Cells(15, 8)))
datemax = CDbl(CDate(Sheets("start").Cells(15, 9)))
Worksheets("Database").Range("A1").AutoFilter Field:=10, Criteria1:=">=" & datemin, _
Operator:=xlAnd, Criteria2:="<=" & datemax
Set rngSrc = Worksheets("Database").Range("A2")
Set rngDst = Worksheets("Product").Range("A11")
Do
If Not rngSrc.EntireRow.Hidden And rngSrc.Value <> "" Then
'fill KPI
rngDst.Value = Format(rngSrc.Value, "mm/dd/yyyy") 'Date1
rngDst.Offset(, 1).Value = Format(rngSrc.Offset(, 1).Value, "mm/dd/yyyy") 'Date2
rngDst.Offset(, 2).Value = rngSrc.Offset(, 2).Value 'value1
rngDst.Offset(, 3).Value = rngSrc.Offset(, 3).Value 'value2
rngDst.Offset(, 5).Value = rngSrc.Offset(, 4).Value 'value3
rngDst.Offset(, 8).Value = rngSrc.Offset(, 7).Value 'comment
rngDst.Offset(, 12).Value = rngSrc.Offset(, 5).Value 'person
Set rngDst = rngDst.Offset(1, 0)
End If
Set rngSrc = rngSrc.Offset(1, 0)
Loop Until rngSrc = ""
End Sub

Copying the data above blank cell to the last blank cell before meeting the next cell contains

Can someone help me if this is possible to do?
Logic is: If ColA = 1 and ColC >=1 then it should copy the entire row and insert new row below the last blank cell before meeting the next cell that contains then 1 will become 0.
Raw:
Final output should be:
I tried to put it as text but it doesn't seem right. the code i have for now is only this, its my first project tho. my code is still incomplete as i don't know what to do next. i tried a lot of codes but not working. here's the code:
Dim asd As Integer
Dim LastRow As Long
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For zxc = 2 To C
If Cells(zxc, "A").Value = 1 And Cells(zxc, "C").Value >= 1 Then
asd = asd + 1
End If
Next zxc
Dim AddCountRow As Long
AddCountRow = LastRow + asd
For i = 2 To AddCountRow
Dim A As Long
A = Worksheets("Sheet1").Cells(i, "A").Value
Dim B As Long
B = Worksheets("Sheet1"). Cells(i + 1, "D"). Value
If A >= 1 And B >= 1 Then
Cells(i + 1, "A").EntireRow.Insert
i = i + 1
End If
Next i
End Sub
Thank you so much guys!
This is a different approach. Considering maybe you have data below and
lastrow could not be reliable.
Look for the <<< Customize this >>> where I set the first cell where you have the header.
This code covers the data in the sample image:
Sub CopyInsertRows()
Dim colAValue As String
Dim colBValue As String
Dim colCValue As String
Dim colDValue As String
Dim initialCell As String
Dim rowCounter As Long
' <<< Customize this >>>
initialCell = "A4"
' Loop through all cells
For rowCounter = 2 To Rows.Count
If Range(initialCell).Cells(rowCounter, 1).Value <> vbNullString Then
colAValue = Range(initialCell).Cells(rowCounter, 1).Value
colBValue = Range(initialCell).Cells(rowCounter, 2).Value
colCValue = Range(initialCell).Cells(rowCounter, 3).Value
colDValue = Range(initialCell).Cells(rowCounter, 4).Value
ElseIf Range(initialCell).Cells(rowCounter, 1).Value = vbNullString And Range(initialCell).Cells(rowCounter + 1, 1).Value <> vbNullString Then
Range(initialCell).Cells(rowCounter + 1).EntireRow.Insert
Range(initialCell).Cells(rowCounter + 1, 1).Value = "0"
Range(initialCell).Cells(rowCounter + 1, 2).Value = colBValue
Range(initialCell).Cells(rowCounter + 1, 3).Value = colCValue
Range(initialCell).Cells(rowCounter + 1, 4).Value = colDValue
rowCounter = rowCounter + 1
End If
If Range(initialCell).Cells(rowCounter, 4).Value = vbNullString Then
Range(initialCell).Cells(rowCounter, 1).Value = "0"
Range(initialCell).Cells(rowCounter, 2).Value = colBValue
Range(initialCell).Cells(rowCounter, 3).Value = colCValue
Range(initialCell).Cells(rowCounter, 4).Value = colDValue
Exit For
End If
Next rowCounter
End Sub
This code covers the data in the sample linked file:
Sub CopyInsertRows()
Dim sourceRow As Range
Dim initialCell As String
Dim dateColumnLetter As String
Dim dateColumnNumber As Integer
Dim rowCounter As Long
' <<< Customize this >>>
initialCell = "A1" ' First cell of header row
dateColumnLetter = "AA" ' Where
' Get column number
dateColumnNumber = Range(dateColumnLetter & 1).Column
' Loop through all cells
For rowCounter = 2 To Rows.Count
If Range(initialCell).Cells(rowCounter, 1).Value <> vbNullString Then
' Store row values
Set sourceRow = Range(initialCell).Range("A" & rowCounter & ":" & dateColumnLetter & rowCounter)
ElseIf Range(initialCell).Cells(rowCounter, 1).Value = vbNullString And Range(initialCell).Cells(rowCounter + 1, 1).Value <> vbNullString Then
' Insert new row
Range(initialCell).Cells(rowCounter + 1).EntireRow.Insert
' Duplicate source row
Range(initialCell).Range("A" & rowCounter + 1 & ":" & dateColumnLetter & rowCounter + 1).Value = sourceRow.Value
' Replace first cell
Range(initialCell).Range("A" & rowCounter + 1).Value = "0"
rowCounter = rowCounter + 1
End If
If Range(initialCell).Cells(rowCounter, dateColumnNumber).Value = vbNullString Then
' Duplicate source row
Range(initialCell).Range("A" & rowCounter & ":Y" & rowCounter).Value = sourceRow.Value
' Replace first cell
Range(initialCell).Range("A" & rowCounter + 1).Value = "0"
Exit For
End If
Next rowCounter
End Sub
You will be inserting rows so work from the bottom up.
Sub addLines()
Dim i As Long, lr As Long, n As Long
With Worksheets("sheet5")
'collect last data row
lr = .Cells(.Rows.Count, "D").End(xlUp).Row + 1
'loop through the rows backwards, inserting rows and transferring values
For i = lr To 3 Step -1
If i = lr Or .Cells(i, "A") <> vbNullString Then
n = Application.Match(1E+99, .Range("A:A").Resize(i - 1, 1))
.Cells(i, "A").Resize(1, 4).Insert Shift:=xlDown
.Cells(i, "A").Resize(1, 4) = .Cells(n, "A").Resize(1, 4).Value
.Cells(i, "A") = 0
End If
Next i
End With
End Sub

Error 381 populating listbox from a sheet table

I am getting all the time error 381.. What is there wrong? If i use only 1 column it works, if i add 2nd and more it stops working.
I try to populate my rows which compile with "if statement".
it stops each time to work at 2nd column.
UserForm + some Data:
https://drive.google.com/open?id=1hfCAu2m7C4kISSPJSvyjWc-TvxBr-fOO
2nd Version of code:
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
With ListBoxAbg
.Clear
.ColumnCount = 2
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
.AddItem
.List(i - 1, 0) = ws.Cells(i, 1).Value
.List(i - 1, 1) = ws.Cells(i, 3).Value
End If
Next i
End With
End Sub
....
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
AbgeListField.Clear
AbgeListField.ColumnCount = 7
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
AbgeListField.AddItem ws.Cells(i, 1).Value
AbgeListField.List(i - 1, 1) = ws.Cells(i, 2).Value
AbgeListField.List(i - 1, 2) = ws.Cells(i, 3).Value
AbgeListField.List(i - 1, 3) = ws.Cells(i, 4).Value
AbgeListField.List(i - 1, 4) = ws.Cells(i, 5).Value
AbgeListField.List(i - 1, 5) = ws.Cells(i, 6).Value
AbgeListField.List(i - 1, 6) = ws.Cells(i, 7).Value
End If
Next i
End Sub
i found the answer in that post:
https://social.msdn.microsoft.com/Forums/office/en-US/f5619db9-be72-41e3-a353-54ebb021f936/runtime-error-381-could-not-set-the-list-property-invalid-property-array-index?forum=exceldev
i added new dim nxtItme As Long. it works perfect now:
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Dim nxtItem As Long
Set ws = E1G
nxtItem = 0
With ListBoxAbg
.Clear
.ColumnCount = 6
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
.AddItem
.List(nxtItem, 0) = ws.Cells(i, 1).Value
.List(nxtItem, 1) = ws.Cells(i, 3).Value
.List(nxtItem, 2) = ws.Cells(i, 4).Value
.List(nxtItem, 3) = ws.Cells(i, 5).Value
.List(nxtItem, 4) = ws.Cells(i, 6).Value
nxtItem = nxtItem + 1
End If
Next i
End With
End Sub

Not able to operate on dynamic non empty cells

i have to sort data from sheet1 to sheet2 with reference to non-empty cell in column A. and
i have written code for it as below:
Sub polo()
Dim lastrow As Long
lastrow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
j = 2
For i = 1 To lastrow
If Sheets("Sheet1").Cells(i, 1).Value = "" Then
Sheets("Sheet2").Cells(j, 2).Value = Sheets("Sheet1").Cells(i, 1).Offset(, 1).Value
Sheets("Sheet2").Cells(j, 4).Value = Sheets("Sheet1").Cells(i - 1, 2).Value
j = j + 1
End If
Next i
End Sub
But the problem is, i am getting result as in column D of sheet2.
I want result as shown in column E.
Please help.
Try this version:
Sub polo()
Dim lastrow As Long
Dim sTemp as String
lastrow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
j = 2
For i = 1 To lastrow
If Sheets("Sheet1").Cells(i, 1).Value = "" Then
Sheets("Sheet2").Cells(j, 2).Value = Sheets("Sheet1").Cells(i, 1).Offset(, 1).Value
Sheets("Sheet2").Cells(j, 4).Value = stemp
j = j + 1
Else
stemp = Sheets("Sheet1").Cells(i, 2).Value
End If
Next i
End Sub

Resources