Conditional Formatting - Subscript Out of Range - excel

I am new to conditional formatting. I am basing this off of recorded macro where I have cleaned up the code. It makes its way through one iteration but throws a "Subscript out of range" error. Would someone please help me understand whats happening here.
Dim arrAisles, arrCol
Dim rng As Range
arrAisles = Array("C9:E", "H9:J", "M9:O")
arrCol = Array(5, 10, 15)
Set rng = Sheets("Main").Range(arrAisles(i) & GetLastRow("Main", arrCol(i)))
With rng
For i = 0 To 2
.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 8109667
.TintAndShade = 0
.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValuePercentile
.FormatConditions(1).ColorScaleCriteria(2).Value = 50
.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
.TintAndShade = 0
.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 7039480
.TintAndShade = 0
Next i
End With

Related

FormatConditions Borders not supported

I have some code that should formating cells with a border around (FormatConditions), but I get an error that the object isn´t supported
"VBA Object Doesn’t Support this Property or Method Error (Error 438)"
Can anybody help? I think it depends on MeinBereich.FormatConditions.Borders(xlEdgeLeft)
Private Sub Workbook_Open()
Dim my_month As String
my_month = Format(Date, "mmmm") + " " + Format(Date, "yy")
Sheets(my_month).Activate
Range("A1").Select
LR = Cells(Rows.Count, 1).End(xlUp).Row
Dim DatumColumn As Range
Dim foundRng As Range
my_day = Format(Date, "dd") + "."
Set foundRng = Range("B2:AF2").Find(my_day)
Dim MeinBereich As Range
Set MeinBereich = Range(Cells(foundRng.Row, foundRng.Column), Cells(LR, foundRng.Column))
MeinBereich.FormatConditions.Delete
Dim intErgebnis As Integer
intErgebnis = StrComp(foundRng, my_day, vbTextCompare)
If intErgebnis = 1 Then
With MeinBereich
.FormatConditions.Add Type:=xlExpression, Formula1:="=100"
With MeinBereich.FormatConditions.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Color = -16776961
.TintAndShade = 0
.Weight = xlThick
End With
With MeinBereich.FormatConditions.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = -16776961
.TintAndShade = 0
.Weight = xlThick
End With
With MeinBereich.FormatConditions.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -16776961
.TintAndShade = 0
.Weight = xlThick
End With
With MeinBereich.FormatConditions.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Color = -16776961
.TintAndShade = 0
.Weight = xlThick
End With
End With
End If
End Sub
Format conditions and borders are a bit tricky - within VBA and GUI as well:
The basic concept:
Dim fc As FormatCondition
With MeinBereich
.FormatConditions.Delete
Set fc = .FormatConditions.Add(Type:=xlExpression, Formula1:="=100")
With fc.Borders(xlBottom)
.Color = -16776961
End With
End With
Assign the added format condition to a variable. Then you work with this to set the formatting.
You only have Borders xlBottom, xlTop, xlLeft, xlRight.
You can't set/don't need to set line style nor weight. It is always coninous and light.
As said: that's the same within GUI. If you want a bold line via format condition, you have to go the "reverse" way. Format the whole range with a normal bold border - and set it to "no border" based on the contrary condition (e.g. <> 100).

Error with changing interior of the cell?

I'm trying to write code that activates a different workbook and modifies the color of a range in that workbook. I'm getting an error message on the With lines.
I've had past problems with the code modifying cells in the wrong workbook, but I think I figured that out using ActiveSheet. The error listed is "Application or Object-Defined Error", and I have no idea what is incorrect.
Private Sub CommandButton5_Click()
projname = Range("E17")
projnum = Range("E19")
Dim obook As Workbook
Set obook = Workbooks(Replace(projname, " ", "") + ".xlsx")
obook.Activate
ActiveSheet.Range("A99") = 6
counter = ActiveSheet.Range("A99").Value
If counter Mod 2 <> 0 Then
With ActiveSheet.Range(Cells(globalcounter, 1), Cells(globalcounter, 8)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
Else
With ActiveSheet.Range(Cells(globalcounter, 1), Cells(globalcounter, 8)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End If
counter = counter + 1
End Sub

Excel Databar does not Display

I have two Excel sheets where 6 of the 30 columns have databars in them. They all use the same code as below.
But two of the columns in one of the sheets do not display the databar. When I look at Manage Rules in conditional formatting, I do see the databar there.
Then I create several files from that sheet based on different sorts - product, region, etc. and these files also inconsistently do not show the databar.
I am using Excel 2010 32-bit on Windows 7 64-bit.
What am I doing wrong? Thanks in advance for your help.
Public Function formatDatabar(wbkO As Workbook, wks As Worksheet, colNo As Integer)
Dim i As Integer, ctr As Integer, col As Integer
wbkO.Activate
Set wks = wbkO.Worksheets(wks.Name)
wks.Activate
ctr = findLastRow(wks.Name)
col = findLastCol(wks.Name)
wbkO.Activate
wks.Activate
wks.Range(wks.Cells(5, 1), wks.Cells(ctr, col)).Select
With Selection
.Cells.Font.Size = "8"
.Cells.Font.Bold = False
.Cells.Font.Name = "Calibri"
.VerticalAlignment = xlCenter
End With
wks.Range(wks.Cells(5, colNo), wks.Cells(ctr, colNo)).Select
Selection.FormatConditions.AddDatabar
Selection.FormatConditions(Selection.FormatConditions.Count).ShowValue = True
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueAutomaticMin
.MaxPoint.Modify newtype:=xlConditionValueAutomaticMax
End With
With Selection.FormatConditions(1).BarColor
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.13012579
End With
Selection.FormatConditions(1).BarFillType = xlDataBarFillGradient
Selection.FormatConditions(1).Direction = xlContext
Selection.FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor
Selection.FormatConditions(1).BarBorder.Type = xlDataBarBorderSolid
Selection.FormatConditions(1).NegativeBarFormat.BorderColorType = _
xlDataBarColor
With Selection.FormatConditions(1).BarBorder.Color
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.13012579
End With
Selection.FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic
With Selection.FormatConditions(1).AxisColor
.Color = 0
.TintAndShade = 0
End With
With Selection.FormatConditions(1).NegativeBarFormat.Color
.Color = 255
.TintAndShade = 0
End With
With Selection.FormatConditions(1).NegativeBarFormat.BorderColor
.Color = 255
.TintAndShade = 0
End With
End Function
In the 2472nd row, the % was very high, making all others almost nothing. This caused the databar for all other rows to not display.
Thanks Scott for your help.

VBA Cell Format Error

I'm trying to format a group of cells using a for loop (later a conditional while loop)
I'm getting the error '1004'
Method 'Range' of object "Global' failed
I can't figure out how to convert the cells value to a range value that doesn't throw this error. The Cells(x,y) by itself throws an error as well
Thanks in advance
For row = 2 To 5
With Range(Cells(row, 5)).Interior
.Pattern = xlPatternRectangularGradient
.Gradient.RectangleLeft = 0.5
.Gradient.RectangleRight = 0.5
.Gradient.RectangleTop = 0.5
.Gradient.RectangleBottom = 0.5
.Gradient.ColorStops.Clear
End With
With Range(Cells(row, 5)).Interior.Gradient.ColorStops.Add(0)
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Range(Cells(row, 5)).Interior.Gradient.ColorStops.Add(1)
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.250984221930601
End With
Next row
Why are you even looping?
Sub SO()
With Range(Cells(2, 5), Cells(5, 5)).Interior
.Pattern = xlPatternRectangularGradient
.Gradient.RectangleLeft = 0.5
.Gradient.RectangleRight = 0.5
.Gradient.RectangleTop = 0.5
.Gradient.RectangleBottom = 0.5
.Gradient.ColorStops.Clear
With .Gradient.ColorStops.Add(0)
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With .Gradient.ColorStops.Add(1)
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.250984221930601
End With
End With
End Sub
No need for Range()
Just do Cells(row, 5).Interior
For row = 2 To 5
With Cells(row, 5).Interior
.Pattern = xlPatternRectangularGradient
.Gradient.RectangleLeft = 0.5
.Gradient.RectangleRight = 0.5
.Gradient.RectangleTop = 0.5
.Gradient.RectangleBottom = 0.5
.Gradient.ColorStops.Clear
End With
With Cells(row, 5).Interior.Gradient.ColorStops.Add(0)
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Cells(row, 5).Interior.Gradient.ColorStops.Add(1)
.ThemeColor = xlThemeColorAccent6
.TintAndShade = -0.250984221930601
End With
Next row
Get rid of of Range()
With Cells(row, 5).Interior
You want to either use
Cells(row, col)
Cells(2, 5)
or
Range(addressString)
Range("E2")
If you don't put a worksheet object in front of either of them they'll refer to the Activesheet or 'Me' if you're working within a worksheet module.
For reference something like:
Dim ws as worksheet
set ws = Thisworkbook.sheets("Sheet1")
ws.cells(2, 5)
ws.Range("E2")

Excel Range selection problem

I'm trying to make a macro that select colors a group of 5 cells
So basically it goes:
clear, yellow, blue, clear, yellow, blue, ...
Sub ColorBanding()
Dim num As Integer
For i = 2 To 50
Dim range As String
range("A" + Str(i) + ":E" + Str(i)).Select
If i Mod 3 = 0 Then
Cells(1, 1).Select
' Yellow
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ElseIf i Mod 3 = 2 Then
' Blue
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
End If
Next i
End Sub
I'm having trouble with this line:
"range("A" + Str(i) + ":E" + Str(i)).Select"
Example on evaluation for i = 2:
outputs: "A 2:E 2"
should be: "A2:E2"
The technical error I get is:
"Expected an array"
Is there a better way of doing this?
And is there a way to get this way to work right?
This is correct syntax. Also don't select/activate anything as it only slows things down. What use is 'num'?
Sub test()
Dim num As Integer, MyRange As Range, i As Integer
For i = 2 To 50
Set MyRange = Range("A" & i & ":E" & i)
MsgBox MyRange.Address 'Delete this test line
With MyRange.Cells(1, 1).Interior
If (i Mod 3) = 0 Then
'Yellow
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
ElseIf i Mod 3 = 2 Then
'Blue
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End If
End With
Next i
End Sub
The problem is here
Dim num As Integer
For i = 2 To 50
Dim range As String
range("A" + Str(i) + ":E" + Str(i)).Select
If i Mod 3 = 0 Then
Cells(1, 1).Select
Please put all dims at the top
dim range as string? what where you thinking, a range is a range. As in a lumb of cells on a worksheet.
range is a reserved word, don't use it as a variable name, use something like MyRange instead.
Objects (like range, worksheet etc..) can only be assigned in a Set object = reference_to_other_object_of_the_same_type statement, see below.
Cells(1,1) relative to what? The default object is ActiveWorksheet and ActiveWorksheet.Cells(1,1) is permanently fixed to cell "A1". MyRange.Cells(1,1) on the other hand can move around.
In VBA '&' concatenates strings, the '+' operator only works on numbers
Change it to:
Dim num As Integer
Dim MyRange as Range
Dim i as Integer
For i = 2 To 50
Set MyRange = range("A" & Str(i) & ":E" & Str(i))
MyRange.Select
If (i Mod 3) = 0 Then
MyRange.Cells(1, 1).Select
...
Haven't tested the code yet, but I'm pretty sure it is close.

Resources