Microsoft Excel VBA Scripting: Recursive column matching - excel

Sub NewMacro()
Dim endRow As Long
endRow = Sheet1.Range("A999999").End(xlUp).Row
For i = 1 To endRow
If Sheet1.Range("A" & i).Value = Sheet1.Range("F" & i).Value Then
Sheet1.Range("K" & i).Value = "Yes" Else
Sheet1.Range("K" & i).Value = "No"
End If
Next i
End Sub
This will compare column A with column F and displays the result in column K.
What I need is if this value is true, then like the above it should compare column B with column G, column C with column H and so on......and should display the results in next column. Please help.

I think you need a loop on the columns:
Sub NewMacro()
Dim endRow As Long
Dim i As Long
Dim c As Long
With Sheet1
endRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To endRow
For c = 1 To 5
If .Cells(i, c).Value = .Cells(i, c + 5).Value Then
.Cells(i, c + 10).Value = "Yes"
Else
.Cells(i, c + 10).Value = "No"
End If
Next c
Next i
End With
End Sub
This compares column A with F, column B with G, column C with H, column D with I and column E with J. Results are placed in columns K, L, M, N and O respectively.
This is equivalent to using the formula =IF(A1=F1,"Yes","No") in cell K1 and copying it across and down.
And a version which will update columns with "Yes", but stop as soon as it reaches a "No":
Sub NewMacro()
Dim endRow As Long
Dim i As Long
Dim c As Long
With Sheet1
endRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To endRow
For c = 1 To 5
If .Cells(i, c).Value = .Cells(i, c + 5).Value Then
.Cells(i, c + 10).Value = "Yes"
Else
.Cells(i, c + 10).Value = "No"
Exit For
End If
Next c
Next i
End With
End Sub

Related

How to delete Rows in Excel VBA

Sub A()
Dim I, Q, C_Count As Integer
C_Count = Worksheets("0618").Cells.SpecialCells(xlLastCell).Column
For I = 7 To C_Count
Q = Worksheets("0618").Cells(9, I).Value
If 0 < Q And Q < 100 Then
Worksheets("sheet1").Cells(I - 1, 3).Value = Worksheets("0618").Cells(2, I).Value
Worksheets("sheet1").Cells(I - 1, 4).Value = Worksheets("0618").Cells(9, I).Value
Worksheets("sheet1").Cells(I - 1, 5).Value = Worksheets("0618").Cells(4, I).Value
End If
Next
End Sub
The result of the code
I want to delete the empty rows with vba code but don't know how to.
Could someone tell me how to do it?
This is a sample code:
Sub Delete()
Dim LastRow As Long
Dim i As Long
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row of Column A Sheet1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop starting from last row to 1 row
For i = LastRow To 1 Step -1
'Check if the value in column A row i is empty
If .Range("A" & i).Value = "" Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub

Deleting Similar Rows using Excel VBA

I am trying to write a VBA code for an Excel macro so that I can manually trigger the macro to do the following:
In the event that any two rows have:
Same value in column A
Same value in Column B
"apple" in Column C
Same value in Column D
Then I would like all of those rows to be deleted except the row with the highest value in column E.
As an example, if:
cell A1 = cell A2
cell B1 = cell B2
cell C1 and Cell C2 = "apple"
cell D1 = cell D2
Cell E1 = 5 and Cell E2 = 10
Then Row 1 gets deleted and Row 2 remains.
The overall goal is to delete similar rows.
Per a user's suggestions, this process can be aided/simplified by sorting range by c="apple",a,b,d so that rows can be analyzed consecutively.
Example of Code Outcome
I put together the following code, but I am unfamiliar with the delete row aspect and how to incorporate the highest value, but this was my best shot. The If and elseif statements are questionable.
Sub Macro()
Dim a As Range
Dim b As Range
Dim c As Range
Dim d As Range
Dim e As Range
For Each a In Range("A1:A9999")
For Each b In Range("B1:B9999")
For Each c In Range("C1:C9999")
For Each d In Range("D1:D9999")
For Each e In Range("E1:E9999")
If a.Offset(-1, 0) = a And b.Offset(-1, 0) And c.Offset(-1, 0) = c And d.Offset(-1, 0) = d And e.Offset(-1, 0) < e Then Range(a).EntireRow.Delete
ElseIf a.Offset(-1, 0) = a And b.Offset(-1, 0) And c.Offset(-1, 0) = c And d.Offset(-1, 0) = d And e.Offset(-1, 0) > e Then Range(a.Offset(-1, 0)).EntireRow.Delete
Exit For
Next a
Next b
Next c
Next d
Next e
End Sub
I hope it works.
Option Explicit
Sub RunMacro()
Dim i As Long, LastRow As Long, j As Long
Dim cellA, cellB, cellC, cellD, cellE
Dim Rng As Range
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
cellA = Range("A" & i).Value
cellB = Range("B" & i).Value
cellC = Range("C" & i).Value
cellD = Range("D" & i).Value
cellE = Range("E" & i).Value
For j = LastRow To 2 Step -1
If Range("A" & j).Value = cellA And Range("B" & j).Value = cellB Then
If Range("C" & j).Value = cellC And Range("D" & j).Value = cellD Then
If cellE > Range("E" & j).Value Then
Range("E" & j).EntireRow.Delete
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
End If
End If
End If
Next j
Next i
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set Rng = Range("A1", Range("E1").End(xlDown))
Rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5), Header:=xlYes
End With
End Sub

Repeat column contents by "n" rows based on column value

I would like to repeat ID number based on the "number" number. For example:
to
I have tried the following so far..
Sub MySub()
Do While B2 = n
CurrentSheet.Range("a1:c1").EntireRow.Resize(n).Insert
Loop
End Sub
It probably doesn't make much sense, as I am fairly new!
If you wanted to list the data in column D, you could use this
Sub x()
Dim r As Range
For Each r In Range("A2", Range("A" & Rows.Count).End(xlUp)) 'loop through A
Range("D" & Rows.Count).End(xlUp)(2).Resize(r.Offset(, 1).Value).Value = r.Value 'duplicate number of times in B
Next r
End Sub
If you want to insert into your existing data
Sub x()
Dim r As Long
For r = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
If Cells(r, 2) > 1 Then
Cells(r + 1, 1).EntireRow.Resize(Cells(r, 2).Value - 1).Insert shift:=xlDown
Cells(r + 1, 1).Resize(Cells(r, 2).Value - 1) = Cells(r, 1).Value
End If
Next r
End Sub

If a value in column M equals x then column A should Equal Column E

I need to start at the top of column L then go down row by row until the last row.
If the value in column L = 8 then copy the value from column E into column A.
If the value of column L = 7 then copy the value from column E into column B.
The error I receive is:
Method Range of Object_Global failed (1004)
Sub CopyVals()
Dim lngLoop As Long, lngRow As Long
Do Until IsEmpty(ActiveCell)
If Range("L" & lngLoop).Value = 8 Then
Range("E" & lngLoop).Copy Range("A" & lngLoop)
ElseIf Range("L" & lngLoop).Value = 7 Then
Range("E" & lngLoop).Copy Range("B" & lngLoop)
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Sub CopyVals()
Dim lngLoop As Long
lngLoop = 1
For lngLoop = 1 To Rows.Count
If Cells(lngLoop, 12).Value = "8" Then Cells(lngLoop, 1).Value = Cells(lngloop, 5).Value
If Cells(lngLoop, 12).Value = "7" Then Cells(lngLoop, 2).Value = Cells(lngloop, 5).Value
Next lngLoop
End Sub

VBA code to find the sum of unique elements in a range

I have 2 columns and need a VBA code to sum the values of unique elements in column "A", print the unique elements in column "D" and sum in column "E" :-
Name Value Name Sum
A 1 A 13
A 2 B 7
B 1 C 3
B 3
C 2
A 1
B 2
A 3
B 1
A 2
A 4
C 1
Can anyone help on this, this is what I tried :-
Sub CountSum()
Dim c As Collection, wf As WorksheetFunction, _
K As Long, N As Long, i As Long, _
v As Variant, d As Collection, y As Variant
Set c = New Collection
Set d = New Collection
Set wf = Application.WorksheetFunction
K = 2
N = Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For i = 2 To N
v = Cells(i, "A").Value
y = Cells(i, "B").Value
c.Add v, CStr(v)
d.Add y
If Err.Number = 0 Then
Cells(K, "D").Value = v
Cells(K, "E").Value = wf.CountIf(Range("A:A"), v)
Cells(K, "F").Value = wf.Sum(Range("B:B"), y)
K = K + 1
Else
Err.Number = 0
End If
Next i
On Error GoTo 0
End Sub
Using a Dictionary:
Sub Tester()
Dim rng As Range, dict As Object
Set rng = Range(Range("A2"), Cells(Rows.Count, 1).End(xlUp)).Resize(, 2)
Set dict = SubTotals(rng, 1, 2)
DumpDict dict, Range("D1")
End Sub
Function SubTotals(rng As Range, colKey As Long, colVal As Long) As Object
Dim rv As Object, rw As Range, k, v
Set rv = CreateObject("scripting.dictionary")
For Each rw In rng.Rows
k = rw.Cells(colKey).Value
v = rw.Cells(colVal).Value
If Not IsError(k) And Not IsError(v) Then
If Len(k) > 0 And IsNumeric(v) Then
rv(k) = rv(k) + v
End If
End If
Next rw
Set SubTotals = rv
End Function
Sub DumpDict(dict As Object, rng As Range)
Dim i As Long, k
i = 0
For Each k In dict.keys
With rng.Cells(1)
.Offset(i, 0).Value = k
.Offset(i, 1).Value = dict(k)
End With
i = i + 1
Next
End Sub
next code works for me, I hope this will help you. This will work perfectly if at column A there are not blank cells between values.
Sub SUM()
Dim i, j, k As Integer
i = 2
j = 2
Range("D1").Value = "NAME"
Range("E1").Value = "VALUE"
'copy the first value of column A to column D
Range("D2").Value = Range("A2").Value
'cycle to read all values of column B and sum it to column E; will run until find a blank cell
While Range("A" & i).Value <> ""
'this check if actual value of column A is equal to before value of column A, if true just add the column B value to E
'else, look for the row in column D where is the same value of column A, if it doesn't exist code create the value
'in column D and E
If Range("A" & i).Value = Range("A" & i - 1).Value Then
Range("E" & j).Value = Range("E" & j).Value + Range("B" & i).Value
Else
flag = 1
While Range("D" & flag).Value <> ""
If Range("A" & i).Value = Range("D" & flag).Value Then
j = flag
Range("E" & j).Value = Range("E" & j).Value + Range("B" & i).Value
flag = Range("D1").End(xlDown).Row
Else
j = 0
End If
flag = flag + 1
Wend
If j = 0 Then
Range("D1").End(xlDown).Offset(1, 0).Value = Range("A" & i).Value
Range("E1").End(xlDown).Offset(1, 0).Value = Range("B" & i).Value
j = Range("E1").End(xlDown).Row
End If
End If
i = i + 1
Wend
MsgBox "End"
End Sub

Resources