Copying specific coloured cells onto a different worksheet - excel

I'm working on a spreadsheet with conditional formatting, which turns some cells green and some red, depending on if they are in/out of the correct range.
What I need is for the red "out of spec" numbers to be copied onto the next sheet leaving the green "within spec" number off the second sheet. A bit like this:
Sheet 1:
a 2
b 4
c 5
d 6
e 3
Sheet 2:
a
b 4
c 5
d 6
e
I hope this makes sense, I did take screenshots but I can't post them! My fingers are crossed that someone can help :)
Thank you in advance
Jazz

I have assumed that data is in Column A of Sheet1.
Tested
Sub checkColornCopy()
Find the last row for automation
lastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
sheet2Counter = 1
For i = 1 To lastRow
Extracting the color of the Cell interior
ConditionalColor = Worksheets("Sheet1").Cells(i, 1).Interior.ColorIndex
Color Index 3 denotes "Red"
If ConditionalColor = 3 Then
If the color is Red thenCopying the cell content of Sheet1 to Sheet2
Worksheets("Sheet2").Cells(sheet2Counter, 1).Value = Worksheets("Sheet1").Cells(i, 1).Value
Making the cell content of Sheet1 blank
Worksheets("Sheet1").Cells(i, 1).Value = " "
sheet2Counter = sheet2Counter + 1
End If
Next
End Sub

This is probably not the best way to do it but, it worked for me.
Try:
Dim i As Integer
Dim cell As String
Sheets("Sheet1").Activate
For i = 1 To 10
'Check if font is red
If Range("A" & i).Font.Color = "fontcolor" Then
cell = Range("A" & i).Value
'Check for a number in the cell and remove the right most number
While IsNumeric(Right(cell, 1))
cell = Range("A" & i).Value
cell = Left(cell, Len(cell) - 1)
Sheets("sheet2").Range("A" & i).Value = cell
Wend
Else
'If font is not red then display cell value on sheet2
Sheets("sheet2").Range("A" & i).Value = Sheets("sheet1").Range("A" & i).Value
End If
Next
Sheets("Sheet2").Activate
Edited
In this case "A3" has red font.
To find the color of your red font use:
sub Text_Color()
Dim color As String
'"A3" has red text.
color = Sheets("sheet1").Range("A3").Font.color
MsgBox "My text color is= " & color
End Sub
Take the number found in the msgbox, in this example 393372. And replace "fontcolor" from the above code with 393372 .

Related

VBA Excel filling up interior color for Entirerow just to the last used column

I am trying to fill up my rows with colour down to the last used column.
I tried:
For i = 2 To lRow
If ActiveSheet.Range("O" & i).Value = "FULL" Then
ActiveSheet.Range ("O" & i), Cells(lCol).Interior.Color = RGB(155, 255, 0)
ElseIf ActiveSheet.Range("O" & i).Value = "NEW" Then
ActiveSheet.Range("O" & i).EntireRow.Interior.ColorIndex = 33
End If
Next
But I am getting "Application defined or object-defined error".
This code is condition-based. When the value "FULL" or "NEW" is to be found down to the last row, then the row with this value has to be highlighted. The problem is, that I don't want to use the entirerow property, as this colour is seen forever. I just need to restrict the .interior.Color property to my last column used.
How can I do that?
I found some other solutions, from where I tried to solve this issue, but in vain.
How to select a range of the second row to the last row
How do I find the last column with data?
First of all, we need to define the lcol properly
Dim wo as Worksheet
Dim lCol as Long
Then always set our worksheet as a priority
Set wo = ThisWorkbook.ActiveSheet
lCol = wo.UsedRange.Columns(wo.UsedRange.Columns.Count - 14).Column
And afterward, define our last column.
I've put -14, which means that the last 14 columns won't be affected, as per the explanation here:
ActiveSheet.UsedRange.Columns.Count - 8 what does it mean?
Next my code should look like this:
For i = 2 To lRow
If ActiveSheet.Range("O" & i).Value = "FULL" Then
ActiveSheet.Range("A" & i).Resize(1, lCol).Interior.Color = RGB(155, 255, 0)
ElseIf ActiveSheet.Range("O" & i).Value = "NEW" Then
ActiveSheet.Range("O" & i).EntireRow.Interior.ColorIndex = 33
End If
Next
referring to the very first column A instead of "O", the filtered value was picked up from.
Another way you can find here:
https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba

VBA - How to output a variable to a particular row and column in excel?

This code should find the correct cell (in the column corresponding to it's 'length' and the next empty row) in which to output a variable.
I'm getting the error message:
method range of object _worksheet failed
on lines 13 onward containing "outputcolumn"
In the MsgBox lines, the correct column and row number are being displayed, so I am not sure why it is not happy with my outputcolumn in particular.
Private Sub OutputRowAndColumn()
'Choose correct column: Find the length column and name this outputcolumn
Dim cell As Range, outputcolumn As Integer
Set cell = Range("FindLength").Find(Range("Length").Value, LookIn:=xlValues)
If Not cell Is Nothing Then
outputcolumn = cell.Column
End If
MsgBox "Output column is number " & outputcolumn & "."
'Choose correct row: If the cell to the left of "cell" is empty then this is the first row of output otherwise find next empty cell down
If Sheet1.Range(outputcolumn & "4").Offset(0, 1).Value = "" Then
outputrow = 4 ''' error msg '''
ElseIf Sheet1.Range(outputcolumn & "5").Offset(0, 1).Value = "" Then
outputrow = 5
Else
outputrow = Sheet1.Range(outputcolumn & "4").Offset(0, 1).End(xlDown).Row + 1
End If
MsgBox "Output row is number " & outputrow & "."
'Copy values 1, 2 and 3 from sheet 2 to sheet 1
Sheet1.Range(outputcolumn & outputrow).Offset(0, 1).Value = Sheet2.Range("Value1").Value ''' error msg '''
Sheet1.Range(outputcolumn & outputrow).Offset(0, 2).Value = Sheet2.Range("Value2").Value
Sheet1.Range(outputcolumn & outputrow).Offset(0, 3).Value = Sheet2.Range("Value3").Value
End Sub
outputcolumn is a numeric value (you defined it as Integer, but you always should define variables holding row or column numbers as long to avoid overflow errors).
So let's say outputcolumn gets the number 2 (column B). You write Sheet1.Range(outputcolumn & "4"). To access a range by it's address, You would have to write something like Range("B4"), but what you write is Range(2 & "4"), which means Range("24"), and that is an invalid address for a Range.
You could try to translate the column number 2 to a B, but there is an easier way to access a cell when you know the row and column number: Simply use the cells-property:
If Sheet1.Cells(4, outputcolumn).Offset(0, 1).Value = "" Then
' (or)
If Sheet1.Cells(4, outputcolumn+1).Value = "" Then
Just note that the order of the parameters is row, column.
"outputcolumn" is numeric in your case and when using .Range(), it needs to be a proper alphanumeric cell reference like "C5", not all numeric.
I haven't tried it directly but changing this ...
If Not cell Is Nothing Then
outputcolumn = cell.Column
End If
... to this ...
If Not cell Is Nothing Then
outputcolumn = Split(cell.Address, "$")(1)
End If
... will go a long way to helping you.

How do I pull text from a cell in a different sheet if the cell two columns away contains specific text?

So essentially what I am trying to do is make name-tags in a spreadsheet for an attendance list I have created in a separate spreadsheet.
I have their full names in one column, and two columns over I have what colour they have been assigned to.
I have separate spreadsheets for each colour for the name-tags.
I want the formula to be written in a separate spreadsheet, let's call it "Green 20". So Green 20 would have multiple cells containing the formula that does the following:
Check to see if the word "Pink" is in a cell between D6:D100 in Spreadsheet
If "Pink" is in the cell, I want the formula to look at the row "Pink" is in and print out cell B? (? meaning whatever row it is) that contains the attendee's name.
So essentially the formula has to get data from a separate spreadsheet, check column D for specific text containing a colour, if the text is in one of the cells in the column then it needs to check the row the text is in and print out the text (First name and last name) in column B corresponding to the row that colour was in.
So I actually went ahead and wrote a macro with one of my co-workers and this is what it ended up looking like:
Public Sub NameTags()
' NameTags Macro
' March 20
Dim Rng As Range
Set Rng = Range("$B$5:$E$100")
Dim rowcount, colselect As Integer
rowcount = 1
colselect = 2
Sheets("March 20, 2017").Range("$B$4:$E$100").AutoFilter Field:=4, Criteria1:="Green"
For Each Row In Rng.Rows
'MsgBox Row.Row
If Cells(Row.Row, 2).EntireRow.Hidden = False Then
If Cells(Row.Row, 2).Value <> "" Then
copystring = Cells(Row.Row, 2).Value & " " & Cells(Row.Row, 3).Value
Sheets("Green 20").Cells(rowcount, colselect) = copystring
Sheets("Green 20").Activate
Cells(rowcount, colselect).Select
Set mergecellrng = Cells(rowcount, colselect)
Set mergecellrng2 = mergecellrng.Offset(0, 3)
Range(mergecellrng, mergecellrng2).Merge
mergecellrng.Font.Name = "Segoe UI"
mergecellrng.Font.Size = 22
mergecellrng.HorizontalAlignment = xlCenter
mergecellrng.VerticalAlignment = xlCenter
If colselect = 8 Then
rowcount = rowcount + 7
End If
Select Case colselect
Case 2
colselect = 8
Case 8
colselect = 2
End Select
End If
End If
Sheets("March 20, 2017").Activate
Next
An it repeats for each colour

Can I create a self updating module in excel?

So I have a column with data that need to be simplified.
ColumnA ColumnB
Cyan Blue
Navy Blue
Forest Green
I want to write a module that would ask me what to do about cyan when it first encounters it and I have to input blue. But for all the instances that cyan is encountered after this, the corresponding column B needs to be blue.
Sub SIMPLIFY()
RECORD_COUNT = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To RECORD_COUNT
If WorksheetFunction.CountIf(Columns("A"), Range("A" & i)) > 0 Then
RECORD_ROW = WorksheetFunction.Match(Range("A" & i), Columns("A"), 0)
If Cells(RECORD_ROW, 2) <> Empty Then
Cells(i, 2) = Cells(RECORD_ROW, 2)
Else
Cells(i, 2) = InputBox("Input Color for " & Cells(i, 1))
End If
End If
Next i
End Sub
Copy above code to worksheet VBA.
The code waits for any changes in column "A" and checks for previous data.
It inputs color automatically if it exists, or it asks for color when it's new.

alternating row colors/numbers in Excel - VBA

So I am working on a reporting tool in Access. It queries the local tables and creates a Excel document (through VBA) and makes an Excel file.
I use the following code to color alternative code and it works beautifully
For a = 1 To rs.RecordCount
With ExcelSheet
.Cells(a + 1, 1) = a
.Cells(a + 1, 1).EntireRow.Interior.ColorIndex = IIf((a + 1) Mod 2 = 0, 2, 15)
End With
Next
Note I have to do a + 1 because a = 1 is the title row, and that is the title row.
Note: .Cells(a + 1, 1) = 1 numbers the rows (1 , 2, 3, ...)
Note : IIf((a + 1) Mod 2 = 0, 2, 15) The 2 and 15 are color codes.
Now my question is that when someone gets the Excel report they might delete a row, or do a sort operation or whatever and when they do that, it messes up the rows.
ex:
1 white row
2 grey row
3 white row
4 grey row
if i sort them I get
3 white row
1 white row
2 grey row
4 grey row
which is not what I want, I want it to keep the formatting and the numbering
Anyone to accomplish this using VBA in Access?
Tech: Office 2007
This can be accomplished with the ROW() function and some conditional formatting. The ROW() function returns the current row of the cell it is in, so it will change whenever cells are deleted, moved or sorted. Conditional formatting is re-applied whenever its conditions change, so moving or sorting rows would inform Excel to update the colors accordingly. The code would look as follows:
Dim a As Integer
Dim oneRow As Range
For a = 1 To rs.RecordCount
With ExcelSheet
''// show the row number in the first cell
.Cells(a + 1, 1).Formula = "=ROW()"
''// set formatting to alternate row colors
Set oneRow = .Cells(a + 1, 1).EntireRow
oneRow.FormatConditions.Delete
oneRow.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=MOD(ROW()-1, 2)=0"
oneRow.FormatConditions(1).Interior.ColorIndex = 2
oneRow.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=MOD(ROW()-1,2)=1"
oneRow.FormatConditions(2).Interior.ColorIndex = 15
End With
Next a
#e.James
Dim rowRange As Range
ExcelSheet.Cells(1, 1).EntireColumn.ColumnWidth = 4
ExcelSheet.Cells(1, 1) = "#"
Set rowRange = Range("2:2", rs.RecordCount & ":" & rs.RecordCount)
rowRange.Select
With ExcelApp.Selection
.FormatConditions.Delete
.FormatConditions.Add xlExpression, Formula1:="=MOD(ROW(),2)"
.FormatConditions(1).Interior.ColorIndex = 15
End With
That dosnt work. It just highlights the very top row (the title row) grey.
EDIT NEVERMIND
Its supposed to be
Set rowRange = ExcelSheet.Range("2:2", rs.RecordCount & ":" & rs.RecordCount)
EDIT NUMBER 2:
Do you know how I can insert the row numbers in each row using this method?
ANSWER:
ExcelSheet.Cells(1, 1).EntireColumn.ColumnWidth = 4
Set RowRange = Range("2:2", rs.RecordCount & ":" & rs.RecordCount)
RowRange.Columns(1).Formula = "=ROW()-1"
With RowRange
.FormatConditions.Delete
.FormatConditions.Add xlExpression, Formula1:="=MOD(ROW(),2)"
.FormatConditions(1).Interior.ColorIndex = 15
End With

Resources