VBA Cell Format Error - excel

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

Related

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

How to highlight cells with decimals or characters less than 3 and more than 6 using vba?

I'm new to VBA coding and please help me create a VBA script with the following conditions.
Should highlight cells containing decimals.
Should highlight cells with number of characters less than 3 or more than 6.
Should execute from Column G (G1) till the last row last used cell.
My data is alphanumeric or numeric.
I have tried using characters.count and Value.count but it didn't work out. Hope it will work with len, but I'm not sure how to start with.
Attached is the sample excel file with highlighted cells
I have tried the below code. Since my data is alphanumeric, characters count doesn't help.
Sub HighlightCells()
Range(" G1").Select
Do
If ActiveCell.Characters.Count < 3 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell
Loop Until ActiveCell = ""
Range(" G1").Select
Do
If ActiveCell.Characters.Count > 6 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell
Loop Until ActiveCell = ""
End Sub
Before:
This code is almost a direct translation of your description in English into VBA:
Sub Dural()
Dim N As Long, i As Long, s As String, L As Long
N = Cells(Rows.Count, "G").End(xlUp).Row
For i = 1 To N
s = Cells(i, "G").Text
L = Len(s)
If InStr(1, s, ".") > 0 Or (L < 3 Or L > 6) Then
With Cells(i, "G").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next i
End Sub
and after:
Sub Test()
Application.ScreenUpdating = False
LastRow = Rows(ActiveSheet.UsedRange.Row + _
ActiveSheet.UsedRange.Rows.Count - 1).Row
LastCol = Columns(ActiveSheet.UsedRange.Column + _
ActiveSheet.UsedRange.Columns.Count - 1).Column
For Each cll In Range(Cells(1, 7), Cells(LastRow, LastCol))
s = cll.Value
l = Len(s)
If ((l > 0) And (l < 3)) Or (l > 6) Or (s Like "*#.#*") _
Then cll.Interior.Color = vbRed
Next cll
Application.ScreenUpdating = True
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.

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