code causing a 1004 error and im not sure why - excel

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

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).

VBA Conditional Formatting of specific Borders

Hej,
So I am trying to use VBA to add conditonal formatting to compensate for some other coding, which changes the ranges from time to time. My problem is, that I only the conditional formatting to apply to xlEdgeRight and xlEdgeLeft. However, VBA always tells me that it cannot set the border style. Any ideas?
Dim rngMark As Range
Dim DateCond As FormatCondition
Dim BordNum As Long
Call wsDef
Set rngMark = wksS.Range("E11:CPB25")
rngMark.FormatConditions.Delete
Set DateCond = rngMark.FormatConditions.Add(Type:=xlExpression, Formula1:="=AND(TODAY()>=E$7,TODAY()<F$7)")
DateCond.SetFirstPriority
With DateCond
.StopIfTrue = False
.Font.ThemeColor = xlThemeColorAccent2
.Font.Bold = True
.Borders.LineStyle = xlNone
.Borders.LineStyle = xlNone
End With
For BordNum = 7 To 8
With DateCond.Borders(BordNum)
.LineStyle = xlContinuous
.TintAndShade = 0
.Color = -16776961
.Weight = xlThin
End With
Next BordNum
Programming formatconditions - especially borders - is a bit special.
You have to use xlLeft = -4131 and xlRight = -4152 in this case:
Dim BordNum As Long
Dim arrBordNum(1) As Long
arrBordNum(0) = -4131 'xlleft
arrBordNum(1) = -4152 'xlright
For BordNum = 0 To UBound(arrBordNum)
With DateCond.Borders(arrBordNum(BordNum))
.LineStyle = xlContinuous
.TintAndShade = 0
.Color = -16776961
.Weight = xlThin
End With
Next BordNum

Conditional formatting in excel using VBA for every single cell

I am new to Excel VBA and want to create VBA code to use conditional formatting the cell containing "x" only to place the borders for the cell.
I want to do the exact same thing with VBA code:
APPLIES TO EVERY SINGLE CELLS IN THE WORKBOOK.
APPLIES TO EVERY SINGLE CELLS IN THE WORKBOOK.
APPLIES TO EVERY SINGLE CELLS IN THE WORKBOOK.
This is my strategy for doing so, don't know if it is correct:
First I want to select all the cell in all worksheets containing only "x".
Then change the border of all the selected cells to "All borders".
However I got many errors in the code and I cannot fix it.
Here is my code:
Sub Test()
Dim ws As Worksheet
Dim SelectCells As Range
Dim xcell As Object
Dim i As Long
Dim Last As Long
For Each ws In ThisWorkbook.Worksheets
ws.Activate
For Each xcell In ws.UsedRange.Cells
If xcell.Value = "x" Then
If SelectCells Is Nothing Then
Set SelectCells = Range(xcell.Address)
Else
Set SelectCells = Union(SelectCells, Range(xcell.Address))
End If
End If
Next
SelectCells.Select
SelectCells.Borders(xlDiagonalDown).LineStyle = xlNone
SelectCells.Borders(xlDiagonalUp).LineStyle = xlNone
With SelectCells.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With SelectCells.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With SelectCells.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With SelectCells.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With SelectCells.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With SelectCells.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Next
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

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.

Resources