Excel Databar does not Display - excel

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.

Related

Applying Conditional Formatting Row By Row based on Cell Critieria

Struggling with this one. Have a table with many columns but looking to apply the conditional formatting to a subset. Table looks like below. Number/name of columns are fixed but number of rows varies based on some user inputs. Below image shows the table
Table Image
With this table I am looking to apply the color scale conditional formatting on a row by row basis. The added wrinkle is the formatting needs to be dependent on the value in the column titled "Sign". Logic goes if Sign value in row is "+" then applies color scale with green for highest value, red for lowest and white for 50th percentile (used macro recorder to capture stock color scales in excel to produce code below). If Sign value in row is "-" then I reverse it with Red for highest value, green for lowest.
My code is as per below. Problem I'm facing is it is duplicating the formatting for each row MANY times and slowing down everything. Any ideas why its duplicating the formatting for each row?
Any help is GREATLY appreciated!
Dim Rng As Range
Dim cel As Range
Dim i As Integer
Application.ScreenUpdating = False
For Each Rng In Worksheets("Sheet1").Range("Table1[[Sign]:[TREND5]]").Rows
For Each cel In Rng.Cells
For i = 1 To Len(cel)
If cel.Value = "+" Then
Rng.FormatConditions.AddColorScale ColorScaleType:=3
Rng.FormatConditions(Rng.FormatConditions.Count).SetFirstPriority
Rng.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With Rng.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
Rng.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
Rng.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Rng.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 16776444
.TintAndShade = 0
End With
Rng.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With Rng.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
ElseIf cel.Value = "-" Then
Rng.FormatConditions.AddColorScale ColorScaleType:=3
Rng.FormatConditions(Rng.FormatConditions.Count).SetFirstPriority
Rng.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With Rng.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
Rng.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
Rng.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Rng.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 16776444
.TintAndShade = 0
End With
Rng.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With Rng.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
End If
Next i
Next
Next
Application.ScreenUpdating = True
End Sub

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

code causing a 1004 error and im not sure why

So i have this string of code that should let me auto highlight rows and columns of active cells... The catch is it is popping a 1004 error and I'm not sure why. Possibly in the syntax?
It flags the ".colorindex=20"
Im running excel 2010
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rr
Static cc
If cc <> "" Then
With Columns(cc).Interior
.ColorIndex = xlNone
End With
With Rows(rr).Interior
.ColorIndex = xlNone
End With
End If
r = Selection.Row
c = Selection.Column
rr = r
cc = c
With Columns(c).Interior
.ColorIndex = 20
.Pattern = xlSolid
End With
With Rows(r).Interior
.ColorIndex = 20
.Pattern = xlSolid
End With
End Sub
you have to add :
Dim r, c
the code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rr
Static cc
Dim r, c
If cc <> "" Then
With Columns(cc).Interior
.ColorIndex = xlNone
End With
With Rows(rr).Interior
.ColorIndex = xlNone
End With
End If
r = Selection.Row
c = Selection.Column
rr = r
cc = c
With Columns(c).Interior
.ColorIndex = 20
.Pattern = xlSolid
End With
With Rows(r).Interior
.ColorIndex = 20
.Pattern = xlSolid
End With
End Sub
Solved... had to edit the colors in this format. and then she ran... my only catch to it is it is now reformatting the colors I already have on the sheet.
Is there a work around to that from the original code to where it highlight but doesnt reformat?
With Columns(c).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10937227
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Rows(r).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10937227
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub

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