I am trying to do something depending on the interior color of a cell.
This is my code so far but it is showing errors on the If line.
For i = 3 To dumpLastRow
With masterFile.Sheets(dumpRef)
If .Range("A", i).Interior.ColorIndex = 4 Then
''''CODE''''
Else
''''CODE''''
End If
End With
Next
If you have any idea it would be appreciated. Thanks
as alternative this version might be a bit easier to work with
With masterFile.Sheets(dumpRef)
Dim cell As Range
For Each cell In .Range("A3:A" & dumpLastRow).Cells
If cell.Interior.ColorIndex = 4 Then
''''CODE''''
Else
''''CODE''''
End If
Next
End With
You cannot combine letters and numbers like that in range. Use cells instead. You will need to put in cells twice as Range requires that when using cells to populate it.
Range(Cells(i, 1), Cells(i, 1)).Interior.ColorIndex
Related
I have an excel sheet with numbers in each cell. I want to eliminate the cells containing values which are larger than a specific value, different for each row, for example in the picture
I want to eliminate all the cells in a certain row that has values more than the BL cell.
Not sure the exact context in which this is being used, So possibly some conditional formatting would be more stable?
Also not sure what you meant by "Eliminate" so the following code just turns the cell red.
anyway, hopefully this code will help you get started :)
Sub Cell_Vaue_Check()
Dim row As Excel.Range
Dim cel As Excel.Range
For Each row In Sheets("Sheet1").Range("A1:C5").Rows '<<- Replace "Sheets("Sheet1").Range("A1:C5")" with the Sheet and Range you want to check
For Each cel In row.Cells
If cel.Value > Range("E" & cel.row).Value Then '<<- Replace "E" with the Column in which the check value is located
cel.Interior.Color = RGB(288, 0, 0) '<<- This line turns the cell Red. Replace it with whatever code you want depending on what "eliminate" means to you
End If
Next
Next
Set row = Nothing
Set cel = Nothing
End Sub
If Anybody has any improvements please feel free to Add!
Maybe this is a trivial question but just in case anyone encountered the same problem.
I have a excel workbook with several sheets and in each sheet, I want to mark some cells with a particular color (green) and have them sum all together (from all sheets).
This is a spent personal report and each sheet contains comments, so I don't have precise locations for sub-totals.
One solution could be sheet1:A20 + sheet2:B34 ... and so on (manually)
Other it cross my mind will be for each cell that I want as sub-total to colorize in green and make a VBA to sum all across all sheets by color?
Appreciate ideas,
You could do something like this to sum up all values in a specific Range (B2-B4 in my case - but that could be expanded to other ranges, of course):
Dim sumYellowCells As Integer
Dim sumGreenCells As Integer
For Each cell In ActiveSheet.Range("B2:B4")
If cell.Interior.Color = vbGreen Then
sumGreenCells = sumGreenCells + cell.Value
End If
If cell.Interior.Color = vbYellow Then
sumYellowCells = sumYellowCells + cell.Value
End If
Next
Debug.Print sumGreenCells & "-" & sumYellowCells
I'm doing a mail merge from Excel into Word, and I need to copy the background colors of some cells as well as the contents.
Here on SO I've learned that I could add a helper column, and insert a custom function (something like Selection.Interior.Color) that detects the code of the desired cell. I was going to have the mail merge stealthfully pass that code to Word, where a macro would see it and use it to colorize the corresponding table cell.
Unfortunately, Selection.Interior.Color only detects the natural, underlying color of the cell, not the conditionally-formatted color.
Is there a way to detect the color as assigned by the conditional formatting?
(There are 35 different columns using at least 8 different sets of conditional rules).
CONCLUSION: These solutions seem to work, but I decided to avoid adding data or macros to the Excel sheet. Instead I put a macro in Word that basically duplicates the Conditional Formatting functionality. It's slow, but I think its ultimately cleaner.
Thanks, everyone.
As #David said, it seems to be a pain. However, if the conditional formatting includes only the "traditional" standard excel colors (see here), the following seems to respond properly (not exhaustively tested). Column A (rows 1 to 12) contains values from 1 to 12, and conditional formatting were applied to those cells. The code below seems to work, as long as colors are "standard".
Sub Button1_Click()
For i = 1 To 12
Worksheets("Sheet1").Cells(i, 2) = Worksheets("Sheet1").Cells(i, 1).DisplayFormat.Interior.ColorIndex
Next
End Sub
Here is a routine which places the displayformat.interior.color code in a "helper column" next to the column being tested. I also added a column to show the RGB values, but only for interest. Oh, and the colors were all generated by conditional format.
Option Explicit
Sub GetColor()
Dim R As Range, C As Range
Set R = Range(Cells(2, 1), Cells(10, 1))
For Each C In R
C.Offset(0, 1).Value = C.DisplayFormat.Interior.Color
C.Offset(0, 2).Value = converttorgb(C.Offset(0, 1).Value)
Next C
End Sub
Function ConvertToRGB(lColor As Long) As String
Dim H As String
Dim Red As Integer, Green As Integer, Blue As Integer
H = Format(Hex(lColor), "######")
Red = Val("&H" & Right(H, 2))
Green = Val("&H" & Mid(H, 3, 2))
Blue = Val("&H" & Left(H, 2))
ConvertToRGB = Format(Red, "0\, ") & Format(Green, "0\, ") & Format(Blue, "0")
End Function
Depending on how you are transferring this information to Word, you may not even need to have it on the worksheet.
With the help of the below command I am able to clear the contents of the cells but not their background color. How to clear and set the background color of cells in a range?
ob9.Range(ob9.Cells(1,StartCol),ob9.Cells(1,maxcolumn)).ClearContents
EDIT
I tried the below :
CountFill = objExcel1.Application.WorksheetFunction.CountA(ob9.Rows(1))
CountBlnk = objExcel1.Application.WorksheetFunction.CountBlank(ob9.Rows(1))
TotalColumn= CountBlnk + CountFill
ob9.Range(ob9.Cells(1,CountFill + 1 ),ob9.Cells(1,TotalColumn)).Interior.ColorIndex(-4142) '= xlColorIndexNone
Can it be done in a single line?
Thanks
Everything is fine. But don't select given you are running a huge script (knowing what you went through so far)...
with ob9.Range(ob9.Cells(1,StartCol),ob9.Cells(1,maxcolumn))
.Interior.ColorIndex = xlColorIndexNone
.Interior.ColorIndex = 120
End With
If you are directly using the range you may even remove with block, as it too has some performance-slowing drawback.
Reference article
Answer for your sub questions:
How to get column name from column number?
Excel column number from column name
How to set range based on the OP's maxcolumn name or number.
Range(row,column).
You mentioned you need row 1, maxcolumn then you can build the cell using those two data.
MsgBox Sheets(3).Rows(1).Columns(5).Address
so try out:
MsgBox Sheets(3).Rows(1).Columns(maxcolumn).Address
You could try
ob9.Range(ob9.Cells(1,StartCol),ob9.Cells(1,maxcolumn)).Select
Selection.Interior.ColorIndex = xlColorIndexNone
Selection.Interior.ColorIndex = xlNone
One of the last two lines should work, but I'm not sure off-handedly which one (I don't have Excel). If you could try both and report back, that would be great.
You can set colors using:
Selection.Interior.Color = RGB(255,0,0)
I'm currenlty putting together an If, ElseIf and Else statement to go through the lists and make it standard. The parts where I'm struggling are those cells where a superscript character has been used at the end of the sentaence for footnote references. These cells need to be given specific line heights different from the standard 10.5.
Thank you marg for the working If, ElseIf, Else statement.
But how can I find & treat those cells with a superscript character at the end (it's always at the end?
Any guidance or pointers very welcome.
The code below does not update cells with superscript only at the end of the sentence.
Dim targetCell As Range
...
ElseIf targetCell.font.superscript Then
targetCell.RowHeight = 12.75
...
etc
Many thanks
Mike.
This should do it.
Sub testForSuperscriptAtEndOfCellValue()
Dim c As Range
For Each c In Range("A:A").Cells
If c.Characters(Len(c), 1).Font.Superscript Then
c.EntireRow.RowHeight = 42
End If
Next c
End Sub