Compare two columns in excel and seeing if values are the same - excel

This image shows the before. https://drive.google.com/open?id=0B8BmxxuBoGYnVkhDaEF2b1J6ejA
The objective of the code is to look at the value of the first cell of column 1 then look for that same value in Column 4 by going down the Column. In the case of the first cell in Column 1 it would be honey and the corresponding row in Column 4 is 6. Then it will duplicate the values from Column 5 and Column 6 that corresponds with honey in Column 4 and put it in Column 2 and Column 3 in the row that corresponds with honey for Column 1. Every time a cell in Column 2 or Column 3 is filled it will be colored blue. I don't know how to get the syntax right to set a string in one cell equal to a string in another cell and determining if the cell is blank in the first place.
This image shows the after.
https://drive.google.com/open?id=0B8BmxxuBoGYnX1VXWllaQTAxWFE
Sub checkcolumns()
'j determines the row for Column 1. n determines the row for Column 4'
Dim j As Integer
Dim n As Integer
j = 1
n = 1
'The first part is a Do While loop and is intended to check if the first
'cell is filled with something. If it's not then the code won't run.'
Do While Cells(j,1).Value <> Not vbNullString
'The next part determines whether the first cell from Column 1 and
'first cell from Column 4 are the same. If they aren't then it will
'search for the cell in Column 4 that has the same value. n denotes the row
'for column 4 and the Do Until loop will determine which row in column 4
'has the exact value as the cell we're looking at from Column 1
if Cell(j,1) NotEqual Cell(n, 4)
Do Until cell(j, 1) Equalto Cell(n, 4)
n = n + 1
End
'The next if statements first determine whether Column 2 of the row we're
'looking at has a value already. If it does not then that cell is
'populated with whatever value is in Column 5 of the corresponding row for
'Column 4 which is found with n. This is repeated for Column 3 using
'Column 6.
if Cells(j, 2).Value <> vbNullString Then
Cells(j, 2) = Cells(n, 5)
Cells(j, 2).Interior.ColorIndex = 5
End if
if Cells(j, 3).Value <> vbNullString Then
Cells(j, 3) = Cells(n, 6)
Cells(j, 3).Interior.ColorIndex = 5
End if
'This else statement below is for the case where the cell value from
'Column 1 on that row is equal to the cell value of Column 4 on that
'same row, so j and n would be equal.
Else
if Cells(j, 2).Value <> vbNullString Else
Cells(j, 2) = Cells(n, 5)
Cells(j, 2).Interior.ColorIndex = 5
End If
if Cells(j, 3).Value <> vbNullString Else
Cells(j, 3) = Cells(n, 6)
Cells(j, 3).Interior.ColorIndex = 5
End If
End If
'Once it has checked the first row in Column 1. It will then look at the
'second row.
j = j + 1
End
End Sub

Put this formula in B2:
=VLOOKUP(A2,$D$2:$F$7,2,FALSE)
And then put this formula in C2:
=VLOOKUP(A2,$D$2:$F$7,3,FALSE)
A2 is the value you want to search in column D
$D$2:$F$7 creates a static table to search
2 or 3 is the column in that table (from first col of table) you want returned
False requires an exact match on the search
Once you put them in the cells, drag them down.

Related

sum filtered column in table Excel VBA

I am trying to write code that takes filtered criterium in column H (Associated work flow) from table below and than sums column G (Celková výše výnosu v EUR) according to columns C (Outbound/Inbound) and I (Úspěšně předáno) and place final number to Cell K3 by pressing button (Spustit).
I think it should work like this, i choose criterium in column H, for example Jade&Fountain, it will show me only rows that includes Jade&Fountain, i will press the button, and it will sum only those with Yes in column I + if there is I in column C, it will subtract from the total, if there is O, it will add to the total. Then it will show the total in Cell K3.
I add code i wrote, it should work, but i am not sure in the Do While Cells(i,2).SpecialCells(xlCellTypeVisible).Value <> "" section. Thanks for any response.
Sub While_loop()
Dim i As Integer
Dim x As Integer
i = 3
x = 0
Do While Cells(i, 2).SpecialCells(xlCellTypeVisible).Value <> ""
If Cells(i, 9).Value = "Yes" Then
If Cells(i, 3).Value = "O" Then
x = x + Cells(i, 7).Value
End If
If Cells(i, 3).Value = "I" Then
x = x - Cells(i, 7).Value
End If
End If
i = i + 1
Loop
Cells(2, 12).Value = x
End Sub
Table i am refering to
You can do that without VBA if you want to, following this tutorial https://exceljet.net/formula/count-visible-rows-only-with-criteria
Try this function (assuming data in row 3-15), the key is the SUBTOTAL function:
=SUMPRODUCT((C3:C15="O")*(I3:I15="Yes")*(SUBTOTAL(109,OFFSET(G3:G15,ROW(G3:G15)-MIN(ROW(G3:G15)),0,1))))
That gives the sum of all visible rows with your 2 criteria. If you want to deduct the I :
=SUMPRODUCT((C3:C15="O")*(I3:I15="Yes")*(SUBTOTAL(109,OFFSET(G3:G15,ROW(G3:G15)-MIN(ROW(G3:G15)),0,1))))-SUMPRODUCT((C3:C15="I")*(I3:I15="Yes")*(SUBTOTAL(109,OFFSET(G3:G15,ROW(G3:G15)-MIN(ROW(G3:G15)),0,1))))

Copy values based on criteria with macro

I have 2 columns, one with dates (column A:A, of type dd/mm/yyyy hh:mm) the other with values of a parameter (column B:B), registered at each date. But not all dates have registered values (in which case in column B I will have -9999.
What I need is to copy to other columns (say D:D and E:E) only the cells where there is a value other than -9999 and the correspondent date too. For example:
Example
My data series is pretty long, it can get to 10000 or more lines, so I cannot do this selection “manually”. I would prefer macros, not array formulae, because I want to choose the moment of the calculation.
This code should do what you are looking for. This will copy all rows with a value in column B, into columns D and E.'
Sub copyrows()
Dim RowNo, newRowNo As Long
RowNo = 2
newRowNo = 2
With ThisWorkbook.ActiveSheet
.Cells(1, 4).Value = "Date"
.Cells(1, 5).Value = "H_Selected"
Do Until .Cells(RowNo, 1) = ""
If .Cells(RowNo, 2) <> "-9999" Then
.Cells(newRowNo, 4) = .Cells(RowNo, 1)
.Cells(newRowNo, 5) = .Cells(RowNo, 2)
newRowNo = newRowNo + 1
End If
RowNo = RowNo + 1
Loop
End With
End Sub

How to find duplicates same column and print duplicates id in another column's?

I need to compare the value in a single column in a single table. Here is a sample table:
Duplicates values id want print in column C.
In the above example ID 1 and ID 6 are duplicates, So cell c2 want to print ID 6, like that c7 want print ID 1.
How to get ans for this ?
Can try below,
Sub test()
i = 2
With ActiveSheet
For Each Cell In .Range("B2:" & .Range("B2").End(xlDown).Address)
If Cell.Row + 1 < .Range("B2").End(xlDown).Row Then
For Each C In .Range(.Cells(Cell.Row + 1, 2).Address, .Range("B2").End(xlDown).Address)
If C.Value = Cell.Value Then
.Cells(i, 3).Value = C.Offset(0, -1).Value
i = i + 1
End If
Next
End If
Next
End With
End Sub

Search text string for a match and change font color

It's been 6 years since I've worked with Excel and i'm a little bit rusty. Here's my scenario:
I am exporting a list of issues to Excel. I need to be able differentiate the associated Link numbers in a cell (mulitple values) from each other. Example, i have two columns,
Key = the number for a ticket
Linked Issues = The Keys associated
I need a statement that would scan the Key column and find a match in the Linked Issues column. Then once the match is found the matching text will assume the font color of the Key.
Where this get complicated is each cell of the Linked Issues column could look something like this iss-3913, iss-3923, iss-1649. So essentially the scan would be for a match within the string. Any help is appreciated.
I am sorry, I don't have time to finish this right now, but wWould something like this help with maybe a loop for each cell in the first column?
Edit: Finished now, second edit to update to B5 and Z5, edit 3 fixed goof with column reference and updated to use variables to assign what column to look in.
Sub colortext()
start_row = 5
key_col = 2
linked_col = 26
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
o = start_row 'start with row one for second column
Do While Not IsEmpty(Cells(o, linked_col)) 'Do until empty cell
If Not InStr(1, Cells(o, linked_col), Cells(i, key_col)) = 0 Then 'if cell contents found in cell
With Cells(o, linked_col).Characters(Start:=InStr(1, Cells(o, linked_col), Cells(i, key_col)), Length:=Len(Cells(i, key_col))).Font
.Color = Cells(i, key_col).Font.Color 'change color of this part of the cell
End With
End If
o = o + 1 'increment the cell in second column
Loop
i = i + 1 'increment the cell in the first column
Loop
End Sub
or maybe
Something like this?
Excel VBA: change font color for specific char in a cell range
This is an old post but I thought I would provide my work around to the conditional formating issue I was having.
Sub colorkey()
start_row = 5
key_col = 2
flag_col = 4
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
Tval = Cells(i, flag_col).Value
Select Case Tval
Case "Requirement"
'cval = green
cVal = 10
Case "New Feature"
'cval = orange
cVal = 46
Case "Test"
'cval = lt blue
cVal = 28
Case "Epic"
'cval = maroon
cVal = 30
Case "Story"
'cval = dk blue
cVal = 49
Case "Theme"
'cval = grey
cVal = 48
Case "Bug"
'cval = red
cVal = 3
Case "NOT MAPPED"
'cval = Maroon
cVal = 1
End Select
Cells(i, key_col).Font.ColorIndex = cVal
i = i + 1 'increment the cell in the first column
Loop
End Sub
Sub colorlinked()
start_row = 5
key_col = 2
linked_col = 26
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
o = start_row 'start with row one for second column
Do While Not IsEmpty(Cells(o, linked_col)) 'Do until empty cell
If Not InStr(1, Cells(o, linked_col), Cells(i, key_col)) = 0 Then 'if cell contents found in cell
With Cells(o, linked_col).Characters(Start:=InStr(1, Cells(o, linked_col), Cells(i, key_col)), Length:=Len(Cells(i, key_col))).Font
.Color = Cells(i, key_col).Font.Color 'change color of this part of the cell
End With
End If
o = o + 1 'increment the cell in second column
Loop
i = i + 1 'increment the cell in the first column
Loop
MsgBox "Finished Scanning"
End Sub

Excel-VBA Insert five empty rows below each group of name

I'm in the middle of trying to do the above but fail. referring to the image below, i need to insert five empty rows below the 4th row (john lee) and another five empty rows below 7th row (bryan key) and another five below 9th row (casey carton) and so on, i got 30+ groups of different name to do. wondering how to write vba for this? thanks.
So 5 blanks after a change in sorted column A?
Const blanks = 5
Dim lastValue As String, i As Long, r As Long
Do
r = r + 1
If r > 1 And lastValue <> Cells(r, 1).Value Then
If Cells(r, 1).Value = "" Then Exit Do
For i = 1 To blanks
Rows(r).Insert Shift:=xlDown
Next
r = r + blanks
End If
lastValue = Cells(r, 1).Value
Loop
the 1 in Cells() is the column index, i.e. 1 == A

Resources