Compare numbers between two columns and match the colours - excel

I am struggling to find any info on the internet to make this work, please help me out.
I would like a function to do the following (summarized below)
As you can see column A3:A7 has a number in each cell and a colour associated with that specific number.
I would like the code to scan through A3:A7 and match the numbers in C3:C7 with the colour that's already applied. (See below for detailed explanation)
For instance, A3 has a value of 1 and is yellow, I would like the code to scan through all numbers in Column C (C3:C7) and identify that C6 is also 1, therefore it will apply yellow to C6.
Initial:
Final:
Also can this be done across two different Sheets.For example lets say A3:A7 is on Sheet1 and I want to find matches in C3:C7 in Sheet2

Sub ColourCells()
Dim Rng1 As Range, Rng2 As Range, Rng2Item As Range
Dim Rng1LRow As Long, Rng2LRow As Long
Dim Rng1Match As Variant
With Worksheets("Sheet1")
Rng2LRow = .Cells(.Rows.Count, 3).End(xlUp).Row
Set Rng2 = .Range("C3:C" & Rng2LRow)
End With
With Worksheets("Sheet2")
Rng1LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set Rng1 = .Range("A3:A" & Rng1LRow)
End With
For Each Rng2Item In Rng2
With Rng2Item
Rng1Match = Application.Match(.Value, Rng1, 0)
If IsError(Rng1Match) Then
GoTo NextItem
Else
.Interior.Color = Application.Index(Rng1, Rng1Match, 0).Interior.Color
End If
End With
NextItem:
Next Rng2Item
End Sub

Sub test()
Dim rng1 As Range, rng2 As Range, rng As Range
Set rng1 = Range("A3:A7")
Set rng2 = Range("C3:C7")
For Each rng In rng2
With Application.WorksheetFunction
If .CountIf(rng1, rng.Value) > 0 Then rng.Interior.Color = .Index(rng1, .Match(rng.Value, rng1, 0), 1).Interior.Color
End With
Next rng
Set rng1 = Nothing
Set rng2 = Nothing
End Sub

Related

take the name of the month in date format

https://i.stack.imgur.com/9aNsi.jpg
I learned VBA from scratch and started simple.
I just want to display the month name from the date format in Column B2: B690, and put it in Column A2: A690 .. but my code has an error.
can you help me find the right solution in coding?
Dim rng1, rng2 As Range
Set rng1 = Range("A2:A690")
Set rng2 = Range("B2:B690")
rng1.Value = WorksheetFunction.Text(rng2, "mmmm")
Try the next code, please:
Dim rng1, rng2 As Range
Set rng2 = Range("B2:B690")
rng2.Copy Range("A2")
Set rng1 = Range("A2:A690")
rng1.NumberFormat = "MMMM"
Or avoiding the use of clipboard:
Sub testTextMonthName()
Dim rng1, rng2 As Range
Set rng1 = Range("A2:A690")
Set rng2 = Range("B2:B690")
With rng1
.Value2 = rng2.Value2
.NumberFormat = "MMMM"
'.value = .Text 'if you need it as text, uncomment this line, please
End With
End Sub

Excel VBA Code to merge similar adjacent cells

I'd like to merge identical adjacent cells within a column. Some online examples loop through the column and merge every time the cell below matches, and I'd like to avoid that. Here's my current broken attempt that spits out run-time error 5.
Sub Merge2()
Application.ScreenUpdating = False
Dim rng1 As Range
Dim rng2 As Range
Dim certaincell As Range
Dim LastRow As Long
LastRow = 0
LastRow = Cells(Rows.Count, 35).End(xlUp).Row
Set rng1 = Range(Cells(2, 35), Cells(LastRow, 35))
CheckUnder:
For Each certaincell In rng1
Set rng2 = Union(rng2, certaincell) 'Add the checking cell to the range
If certaincell.Value = certaincell.Offset(1, 0).Value Then 'if the cell is the same as the cell under
'move on to next cell
Else
rng2.Merge 'merge similar cells above
Set rng2 = Nothing
End If
Next
Application.ScreenUpdating = True
End Sub
The variable rng2 is initially set to Nothing. So, adjust your code as follows:
For Each certaincell In rng1
If rng2 Is Nothing Then
Set rng2 = certaincell
End If
Set rng2 = Union(rng2, certaincell) 'Add the checking cell to the range
If certaincell.Value = certaincell.Offset(1, 0).Value Then
Else
rng2.Merge 'merge similar cells above
Set rng2 = Nothing
End If
Next
The if statement will check if the rng2 is nothing and if so, it will assign the currently checked certaincell to the variable.
Also, merging cells with data will pop up some error dialogs. This can be avoided by using Application.DisplayAlerts = False.
Make sure to turn it on using Application.DisplayAlerts = True at the end.

Vba Code to Delete data based on a drop box choice

I have a sheet that has a List box when that is selected codes appear. If a code is selected, excel copies the data from a worksheet (with the same code) into a quotation sheet.
If I make a change and select another code in the same list box, I need excel to go and find the old data and delete it in the Quotation sheet.
Public Sub delete_selected_rows()
Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
Dim lastRow As Long
With Worksheets("Q")
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set rng1 = .Range("B1:B" & lastRow)
End With
Set rng2 = Worksheets("SO").Range("D35")
For Each c In rng1
If Not IsError(Application.Match(c.Value, rng2, 0)) Then
'if value from rng1 is found in rng2 then remember this cell for deleting
If rngToDel Is Nothing Then
Set rngToDel = c
Else
Set rngToDel = Union(rngToDel, c)
End If
End If
Next c
If Not rngToDel Is Nothing Then rngToDel.CurrentRegion.Delete
End Sub
How can I get CurrentRegion to count an extra 30 rows the delete?
VBA's ISERROR won't catch the error caused by a failed MATCH worksheet function. You need to construct that part differently.
Public Sub delete_selected_rows()
Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
Dim lastRow As Long
Dim R As Long
With Worksheets("Q")
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Set rng1 = .Range("B1:B" & lastRow)
End With
Set rng2 = Worksheets("SO").Range("D35")
For Each c In rng1
On Error Resume Next
R = 0
R = WorksheetFunction.Match(c.Value, rng2, 0)
On Error GoTo 0
If R Then
'if value from rng1 is found in rng2 then remember this cell for deleting
' R is the row number in rng2 where a match was found
' since rng2 is a single cell, R would always be 1, if found
' If rng2 = D35 MATCH be an overkill. Why not simply compare?
Else
If rngToDel Is Nothing Then
Set rngToDel = c
Else
Set rngToDel = Union(rngToDel, c)
End If
End If
Next c
If Not rngToDel Is Nothing Then rngToDel.CurrentRegion.Delete
End Sub
Please observe my comments about rng2. Could there be some mistake? What does SO.D35 contain? If it contains a string of values one of which might be the one you look for MATCH is the wrong function to use.
It seems that you intend to put all items to be deleted on a spike and delete them in one go at the end. I'm not sure that is possible, and it's getting late for me. The more common approach would be to delete one row at a time, as you find them, because once you delete a row all row numbers below that row will change. You can run the entire code with ScreenUpdating turned off and set Application.ScreenUpdating = True after all the deleting has been done.

VBA - determine if a cell value (String) matches a Value (String) in a named range

apologies if this has already been answered although I have searched and search with no luck. in a nutshell im trying to change the cell colour if that cell value does not match a value in a named range.
I have tried a number of methods although none are working for me , any help from the vba gurus would be greatly appreciated.
essentially I have a list of values on sheet1(Create) G2:G5000 that I need to know when they don't match value on sheet2(lists) S2:S64 <--this has a named range of Make.
please see a copy of my current code below
Sub testMake()
Dim MkData As Range, MkVal As Range
Dim MKArray As Variant
Set MkData = Worksheets("Create").Range("G2:G5000")
Set MkVal = Worksheets("Lists").Range("Make")
For Each MyCell In MkData
If MyCell.Value <> Range("MkVal") Then
MyCell.Interior.ColorIndex = 6
Else
MyCell.Interior.ColorIndex = xlNone
End If
Next
End Sub
Thanks you all for any help in advance, I have been looking at this for a few days now and seem to be no closer than when I started.
While I would use conditional formatting you could slightly adapt your code as below to do this programatically:
Sub testMake()
Dim MkData As Range
Dim MkVal As Range
Dim MKArray As Variant
Dim lngRow As Long
Dim rng1 As Range
Dim rng2 As Range
MKArray = Worksheets("Create").Range("G2:G5000").Value2
Set rng1 = Worksheets("Create").Range("G2")
Set MkVal = Range("Make")
For lngRow = 1 To UBound(MKArray)
If IsError(Application.Match(MKArray(lngRow, 1), MkVal, 0)) Then
If Not rng2 Is Nothing Then
Set rng2 = Union(rng2, rng1.Offset(lngRow - 1, 0))
Else
Set rng2 = rng1.Offset(lngRow - 1, 0)
End If
End If
Next
If Not rng2 Is Nothing Then rng2.Interior.ColorIndex = 6
End Sub
You could be using Worksheet function Vlookup to compare between the two ranges:
Sub testMake()
Dim MkData As Range, MkVal As Range
Dim MKArray As Variant
Dim result As Variant
Set MkData = Worksheets("Create").Range("G2:G5000")
Set MkVal = Worksheets("Lists").Range("Make")
For Each MyCell In MkData
On Error Resume Next
result = Application.WorksheetFunction.VLookup(MyCell, MkVal, 1, False)
If Err <> 0 Then
result = CVErr(xlErrNA)
End If
If Not IsError(result) Then
MyCell.Interior.ColorIndex = xlNone
Else
MyCell.Interior.ColorIndex = 6
End If
Next
End Sub

Highlighting duplicates based on data within the same column

I'm trying to create a macro that searches the B column to find duplicates of the same string, and then highlights said duplicates from columns A to I
I've managed to create something that highlights the correct cells but does not seem to be able to find the duplicates. Not entirely sure where I've gone wrong but I think it could be because I'm adapting a former macro that looked across two sheets
Code:
Sub Duplicate()
Dim rng1 As Range, rng2 As Range, rng3 As Range, i As Long, j As Long
Dim w1 As Worksheet
Set w1 = Worksheets("Sheet1")
For i = 1 To w1.Range("B" & Rows.Count).End(xlUp).Row
Set rng1 = w1.Range("B" & i)
For j = 1 To w1.Range("B" & Rows.Count).End(xlUp).Row
Set rng2 = w1.Range("B" & j)
Set rng3 = w1.Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 9))
If StrComp(Trim(rng1.Text), Trim(rng2.Text), vbTextCompare) = 0 Then
rng3.Interior.Color = RGB(168, 188, 255)
End If
Set rng2 = Nothing
Next j
Set rng1 = Nothing
Next i
End Sub
At the moment I see one problem, you are referring to Activecell, However you never activate or select any cell nor range. This makes the color change on a cell that you have selected when running the macro and no other. If you replace activecell.row with variable i the macro should be working fine.
Does this have to be within VBA? The conditional formatting feature in Excel can do this without having to rely on a macro. This should also refresh faster than running the VBA. For this you have to rely on relative references within the conditional formatting, which are relative to the activecell at the time you set up the rule.
Select your data from A:I
Conditional formatting> new rule
Select 'use a formula to determine which cells to format'
Use the formula =COUNTIF($B$1:$B$6,$B1)>1, replacing the first variable with your entire range in column B. The single dollar sign on reference $B1 is very important as it tells it to check column B regardless of which column the formatting is being applied to.
You are always setting rng3 to the same cells, because you never change the active cell. Also, you don't really need to have an extra range because you are already looping through the cells.
Sub Duplicate()
Dim rng1 As Range, rng2 As Range, rng3 As Range, i As Long, j As Long
Dim w1 As Worksheet
Set w1 = Worksheets("Sheet1")
For i = 1 To w1.Range("B" & Rows.Count).End(xlUp).Row
Set rng1 = w1.Range("B" & i)
For j = 1 To w1.Range("B" & Rows.Count).End(xlUp).Row
If j <> i Then
Set rng2 = w1.Range("B" & j)
If StrComp(Trim(rng1.Text), Trim(rng2.Text), vbTextCompare) = 0 Then
rng1.Interior.Color = RGB(168, 188, 255)
rng2.Interior.Color = RGB(168, 188, 255)
End If
Set rng2 = Nothing
End If
Next j
Set rng1 = Nothing
Next i
End Sub

Resources